Definitions Topic 18 Binary Trees A tree is an abstract data type root node internal one entry point, the root nodes "A tree may grow a Each node is either a leaf or an internal node thousand feet tall, but An internal node has 1 or more its leaves will return to children , nodes that can be reached directly from that its roots." internal node. -Chinese Proverb The internal node is said to be the parent of its child nodes leaf nodes CS314 Binary Trees 2 Properties of Trees Properties of Trees and Nodes root Only access point is the root siblings: two nodes that have the same parent All nodes, except the root, have one parent edge edge: the link from one like the inheritance hierarchy in Java node to another Traditionally trees drawn upside down path length: the number of root siblings edges that must be traversed to get from one node to another path length from root to this node is 3 leaves CS314 Binary Trees 3 CS314 Binary Trees 4
More Properties of Trees Tree Visualization depth: the path length from the root of the tree A to this node height of a node: The maximum distance B D C (path length) of any leaf from this node a leaf has a height of 0 the height of a tree is the height of the root of that F G H I J E tree descendants : any nodes that can be reached via 1 or more edges from this node K L M ancestors: any nodes for which this node is a descendant N O CS314 Binary Trees 5 CS314 Binary Trees 6 Clicker 1 Binary Trees What is the depth of the node that contains There are many variations on trees but we M on the previous slide? will start with binary trees A. 0 binary tree: each node has at most two children B. 1 the possible children are usually referred to as C. 2 the left child and the right child D. 3 parent E. 4 Clicker 2 - Same tree, same choice What is the height of the node right child left child that contains D? CS314 Binary Trees 7 CS314 Binary Trees 8
Full Binary Tree Clicker 3 full binary tree: a binary tree is which each What is the maximum height of a full binary node has exactly 2 or 0 children tree with 11 nodes? A. 3 B. 5 C. 7 D. 10 E. Not possible to have full binary tree with 11 nodes. CS314 Binary Trees 9 CS314 Binary Trees 10 Complete Binary Tree Clicker 4 complete binary tree: a binary tree in which What is the height of a complete binary tree every level, except possibly the deepest is that contains N nodes? completely filled. At depth n, the height of the A. O(1) tree, all nodes are as far left as possible B. O(logN) C. O(N 1/2 ) D. O(N) E. O(NlogN) Recall, order can be applied to any function. Where would the next node go It doesn't just apply to running time. to maintain a complete tree? CS314 Binary Trees 11 CS314 Binary Trees 12
Perfect Binary Tree A Binary Node class public class Bnode <E> { perfect binary tree: a binary tree with all leaf private E myData; nodes at the same depth. All internal nodes private Bnode<E> myLeft; private Bnode<E> myRight; have exactly two children. a perfect binary tree has the maximum public BNode(); public BNode(E data, Bnode<E> left, number of nodes for a given height Bnode<E> right) a perfect binary tree has (2 (n+1) - 1) nodes public E getData() public Bnode<E> getLeft() where n is the height of the tree public Bnode<E> getRight() height = 0 -> 1 node public void setData(E data) height = 1 -> 3 nodes public void setLeft(Bnode<E> left) height = 2 -> 7 nodes public void setRight(Bnode<E> right) height = 3 -> 15 nodes } CS314 Binary Trees 13 CS314 Binary Trees 14 Binary Tree Traversals Results of Traversals Many algorithms require all nodes of a binary tree To determine the results of a traversal on a be visited and the contents of each node processed given tree draw a path around the tree. or examined. start on the left side of the root and trace around There are 4 traditional types of traversals the tree. The path should stay close to the tree. preorder traversal: process the root, then process all sub 12 pre order: process when trees (left to right) pass down left side of node in order traversal: process the left sub tree, process the 12 49 13 5 42 root, process the right sub tree in order: process when pass 49 42 post order traversal: process the left sub tree, process underneath node the right sub tree, then process the root 13 49 5 12 42 level order traversal: starting from the root of a tree, 13 5 post order: process when process all nodes at the same depth from left to right, pass up right side of node then proceed to the nodes at the next depth. 13 5 49 42 12 CS314 Binary Trees 15 CS314 Binary Trees 16
Clicker 5 - Tree Traversals Implement Traversals Implement preorder, inorder, and post order A What is a the result of a traversal post order traversal of Big O time and space? D the tree to the left? C Implement a level order traversal using a A. F C G A K H L D J queue B. F G C K L H J D A F G H J C. A C F G D H K L J Big O time and space? D. A C D F G H J K L Implement a level order traversal without a K L queue E. L K J H G F D C A target depth CS314 Binary Trees 17 CS314 Binary Trees 18 Breadth First Search Clicker 6 Depth First Search Which traversal of a binary tree is a breadth from NIST - DADS first search? breadth first search: Any search algorithm that A. Level order traversal considers neighbors of a vertex (node), that is, B. Pre order traversal outgoing edges (links) of the vertex's predecessor in the search, before any outgoing edges of the C. In order traversal vertex D. Post order traversal depth first search: Any search algorithm that E. More than one of these considers outgoing edges (links of children ) of a vertex (node) before any of the vertex's (node) siblings , that is, outgoing edges of the vertex's predecessor in the search. Extremes are searched first. CS314 Binary Trees 20
Breadth First Breadth First Search of Tree A level order traversal of a tree could be C used as a breadth first search Search all nodes in a level before going A G X Z down to the next level O W Q P U K B Z L M R CS314 Binary Trees 21 CS314 Binary Trees 22 Breadth First Search Breadth First Search search level 0 first search level 1 C C Find Node with B Find Node with B A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 23 CS314 Binary Trees 24
Breadth First Search Breadth First Search search level 1 search level 1 C C Find Node with B Find Node with B A G X A G X Z Z O O W Q P U W Q P U K B Z K B Z L L M M R R CS314 Binary Trees 25 CS314 Binary Trees 26 Breadth First Search Breadth First Search search level 1 Find Node with B search level 1 next C C Find Node with B A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 27 CS314 Binary Trees 28
Breadth First Search Breadth First Search Find Node with B Find Node with B search level 2 next search level 2 next C C A G X A G X Z Z O O W Q P U W Q P U K B Z K B Z L L M M R R CS314 Binary Trees 29 CS314 Binary Trees 30 Breadth First Search Breadth First Search Find Node with B search level 2 next Find Node with B search level 2 next C C A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 31 CS314 Binary Trees 32
Breadth First Search Breadth First Search Find Node with B Find Node with B search level 2 next search level 3 next C C A G X A G X Z Z O O W Q P U W Q P U K B Z K B Z L L M M R R CS314 Binary Trees 33 CS314 Binary Trees 34 Breadth First Search Breadth First Search Find Node with B search level 3 next Find Node with B search level 3 next C C A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 35 CS314 Binary Trees 36
BFS - DFS Depth First Search of Tree Breadth first search typically implemented C Find B with a Queue Depth first search typically implemented with A G X Z a stack, implicit with recursion or iteratively with an explicit stack O W Q P U which technique do I use? depends on the problem K B Z L M R CS314 Binary Trees 37 CS314 Binary Trees 38 Depth First Search of Tree Depth First Search of Tree C C Find B Find B A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 39 CS314 Binary Trees 40
Depth First Search of Tree Depth First Search of Tree C C Find B Find B A G X A G X Z Z O O W Q P U W Q P U K B Z K B Z L L M M R R CS314 Binary Trees 41 CS314 Binary Trees 42 Depth First Search of Tree Depth First Search of Tree C C Find B Find B A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 43 CS314 Binary Trees 44
Depth First Search of Tree Depth First Search of Tree C C Find B Find B A G X A G X Z Z O O W Q P U W Q P U K B Z K B Z L L M M R R CS314 Binary Trees 45 CS314 Binary Trees 46 Depth First Search of Tree Depth First Search of Tree C C Find B Find B A G X Z A G X Z O O W Q P U W Q P U K B Z K B Z L L M R M R CS314 Binary Trees 47 CS314 Binary Trees 48
Recommend
More recommend