1 / 17

More on Trees

More on Trees. University. Fac. of Sci. & Eng. Law School. Bus. School. Math. Dept. CS Dept. EE Dept. . Linked Structure for Trees. A node is represented by an object storing Element Parent node Sequence of children nodes Node objects implement the Position ADT. B. . . A. D.

kynton
Download Presentation

More on Trees

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. More onTrees University Fac. of Sci. & Eng. Law School Bus. School Math. Dept. CS Dept. EE Dept. Trees

  2. Linked Structure for Trees • A node is represented by an object storing • Element • Parent node • Sequence of children nodes • Node objects implement the Position ADT B   A D F B A D F   C E C E Trees

  3. D C A B E Linked Structure for Binary Trees  • A node is represented by an object storing • Element • Parent node • Left child node • Right child node • Node objects implement the Position ADT   B A D     C E Trees

  4. D C B A E Linked Structure for Binary Trees  • A node is represented by an object storing • Element • Parent node • Left child node • Right child node • Node objects implement the Position ADT   B A D     D C     F Trees G G F

  5. Preorder Traversal AlgorithmpreOrder(v) visit(v) foreachchild w of v preorder (w) • A traversal visits the nodes of a tree in a systematic manner • In a preorder traversal, a node is visited before its descendants • Application: print a structured document 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.3 BankRobbery 2.1 StockFraud 2.2 PonziScheme 1.1 Greed 1.2 Avidity Trees

  6. Preorder Traversal (Another example) AlgorithmpreOrder(v) visit(v) foreachchild w of v preorder (w) My Explanation: • When a node is reached, visit it. • Visit the sub-trees rooted by its children one by one. 1 2 17 9 14 3 6 10 13 4 5 7 Trees 11 12 16 8 15

  7. Postorder Traversal AlgorithmpostOrder(v) foreachchild w of v postOrder (w) visit(v) • In a postorder traversal, a node is visited after its descendants • Application: compute space used by files in a directory and its subdirectories 9 cs16/ 8 3 7 todo.txt1K homeworks/ programs/ 4 5 6 1 2 Robot.java20K h1c.doc3K h1nc.doc2K DDR.java10K Stocks.java25K Trees

  8. Postorder Traversal (Another example) My explanation: • If the reached node is a leaf, then visit it. • When a node is visited, visit the sub-tree rooted by its sibling on the right. • When the leftmost child is visited, visit its parent. AlgorithmpostOrder(v) foreachchild w of v postOrder (w) visit(v) . 17 7 16 15 14 3 6 10 11 1 2 4 Trees 8 9 13 5 12

  9. +   2 - 3 2 5 1 Evaluate Arithmetic Expressions AlgorithmevalExpr(v) ifisExternal (v) returnv.element () else x evalExpr(leftChild (v)) y evalExpr(rightChild (v))  operator stored at v returnx  y • Specialization of a postorder traversal • recursive method returning the value of a subtree • when visiting an internal node, combine the values of the subtrees Trees

  10. A … B D C E F J G H Array-Based Representation of Binary Trees • nodes are stored in an array 1 2 3 • let rank(node) be defined as follows: • rank(root) = 1 • if node is the left child of parent(node), rank(node) = 2*rank(parent(node)) • if node is the right child of parent(node), rank(node) = 2*rank(parent(node))+1 4 5 6 7 10 11 Trees

  11. Full Binary Tree • A full binary tree: • All the leaves are at the bottom level • All nodes which are not at the bottom level have two children. • A full binary of height h has 2h leaves and 2h-1 internal nodes. 1 3 2 4 6 7 5 This is not a full binary tree. A full binary tree of height 2 Trees

  12. Properties of Proper Binary Trees • Properties for proper binary tree: • e = i +1 • n =2e -1 • h  i • e 2h • h log2e • Notation n number of nodes e number of external nodes i number of internal nodes h height 1 3 2 7 6 No need to remember. 14 15 Trees

  13. Depth(v): no. of ancestors of v Algorithmdepth(T,v) If T.isRoot(v) then return 0; else return 1+depth(T, T.parent(v)) 0 Make Money Fast! 1 1 1 1. Motivations 2. Methods References 2 2 2 2 2 2.3 BankRobbery 2.1 StockFraud 2.2 PonziScheme 1.1 Greed 1.2 Avidity Trees

  14. Height(T): the height of T is max depth of an external node Algorithm height1(T) h=0; for each vT.positions() do if T.isExternal(v) then h=max(h, depth(T, v)) return h T.positions() holds all nodes in T. See the method in LinkedBinaryTree.java. The max is 2. 0 Make Money Fast! 1 1 1. Motivations 2. Methods 2.3 BankRobbery 2.1 StockFraud 2.2 PonziScheme 2 2 2 2 1.1 Greed 1.2 Avidity Trees 2

  15. Height(T,v): • If v is an external node, then height of v is 0. • Otherwise, the height of v is one +max height of a child of v. Algorithm height2(T,v) if T.isExternal(v) then return 0 else h=0 for each wT.children(v) do h=max(h, height2(T, w)) return 1+h Trees

  16. Height(T,v): Algorithm height2(T,v) if T.isExternal(v) then return 0 else h=0 for each wT.children(v) do h=max(h, height2(T, w)) return 1+h 2 Make Money Fast! 1 1 1 1. Motivations 2. Methods References 0 0 0 0 0 2.3 BankRobbery 2.1 StockFraud 2.2 PonziScheme 1.1 Greed 1.2 Avidity Trees

  17. +   2 - 3 2 5 1 Next time: Chapter 7. Heap Exercise1: Given a binary tree T, and two nodes u and v in T, test if u is an ancestor of v. Boolean isAncestor(T, u, v) Exercise2: Suppose that we use array-based representation for a binary tree. Give the positions of the nodes in the following tree This time is slightly worse than last time. I should prepare the speech carefully. 5.33? Trees

More Related