Download presentation

Presentation is loading. Please wait.

Published byForrest Hane Modified over 2 years ago

1
Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.

2
Trees2 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 D A CE F B ADF C E

3
Trees3 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 D A CE BADCE

4
Trees4 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 D A C G BADCE F D F G

5
Trees5 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 Make Money Fast! 1. MotivationsReferences2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 1 2 3 5 4 678 9 Algorithm preOrder(v) visit(v) for each child w of v preorder (w)

6
Trees6 Preorder Traversal (Another example) My Explanation: When a node is reached, visit it. Visit the sub-trees rooted by its children one by one. 1 2 3 9 6 10 13 14 17 Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 15 161211 8 7 54

7
Trees7 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 Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) cs16/ homeworks/ todo.txt 1K programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 456 8

8
Trees8 Postorder Traversal (Another example) 17 7 3 15 6 10 11 14 16 12 1398 5 4 21. 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. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v)

9
Trees9 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 51 32

10
Trees10 Array-Based Representation of Binary Trees nodes are stored in an array … 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 1 23 6 7 45 1011 A HG FE D C B J

11
Trees11 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 2 h leaves and 2 h -1 internal nodes. A full binary tree of height 2 This is not a full binary tree. 1 2 3 4 5 67

12
Trees12 Properties of Proper Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height Properties for proper binary tree: e i 1 n 2e 1 h i e 2 h h log 2 e No need to remember. 1 2 3 6 7 15 14

13
Trees13 Depth(v): no. of ancestors of v Make Money Fast! 1. MotivationsReferences2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 0 1 2 1 2 222 1 Algorithm depth(T,v) If T.isRoot(v) then return 0; else return 1+depth(T, T.parent(v))

14
Trees14 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. Make Money Fast! 1. Motivations 2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 1 2 0 2 22 2 1

15
Trees15 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 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.

16
Trees16 Height(T,v): Make Money Fast! 1. MotivationsReferences2. Methods 2.1 Stock Fraud 2.2 Ponzi Scheme 1.1 Greed1.2 Avidity 2.3 Bank Robbery 2 1 0 1 0 000 1 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

17
Trees17 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 51 32 This time is slightly worse than last time. I should prepare the speech carefully. 5.33?

Similar presentations

OK

CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.

CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Presenter media free download ppt on pollution Download ppt on science and technology Think pair share ppt online Ppt on mind reading machine Ppt on you can win as long as you keep Ppt on layer 3 switching configuration Ppt on rf remote control robot Download ppt on web designing Download ppt on wind energy powerpoint Download ppt on bill gates biography