Title: Tutorial 7: Binary Search Tree
1Tutorial 7 Binary Search Tree
- Short review on binary tree
- Tree traversals
- Binary Search Tree (BST)
- Properties
- FindKey
- FindMin/FindMax
- Insert/Delete
- For sorting?
2Short 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 childleaf nodes
- Left complete tree if
- All levels are full except last level
- Last level filled from left to right
3Tree traversals
- To visit each node of the tree
- Recursively visiting left and right sub-trees
- Pre-order (NLR)
- Current Node, Left sub-tree, Right sub-tree
- In-order (LNR)
- Left, Node, Right
- Post-order (LRN)
- Left, Right, Node
4Tree traversalsExamples
- Pre-order (NLR)
- 9,4,2,3,8,6,7
- Always print the node first
- In-order (LNR)
- 2,4,3,9,6,8,7
- Print whenever return from left
- Post-order (LRN)
- 2,3,4,6,7,8,9
- Print whenever return from right
5Tree traversalsQuestion
- Question Draw the binary tree whose tree
traversal sequences are A, B, D, C, E in
Preorder and B, D, A, C, E in Inorder. - 1st element of preorder A root!
- Note given the root, inorder (LNR) has the left
and right partitions B,D,A,C,E
A
B
D
E
C
6Tree traversals
- Preorder A, B, D, C, E and Inorder B, D, A, C,
E - Next level on the left A-gtB-gtD or A-gtD-gtB?
- Preorder (NLR), (i.e. A-gtLeft) give B first, so
the levels on the left is A-gtB-gtD - D is left or right child of B?
- Inorder (LNR), (i.e. Left-gtB-gtright) gives
empty-gtB-gtD. So, D on the right
A
B
D
D
A
B
E
C
D
7Tree traversals
- Preorder A, B, D, C, E and Inorder B, D, A, C,
E - Next level on the right A-gtC-gtE or A-gtE-gtC?
- Preorder (NLR), (i.e. A-gtright) give C first, so
A-gtC-gtE - E is left or right child of C?
- Inorder (LNR), (i.e. Left-gtC-gtright) gives
empty-gtC-gtE. So, E on the right
A
B
C
D
E
E
A
B
C
D
E
8Binary Search TreeProperties
- A Binary Tree (nodes at most two children)
- Left sub-tree lt Node lt Right sub-tree
- Which one(s) is/are BST(s)?
(1)
(2)
(3)
9Binary Search Tree
- Which one(s) is/are BST(s)?
(1)
(2)
(3)
10Binary Search Tree
- Which one(s) is/are BST(s)?
(3)
(1)
(2)
11Binary Search TreeSummary
- BST Left lt Node lt Right
- Heap Only requiring Parent gt Children
- WHOLE left (or right) sub-tree ltX (or gtX)
- All items in the sub-tree has to be ltX (or gtX)
- NOT necessarily a Complete tree!
- Worse case can be a linked list!
- Same collection of elements can have different
BSTs
12Tree depth/height
- 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 root height of tree
13Binary search treeFind key
- To look for a node with a certain Key
- Left lt Node lt Right
- Recursively do
- Compare(key,node)
- Keyltnode
- Go left
- Keygtnode
- Go right
14Binary search treeFind key
15Binary search treeFind the key 3
- node 6, Key lt 6
- Go left, node 2, Key gt 2
- Go right, node 4, Key lt 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 complexityO(height)
lt
gt
lt
16Binary search treeFind key
- search_binary_tree(node, key)
- if node is null
- return None // not found
- if key lt node.key
- return search_binary_tree(node.left, key)
- else if key gt node.key
- return search_binary_tree(node.right, key)
- else
- return node.value // are you sure this is ?
17Binary search treeFindMin and Find Max
- Left lt Node lt 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
18Binary search treeFindMin and Find Max
- find_min(node)
- if node is null
- return null // what is this?
- if node.left is null
- return node
- else
- return find_min(node.left)
- Really need recursion? Write the same function by
iteration.
19Binary search treeFindMin and Find Max
- find_max(node)
- if node is not null
- while node.right is not null do
- node node.right
- return node
- Iterative version looks simpler
20Binary search treeInsert
- 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.
21Binary search treeInsert the key 5
- node 6, Key lt 6
- Go left, node 2, Key gt 2
- Go right, node 4, Key gt 4
- Go right, NULL, insert 5
- O(1) at each level
- Again, O(height)
lt
gt
5
22Binary search treeInsert key
- insert(node, key)
- if node is null
- node new Node(key)
- else if key lt node.key insert(node.left, key)
- else if key gt node.key insert(node.right, key)
- else node.count // duplicate
- Question Can we do iterative insert?
23Binary search treeDelete
- To delete a key from a BST
- Proceed as if you want to find the key
- If found the node n, delete it!
- Is it really that simple?
- I wish it was
- Step two is a bit harder three cases
24Binary search treeDelete the key 1
- node 6, Key lt 6
- Go left, node 2, Key lt 2
- Go left, node 1, Key 1
- Case one no child
- Delete it!
lt
lt
25Binary search treeDelete the key 8
- node 6, Key gt 6
- Go right, node 8, Key 8
- Case two one child
- My parent bypass me, point to my child instead
- Really no violation?
- Left lt X lt Right
gt
26Binary search treeDelete the key 2
- node 6, Key lt 6
- Go left, node 2, Key 2
- Case two two child
- Replace node 2 with Minnode (m) of right
sub-tree - Recursively delete m
- Why choose this way?
lt
27Binary search treeDelete the key 2, before and
after
28Binary search treeDelete key
- delete(node, key)
- if node is null return // nothing to delete
- if key lt node.key delete(node.left, key)
- else if key gt node.key delete(node.right, key)
- else if both node.left and node.right are not
null - node.key find_min(node.right).key
- delete(node.right, node.key)
- else // no child or one child case
- oldNode node
- if node.left is not null nodenode.left
- else nodenode.right
- delete oldNode
29Binary search treeDelete 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)?
30Time complexity
- All operations discussed takes O(height) time
- Average height O( log N ) (WHY?)
- Textbook pages 133-134
- Worse case (linked list) height ? O(N)