Chapter 6

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Trees 2 definitions:

A tree consists of a set of nodes and a set of edges that connect pairs of nodes. A tree has the following properties: One node of the tree is designated as the root node. Every node n, except the root node, is connected by an edge from exactly one other node p, where p is the parent of n. A unique path traverses from the root to each node. Recursive definition: A tree is either empty or consists of a root and zero or more subtrees, each of which is also a tree. The root of each subtree is connected to the root of the parent tree by an edge. Using the recursive definition of a tree, we know that the tree in Figure 4 has at least four nodes, since each of the triangles representing a subtree must have a root. It may have many more nodes than that, but we do not know unless we look deeper into the tree.

List of lists implementation of tree

Although writing the interface as a set of operations on a list is a bit different from the other abstract data types we have implemented, it is interesting to do so because it provides us with a simple recursive data structure that we can look at and examine directly. In a list of lists tree, we will store the value of the root node as the first element of the list. The second element of the list will itself be a list that represents the left subtree. The third element of the list will be another list that represents the right subtree.

Tree vocabulary Nodes Edges (links?) Root Path Children Parent Siblings Descendants Subtree Leaf node Internal node

Each circle is called a node or vertex An edge connects two nodes to show that there is a relationship between them. Every node (except the root) is connected by exactly one incoming edge from another node. Each node may have several outgoing edges. The root of the tree is the only node in the tree that has no incoming edges. A path is an ordered list of nodes that are connected by edges. Children of a node n are the set of nodes below n and connected to n by an edge. 2 and 3 are children of 1, 4, 5, 6, and 7 are grandchildren, 2 and 3 are siblings Parents of a node n is the node above n and connected to n by an edge Siblings are nodes with the same parents Descendants of a node n are children of n + descendants of the children. Basically all nodes below n that are somehow connected to it Subtree: a node and its descendants, the tree starting at 2 is a subtree. BUT node 4 all by itself would also be a subtree Leaf node: a node with no children Internal node: any node that has children. That is, any node that's not a leaf node.

Why can't we say that every node has two nodes for the definition of a binary tree?

If you said that every node has to have two, it would never end.

3 properties or trees

Trees are hierarchical. You can measure the distance of a node from its root (its level) All of the children of one node are independent of the children of another Each leaf node is unique. We can specify a unique path from the root of the tree to a leaf.

In the case where the tree is more than a binary tree,

another subtree is just another list.

Another important property of trees, derived from their hierarchical nature, is that you can move...

entire sections of a tree (called a subtree) to a different position in the tree without affecting the lower levels of the hierarchy.

Height of the tree:

maximum level of any node of the tree

Notice that we can access subtrees of the list using standard list indexing. The root of the tree is _____, the left subtree of the root is _____, and the right subtree is ______

myTree[0] myTree[1] myTree[2]

If each node in the tree has a maximum of two children, we say...

that the tree is a binary tree.

The level of a node is

the number of edges between the node and the root. The root node is at level 0.


Set pelajaran terkait

Fundamentals Physiological (eaq)Aspects

View Set

Chapter 16: Spirituality Practice Questions

View Set

QUILLBOT VERSION OF PRE AND POST ASS

View Set