1 / 60

Nature Lover’s View Of A Tree

leaves. branches. root. Nature Lover’s View Of A Tree. root. leaves. branches. nodes. Computer Scientist’s View. Linear Lists And Trees. Linear lists are useful for serially ordered data. (e 0 , e 1 , e 2 , …, e n-1 ) Days of week. Months in a year. Students in this class.

joelle-wall
Download Presentation

Nature Lover’s View Of A Tree

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. leaves branches root Nature Lover’s View Of A Tree

  2. root leaves branches nodes Computer Scientist’s View

  3. Linear Lists And Trees • Linear lists are useful for serially ordered data. • (e0, e1, e2, …, en-1) • Days of week. • Months in a year. • Students in this class. • Trees are useful for hierarchically ordered data.

  4. Hierarchical Data And Trees • The element at the top of the hierarchy is the root. • Elements next in the hierarchy are the children of the root. • Elements that have no children are leaves.

  5. President VP3 VP1 VP2 children of root root Manager Manager1 Manager2 Manager grand children of root Worker Bee great grand child of root Example Tree

  6. Definition • A tree t is a finite nonempty set of elements. • One of these elements is called the root. • The remaining elements, if any, are partitioned into trees, which are called the subtrees of t.

  7. President VP3 VP1 VP2 root Manager Manager1 Manager2 Manager Worker Bee Subtrees

  8. Leaves President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

  9. Parent, Siblings, Ancestors, Descendants President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

  10. Level 1 President VP3 VP1 VP2 Level 2 Manager Manager1 Manager2 Manager Level 3 Worker Bee Level 4 Levels

  11. Caution • Some texts start level numbers at 0 rather than at 1. • Root is at level 0. • Its children are at level 1. • The grand children of the root are at level 2. • And so on. • We shall number levels with the root at level 1.

  12. Level 1 President VP3 VP1 VP2 Level 2 Manager Manager1 Manager2 Manager Level 3 Worker Bee Level 4 height = depth = number of levels

  13. President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee 3 Node Degree = Number Of Children 2 1 1 0 0 1 0 0

  14. 3 President 2 1 1 VP3 VP1 VP2 0 0 1 0 Manager Manager1 Manager2 Manager 0 Worker Bee Tree Degree = Max Node Degree Degree of tree = 3.

  15. Binary Tree • Finite (possibly empty) collection of elements. • A nonempty binary tree has a root element. • The remaining elements (if any) are partitioned into two binary trees. • These are called the left and right subtrees of the binary tree.

  16. Differences Between A Tree & A Binary Tree • No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree. • A binary tree may be empty; a tree cannot be empty.

  17. General Trees • Nodes of a general tree can have any number of subtrees • A general tree can be represented using a binary tree

  18. Binary Tree Properties & Representation

  19. Minimum Number Of Nodes • Minimum number of nodes in a binary tree whose height is h. minimum number of nodes is h

  20. Maximum Number Of Nodes • All possible nodes at first h levels are present. Maximum number of nodes = 1 + 2 + 4 + 8 + … + 2h-1 = 2h - 1

  21. Number Of Nodes & Height • Let n be the number of nodes in a binary tree whose height is h. • h <= n <= 2h – 1 • log2(n+1) <= h <= n

  22. Height 4 full binary tree. Full Binary Tree • A full binary tree of a given height h has 2h – 1 nodes.

  23. Numbering Nodes In A Full Binary Tree • Number the nodes 1 through 2h – 1. • Number by levels from top to bottom. • Within a level number from left to right. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15

  24. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15 Node Number Properties • Parent of node i is node i / 2, unless i = 1. • Node 1 is the root and has no parent.

  25. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15 Node Number Properties • Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. • If 2i > n, node i has no left child.

  26. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15 Node Number Properties • Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. • If 2i+1 > n, node i has no right child.

  27. About node i • Parent • If i==1, node i is root • Else i/2 • left child • If 2*i > n, node i is leave • Else 2*i • Right child • If 2*i+1 > n, node i has no right child • Else 2*i+1

  28. Complete Binary Tree With n Nodes • Start with a full binary tree that has at least n nodes. • Number the nodes as described earlier. • The binary tree defined by the nodes numbered 1 through n is the unique n node complete binary tree.

  29. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15 Example • Complete binary tree with 10 nodes.

  30. Binary Tree Representation • Array representation. • Linked representation.

  31. b 1 a 2 3 c 4 5 6 7 d f e g 8 9 10 h i j tree[] 0 5 10 Array Representation • Number the nodes using the numbering scheme for a full binary tree. The node that is numbered i is stored in tree[i]. a b c d e f g h i j

  32. 1 a 3 b 15 7 d c tree[] a - b - - - c - - - - - - - d 0 5 10 15 Right-Skewed Binary Tree • An n node binary tree needs an array whose length is between n+1 and 2n.

  33. Linked Representation • Each binary tree node is represented as an object whose data type is TreeNode. • The space required by an n node binary tree is n * (space required by one node).

  34. The Struct binaryTreeNode class TreeNode { int data; TreeNode *leftChild, *rightChild; TreeNode() {leftChild = rightChild = NULL;} // other constructors come here };

  35. root a c b d f e g h leftChild data rightChild Linked Representation Example

  36. Binary Tree Traversal • Many binary tree operations are done by performing a traversal of the binary tree. • In a traversal, each element of the binary tree is visited exactly once. • During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

  37. Binary Tree Traversal Methods • Preorder • Inorder • Postorder • Level order

  38. a b c Preorder Example (Visit = print) a b c

  39. a b c f e d j g h i Preorder Example (Visit = print) a b d g h e i c f j

  40. Preorder Traversal void PreOrder(TreeNode *t) { if (t != NULL) { Visit(t); PreOrder(t->leftChild); PreOrder(t->rightChild); } }

  41. / * + e f + - a b c d Preorder Of Expression Tree / * + a b - c d + e f Gives prefix form of expression!

  42. a b c Inorder Example (Visit = print) b a c

  43. a b c f e d j g h i Inorder Example (Visit = print) g d h b e i a f j c

  44. Inorder Traversal void InOrder(TreeNode *t) { if (t != NULL) { InOrder(t->leftChild); Visit(t); InOrder(t->rightChild); } }

  45. / * + e f + - a b c d a + b * c - d / e + f Inorder Of Expression Tree Gives infix form of expression (sans parentheses)!

  46. a b c Postorder Example (Visit = print) b c a

  47. a b c f e d j g h i Postorder Example (Visit = print) g h d i e b j f c a

  48. Postorder Traversal void PostOrder(TreeNode *t) { if (t != NULL) { PostOrder(t->leftChild); PostOrder(t->rightChild); Visit(t); } }

  49. / * + e f + - a b c d Postorder Of Expression Tree a b + c d - * e f + / Gives postfix form of expression!

  50. a b c f e d j g h i Level-Order Example (Visit = print) a b c d e f g h i j

More Related