1 / 51

Tree Data Structures

Tree Data Structures. Introductory Examples. Willliam Bill Mary Curt Marjorie Richard Anne. Data organization such that items of information are related by branches. Branch Meaning: Parent Of. Introductory Examples. Curt

geona
Download Presentation

Tree Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tree Data Structures

  2. Introductory Examples Willliam Bill Mary Curt Marjorie Richard Anne • Data organization such that items of information are related by branches. • Branch Meaning: Parent Of

  3. Introductory Examples Curt Bill Carol Ronald Alison Ashley William Laura Victoria Jennifer Todd Dylan Trey Shelby

  4. General Tree Definition • A tree is a finite set of one or more nodes such that: • There is a specially designated node called the root • The remaining nodes are partitioned into n >= 0 disjoint sets T1, T2, .. Tn, where each of these sets is a tree. T1, T2, … Tn are called subtrees of the root.

  5. Introductory Examples Curt Bill Carol Ronald Alison Ashley William Laura Victoria Jennifer Todd Dylan Trey Shelby Root: Curt Bill is a subtree of Curt, and is a tree in itself

  6. Introductory Examples Bill Alison Ashley William Laura Dylan Root: Bill Ashley is a subtree of Bill, and a tree in itself Dylan is a subtree of Ashley, and a tree in itself

  7. Tree Definition • Order of subtrees is irrelevant • These two tree are equivalent: Curt Bill Carol Ronald Curt Ronald Bill Carol

  8. Tree Definitions • A node is an item of information and has branches to other nodes • Similar to linked list node • The degree of a node is the number of subtrees of that node • Nodes with a degree of 0 are terminal nodes or leaf nodes • Nodes with a degree > 0 are non-terminal nodes

  9. Tree Definitions Curt Bill Carol Ronald Alison Ashley William Laura Victoria Jennifer Todd Dylan Trey Shelby Degree(Curt): 3 Degree(Bill): 4 Degree(Ashley): 1 Degree(William): 0 Terminal Nodes: Alison, Dylan, William, Laura, Trey, Shelby, Jennifer, Todd, Ronald – Anyone with no children in real life

  10. Tree Definitions • Roots of subtrees of nodes are children • Parent has one or more children • Children of the same parent are siblings

  11. Introductory Examples Curt Bill Carol Ronald Alison Ashley William Laura Victoria Jennifer Todd Dylan Trey Shelby Children(Curt): Bill, Carol, Ronald Parent(Jennifer): Carol Sibling(Ashley): Alison, William, Laura

  12. Tree Definitions • Grandparentis the parent of the parent of a node • Ancestors: All nodes along the path from the root to the current node. • Descendants: All elements in all subtrees for which this node is a parent.

  13. Introductory Examples Curt Bill Carol Ronald Alison Ashley William Laura Victoria Jennifer Todd Dylan Trey Shelby Grandparent(Dylan): Bill Ancestors(Dylan): Ashley, Bill, Curt Descendants(Bill): Alison, Ashley, William, Laura, Dylan

  14. Tree Definitions • Degree(Tree T): Maximum degree of the nodes in T. • Level(Node N): • Level(Root): 1 • Level(Non-Root): (Number of links between root and the node) + 1 • Height(Tree T): Maximum level of any node in T, also called Depth

  15. Introductory Examples Curt Bill Carol Ronald Alison Ashley William Laura Victoria Jennifer Todd Dylan Trey Shelby Degree(T): 4 [from Bill] Level(Curt): 1, Level(Ashley): 3, Level(Dylan): 4 Height(T): 4 [from Dylan]

  16. Recursion and Trees • Note that the tree definition itself is recursive • Children of a node are roots of their own tree • Most operators we define will be recursive: • Computing height • Traversing • Searching for a node

  17. Tree Representations • Coding a Data Structure For Trees: • First thought: • Follow our linked list node: Store data and pointers to the children • Sticky issues: • Need pointers to all children • What if variable number of children?

  18. Tree Representations • For a tree with fixed maximum degree of k, could have each node store k pointers: • Potentially very wasteful: Assume k-ary tree, with n nodes • nk – n + 1 unused pointers • Only use if most all pointers are going to be used – very dense tree Node for tree with max degree 4 DATA CHILD 1 CHILD 2 CHILD 3 CHILD 4

  19. Tree Representation • Proof: • n nodes, each with k pointers : nk available pointers • Exactly one pointer to each valid node except root: (n-1) used pointers • Total unused = nk – (n-1) => nk – n + 1 • Example: • Turkett Family Tree: 4-ary, 14 nodes • 56 – 14 + 1 = 43 unused pointers (172 bytes wasted) • Mainly wasted in terminal nodes

  20. Tree Representation • Representing arbitrary trees with just two links per node • Left-Child, Right-Sibling Data Left Child Right Sibling

  21. Bill Carol Ronald Alison Victoria 0 0 0 Tree Representation Left Child, Right Sibling Family Tree Curt 0

  22. Specialized Trees • Binary Tree: • A restriction of trees such that the maximum degree of a node is 2. • Order of nodes is now relevant • May have zero nodes • Formal Definition: • A binary tree is a finite set of nodes that either is empty or consists of a root and two disjoint subtrees called the left subtree and the right subtree.

  23. Specialized Trees These are equivalent trees, but are not equivalent binary trees, as binary trees enforce a more specific structure (a meaning of left and right).

  24. Types of Binary Trees Complete – More balanced, All terminals on same or adjacent levels (Definition will come later) Skewed - Unbalanced

  25. Properties of Binary Trees • Given definition of binary tree, interesting (useful?) properties that hold: • Maximum number of nodes on level l is 2l-1 • Maximum number of nodes in a binary tree of depth k is 2k - 1

  26. Proofs • Maximum number of nodes on level l is 2l-1 • Proof by induction: • Base step: • Level 1 = 21-1 = 20 = 1, which holds (root) • Assume holds for n, n > 1 • Level n = 2n-1 • Each node in level n can have at most 2 children in level n+1, so at most 2n-1 * 2 = 2n nodes in level n+1 => it holds (n+1-1 = n)

  27. Proofs • Maximum number of nodes in a binary tree of depth k is 2k – 1 Maximum number of nodes in tree is sum over maximum nodes in a level Sum (l = 1 to k) (2l-1) = 1 + 2 + 4 + 8 … Sum of all powers of 2 up to, but not including, 2k = 2k -1

  28. Properties of Binary Trees • A full binary tree of depth k is a binary tree of depth k having 2k – 1 nodes • All nodes have two children • All terminal nodes on same level 1 3 2 4 5 6 7

  29. Properties of Binary Trees • A binary tree with n nodes and depth k is complete if its nodes correspond to the nodes numbered from 1 to n in a full binary tree of depth k • The height of a complete binary tree with n nodes is (log2(n+1)) – Important and useful!

  30. Properties of Binary Trees • Complete trees 1 3 2 4 5 6 7 1 3 2 1 4 5 3 2

  31. Binary Tree Implementation • Array implementation • Map each numbered node into appropriate spot in one-dimensional array 1 3 2 4 5 6 7

  32. Binary Tree Implementation • Array implementation: • If a complete binary tree with n nodes is represented in an array: • parent(i) is at floor ( i / 2) if i != 1 [i == 1 is the root which has no parent] • left_child(i) = 2i. If 2i > n, i has no left child • right_child(i) = 2i + 1. If 2i + 1 > n, i has no right child Array Index: 0 1 2 3 4 5 6 7 Blank Data1 Data2 Data3 Data4 Data5 Data6 Data7

  33. Binary Tree Implementation • As before, array representations are not the best choice: • Fixed memory size • Doesn’t expand easily • Very wasteful of space unless well balanced. • Better representation: Similar to linked list node • Can move away from sibling representation and just point to two children from a node

  34. Binary Tree Node class BinaryTree; // forward declaration class BinaryTreeNode { friend class BinaryTree; private: char data; BinaryTreeNode* leftChild; BinaryTreeNode* rightChild; };

  35. Binary Tree Class class BinaryTree {public: // public member methods private: BinaryTreeNode* root; };

  36. Binary Tree Node • Only problem with linked node representation: • Difficult to find parent • OK: • Generally not that important to a lot of algorithms • If is important, when traversing list maintain a parent pointer • Could add a parent pointer link to all nodes if particularly important without substantial changes, but with n (number of nodes) bytes more use of memory

  37. Questions • For the binary tree below: • What are terminal nodes? • What are non-terminal nodes? • What is level of each node? D,E A,B,C A=1,B=2,CD=3,E=4 A B C D E

  38. Questions • What is the maximum number of nodes in a k-ary tree of height h? • For 3 = 1 + 3 + 9 + 27 + 81 … • For 4 = 1 + 4 + 16 + 64 … • For k = 1 + k + k2 + k3 … Sum of geometric series: (kh-1) / (k-1) k = 3, h = 4 = (34 – 1) / (3-1) = 80/2 = 40 Does it hold for 2? (2h-1) / (2-1) = 2h-1

  39. Blank A B Blank C D Blank Blank E Blank Questions What would an array representation of the following tree look like: A B C D E

  40. Binary Tree Class • What are functions of interest? class BinaryTree {public: BinaryTree(); // create empty tree BinaryTree(char data, BinaryTree bt1, BinaryTree bt2); //construct a new tree , setting root data to data and links to //other trees bool isEmpty(); // test whether empty BinaryTree leftChild(); // get left child of *this BinaryTree rightChild(); // get right child of *this char Data(); // return data in root node of *this };

  41. Binary Tree Functions • Most operations performed on binary trees require moving through the tree: • Visiting nodes • Inserting an element • Deleting an element • Non-recursive height calculation • … • Useful to have a simple mechanism for moving through trees

  42. Binary Tree Traversal • Tree traversal – • Visit each node in the tree exactly once • Perform some operation • Print data • Add to sum • Check for max height • A traversal produces a linear ordering of the nodes [the order of visits]

  43. Binary Tree Traversal • Treat trees and subtrees in same fashion • Traverse in same order • Use recursion! • Let the following define traversal operations: • L => Move left • V => Visit [Perform operation – print, sum, …] • R => Move right

  44. Binary Tree Traversal • Six possible methods of traversal: • LVR, LRV, VLR, VRL, RVL, RLV • Usually, only use three of these, all with left before right: LVR LRV VLR Inorder Postorder Preorder

  45. Binary Tree Traversal • Let’s trace the traversal functions with examples of expressions (operators and operand) • Traversal Name corresponds to order of outputted expression

  46. Binary Tree Traversal Inorder: LVR A / B * C * D + E Infix expression Visit left child before parent + * E D * / C A B

  47. Binary Tree Traversal • Inorder implementation: void BinaryTree::inorder() { inorder(root); } Void BinaryTree::inorder(BinaryTreeNode* node) { if (node) { inorder(node->leftChild); cout << node->data; // replace cout with inorder(node->rightChild); //arbitrary processing } }

  48. Binary Tree Traversal Postorder: LRV A B / C * D * E + Postfix expression Visit left and right child before parent + * E D * / C A B

  49. Binary Tree Traversal • Postorder implementation: void BinaryTree::postorder() { postorder(root); } void BinaryTree::postorder(BinaryTreeNode* node) { if (node) { postorder(node->leftChild); postorder(node->rightChild); cout << node->data; } }

  50. Binary Tree Traversal Preorder: VLR + * * / A B C D E Prefix expression Visit parent before either child + * E D * / C A B

More Related