Download presentation
Presentation is loading. Please wait.
Published byHope Nelson Modified over 9 years ago
1
Binary Tree
2
Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
3
Some Terminologies Child and Parent Every node except the root has one parent. A node can have zero or more children. Leaves Leaves are nodes with no children. 1,3,7,8 Sibling nodes with the same parent. 2 and 8; 1 and 4; 3 and 7
4
Some Terminologies Path A sequence of edges. Length of a path: number of edges on the path Depth of a node = no. of edges from root Root is also a node, depth of root = ? Height of a node = length of longest path to a leaf Height of leaves = ? Height of 2 = ? Height of root = height of tree
5
Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
6
Short review on binary tree At most two children for each node A root node at the top (or the tree is empty) Nodes with no child=leaf nodes Left complete tree if: All levels are full except last level Last level filled from left to right
7
Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
8
Tree traversals To visit each node of the tree Recursively visiting left and right sub-trees Pre-order (NLR) Node Current Node, Left sub-tree, Right sub-tree In-order (LNR) Node Left, Node, Right Post-order (LRN) Node Left, Right, Node
9
Tree traversals Examples Pre-order (NLR) 9,4,2,3,8,6,7 Always print the node first 9 4 23 8 67
10
Tree traversals Examples In-order (LNR) 2,4,3,9,6,8,7 Print whenever return from left 9 48 2367 48 9
11
Tree traversals Examples Post-order (LRN) 2,3,4,6,7,8,9 Print whenever return from right 9 48 2367 4 9 8
12
Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
13
Binary Search Tree Properties A Binary Tree (nodes at most two children) Node Left sub-tree < Node < Right sub-tree Which one(s) is/are BST(s)? (1)(2) (3)
14
Binary Search Tree Which one(s) is/are BST(s)? (2) (1) (3)
15
Binary Search Tree Which one(s) is/are BST(s)? (1)(2) (3)
16
Binary Search Tree Summary LeftNodeRight BST: Left < Node < Right Heap: Only requiring Parent > Children WHOLEsub-tree WHOLE left (or right) sub-tree X) All items in the sub-tree has to be X) NOT necessarily a Complete tree! Worse case can be a linked list! Same collection of elements can have different BSTs
17
Binary search tree Find key To look for a node with a certain Key Left < Node < Right Recursively do: 1. Compare(key,node) 2. Key<node: Go left 3. Key>node: Go right
18
Binary search tree Find the key 8 node 6, Key > 6 Go right, node 8, Key == 8 >
19
Binary search tree Find the key 3 node 6, Key < 6 Go left, node 2, Key > 2 Go right, node 4, Key < 4 Go left, node 3, Key == 3 O(1) at each level Find a key O(depth) if found O(height) if not found Time complexity=O(height) < > <
20
Binary search tree FindMin and FindMax Left < Node < Right Min = Left most node Recursively go left Until no more left child Max = Right most node Recursively go right Until no more right child
21
Binary search tree FindMin and FindMax
22
Binary search tree Insert To insert a key into BST, similar to Find Key Proceed as if you want to find the key If found, duplicate key do nothing; or update a counter; or insert to a list; or … Otherwise insert X at the last spot.
23
Binary search tree Insert the key 5 node 6, Key < 6 Go left, node 2, Key > 2 Go right, node 4, Key > 4 Go right, NULL, insert 5 O(1) at each level Again, O(height) < > 5
24
Binary search tree Delete To delete a key from a BST Proceed as if you want to find the key If found the node n, delete it! that simple Is it really that simple? I wish it was Step two is a bit harder: three cases
25
Binary search tree Delete the key 1 node 6, Key < 6 Go left, node 2, Key < 2 Go left, node 1, Key == 1 Case one: no child Delete it! < <
26
Binary search tree Delete the key 8 node 6, Key > 6 Go right, node 8, Key == 8 Case two: one child My parent bypass me, point to my child instead Really no violation? Left < X < Right >
27
Binary search tree Delete the key 2 node 6, Key < 6 Go left, node 2, Key == 2 Case two: two child Replace node 2 with Min node (m) of right sub-tree Recursively delete m Why choose this way? <
28
Binary search tree Delete the key 2, before and after
29
Binary search tree Delete Summary Case 1: node n is a leaf (no child): delete it! Case 2: node n has only one child: Parent bypass n, point to n.child and delete n Case 3: node n has two child: Replace n with smallest node (m) of the right sub-tree Recursively delete m O(height)? Smallest node=left-most node Either has right child; OR No child So, delete m can only be case 1 or 2, the simple cases
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.