This preview shows pages 1–7. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 Trees 2 Readings ¡ Required [Carrano & Prichard] Chapter 10 3 Linked list Each node in a Linked list has one child. To traverse from the head node to the last node requires O(n) time. 4 A Tree as a Data Structure • shown upside down • Used to represent relationships • has a hierarchy 5 Definitions node edge Data objects (the circles) in a tree are called nodes . Links between nodes are called edges . Data objects (the circles) in a tree are called nodes. Links between nodes are called edges. 6 Relationship ¡ A is a parent of B and C ¡ B and C are children of A ¡ B and C are siblings (with the same parent A) D E A B F C 7 Relationship ¡ D is an ancestor of B. ¡ B is a descendant of A and D. ¡ Definition: Node X is an ancestor of node Y if ¢ X is a parent of Y, or ¢ X is a parent of some node Z and Z is an ancestor of Y. D E A B F C 8 Tree Nodes root (has no parent) internal nodes (has one parent and at least one child) leaves (has no children) Every node (except the root) of a tree has one parent. A node with no children is a leaf node. In every tree, there is a node without parent, called the root node. Nodes without children are called the leave nodes. The nonleave nodes are also called internal nodes. 9 subtree Subtree A node and all of its descendants form a subtree Q: Can a leaf be a subtree? 10 Tree is recursive ! A tree is either • nothing, or • A node, with some set of subtrees, each of which is a tree ... A tree is recursive in nature. A node and all of its descendant form a subtree. 11 Level of a node ¡ Number of nodes on the path from the root to the node ¢ level of root is 1 ¢ level of A is 2 A B C level Different textbooks give different definition of level. We use the definition as shown in the slide 12 Height of a tree ¡ Maximum level of the nodes in the tree is the height of the tree A B C height = 4 Again, our definition of height is given in the slide. 13 Size of a tree ¡ Number of nodes in the tree is the size of the tree A • The size of this tree is 10. • The size of the subtree rooted at A is 4. Empty tree has size 0 14 Applications of Trees A tree can be used to represent data that is hierarchical in Nature A tree can be used to represent data that is hierarchical in nature. 15 File systems Desktop My Documents My Computer … C: D: H: I: … For example, a file system can be represented as a tree, with the topmost directory as the root (in OS term, this is called the “root” directory). 16 Arithmetic Expressions (a+b) * (ab) + 2 + 2 * + a b a b Q: How do you construct such a tree from a given arithmetic expression? An arithmetic expression can be represented by a tree as well. The leave nodes are the variables/values. The internal nodes are the operations....
View Full
Document
 Spring '11
 dunnowho

Click to edit the document details