More on Trees University Fac. of Sci. & Eng. CS Dept. EE Dept. Bus. School Law School Math. Dept. Trees 1 Linked Structure for Trees A node is represented by an object storing Element Parent node Sequence of children nodes B Node objects implement the Position ADT A B D A C D F F E C Trees E 2 Linked Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node B Node objects implement the Position ADT B A A D C D E C Trees E 3 Linked Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node B Node objects implement the Position ADT B A A D D C F D E C G Trees 4 G Preorder Traversal 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 Algorithm preOrder(v) visit(v) for each child w of v preorder (w) Make Money Fast! 2 5 1. Motivations 9 2. Methods 3 4 1.1 Greed 1.2 Avidity 6 7 2.1 Stock Fraud Trees 2.2 Ponzi Scheme References 8 2.3 Bank Robbery 5 Preorder Traversal (Another example) My Explanation: When a node is reached, visit it. Visit the sub-trees rooted by its children one by one. Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 1 2 3 4 9 6 5 7 17 14 10 8 11 13 Trees 12 15 166 Postorder Traversal In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) cs16/ 3 8 7 homeworks/ todo.txt 1K programs/ 1 2 h1c.doc 3K h1nc.doc 2K 4 5 DDR.java 10K Trees Stocks.java 25K 6 Robot.java 20K 7 Postorder Traversal (Another example) My explanation: Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) . 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. 17 7 3 1 15 14 6 2 4 16 10 5 8 11 Trees 9 12 138 Evaluate Arithmetic Expressions Specialization of a postorder traversal recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees + Algorithm evalExpr(v) if isExternal (v) return v.element () else x evalExpr(leftChild (v)) y evalExpr(rightChild (v)) operator stored at v return x y - 2 5 3 2 1 Trees 9 Array-Based Representation of Binary Trees nodes are stored in an array 1 A … 2 3 B D let rank(node) be defined as follows: 4 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 Trees 5 E 6 7 C F 10 J 11 G H 10 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 5 6 7 A full binary tree of height 2 This is not a full binary tree. Trees 11 Properties of Proper Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height 1 2 Properties for proper binary tree: e = i + 1 n = 2e - 1 h i e 2h 3 h log2 e 7 6 14 Trees 15 No need to remember. 12 Depth(v): no. of ancestors of v Algorithm depth(T,v) If T.isRoot(v) then return 0; else return 1+depth(T, T.parent(v)) 0 Make Money Fast! 1 1 1. Motivations 1 2. Methods 2 2 1.1 Greed 1.2 Avidity 2 2 2.1 Stock Fraud Trees 2.2 Ponzi Scheme References 2 2.3 Bank Robbery 13 Height(T): the height of T is max depth of an external node Algorithm height1(T) h=0; for each vT.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 1 2 Make Money Fast! 2. Methods 1. Motivations 1.1 Greed 1.2 Avidity 2 2.1 Stock Fraud Trees 2 2.2 Ponzi Scheme 2 1 2.3 Bank Robbery 2 14 Height(T,v): 1. If v is an external node, then height of v is 0. 2. 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 wT.children(v) do h=max(h, height2(T, w)) return 1+h Trees 15 Height(T,v): Algorithm height2(T,v) if T.isExternal(v) then return 0 else h=0 for each wT.children(v) do h=max(h, height2(T, w)) return 1+h 2 Make Money Fast! 1 1 1. Motivations 1 2. Methods 0 0 1.1 Greed 1.2 Avidity 0 0 2.1 Stock Fraud Trees 2.2 Ponzi Scheme References 0 2.3 Bank Robbery 16 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 + - 2 5 3 2 This time is slightly worse than last time. I should prepare the speech carefully. 5.33? 1 Trees 17