Das 5
Das 5
2
Binary Search Tree (BST)
• Each node has at most two children
• Implementation:
double-linked list
3
The Node Structure
• Each node in the BST contains:
• x.key – key value
• x.left – pointer to the left child
p
• x.right – pointer to the right child left right
key
• x.p – pointer to the parent node
4
What is a binary search tree?
• Binary-search property:
• Let 𝑥 be a node in a binary search tree.
• If 𝑦 is a node in the left subtree of 𝑥, then: y.key x.key.
• If 𝑦 is a node in the right subtree of 𝑥, then: x.key y.key.
5
Binary search Tree
• For any node 𝑥, the keys in the left subtree of 𝑥 are at most 𝑥. 𝑘𝑒𝑦, and the
keys in the right subtree of 𝑥 are at least 𝑥. 𝑘𝑒𝑦.
• Different binary search trees can represent the same set of values.
• The worst-case running time for most search-tree operations is proportional
to the height of the tree.
a) A binary search tree on
6 nodes with height 2.
b) A less efficient binary
search tree with height 4
that contains the same
keys.
6
Tree Walks
7
Tree Walks
• Keys in the BST can be printed using “Tree Walks”:
• Inorder tree walk
• Preorder tree walk
• Postorder tree walk
8
Inorder tree walk
• Keys of each node printed between keys in the left and right
subtree
• Divide-and-conquer algorithm!
• Prints elements in monotonically increasing order
• Running time Θ(𝑛)
InorderTreeWalk(x)
01 if x NIL then
02 InorderTreeWalk(x.left)
03 print x.key
04 InorderTreeWalk(x.right)
9
Inorder tree walk
• Inorder tree walk (ITW) can be thought of as a projection of
the BST nodes onto a one dimensional interval
10
Other Tree Walks
• Preorder tree walk:
• A preorder tree walk processes each node before processing its
children
• Postorder tree walk:
• A postorder tree walk processes each node after processing its
children
11
Traversing a Binary Search Tree: Summary
• Inorder tree walk:
• Root is printed between the values of its left and right subtrees:
left, root, right → keys are printed in sorted order
• Preorder tree walk: root printed first: → root, left, right
• Postorder tree walk: root printed last: → left, right, root
12
Traversing a Binary Search Tree: Example
• Inorder: 2 3 4 5 7 9
• Preorder: 5 3 2 4 7 9 5
• Postorder: 2 4 3 9 7 5
3 7
2 4 9
13
BST Search
14
Searching a BST
• To find an element with key 𝑘 in a tree 𝑇:
• compare 𝑘 with 𝑇. 𝑘𝑒𝑦
• if 𝑘 < 𝑇. 𝑘𝑒𝑦, search for 𝑘 in 𝑇. 𝑙𝑒𝑓𝑡
• otherwise, search for 𝑘 in 𝑇. 𝑟𝑖𝑔ℎ𝑡
15
Pseudocode for BST Search
• Recursive version – divide-and-conquer algorithm
TREE-SEARCH(x,k)
01 if x == NIL or k == x.key
02 return x
03 if k < x.key
04 return TREE-SEARCH(x.left, k)
05 else
06 return TREE-SEARCH(x.right, k)
16
Pseudocode for BST Search
• Iterative version:
ITERATIVE-TREE-SEARCH(x,k)
01 while x ≠ NIL and k ≠ x.key
02 if k < x.key
03 x = x.left
04 else x = x.right
05 return x
17
Search Examples
• Search(T, 11)
18
Search Examples (2)
• Search(T, 6)
19
Analysis of Search
• Running time on tree of height h is O(h)
• After the insertion of n keys, the worst-case running time of
searching is O(n)
20
BST Minimum
• Find the minimum key in a tree rooted at 𝑥
TreeMinimum(x)
01 while x.left NIL
02 x = x.left
03 return x
21
BST Maximum
• Find the maximum key in a tree rooted at 𝑥
TreeMaximum(x)
01 while x.right NIL
02 x = x.right
03 return x
22
BST Successor and Predecessor
23
Successor
• Given 𝑥, find the node with the smallest key greater than
𝑥. 𝑘𝑒𝑦()
• We can distinguish two cases, depending on the right subtree
of 𝑥
• Case 1:
• right subtree of 𝑥 is non-empty
• successor is the leftmost node in the
right subtree
• this can be done by returning
TreeMinimum(𝑥.right())
24
Successor
• Case 2:
• the right subtree of 𝑥 is empty
• successor is the lowest ancestor of 𝑥 whose left child is also an
ancestor of 𝑥
25
Successor Pseudocode
• For a tree of height h, the running time is O(h)
TreeSuccessor(x)
01 if x.right() NIL Successor is the lowest
02 return TreeMinimum(x.right) ancestor of 𝑥 whose left
03 y = x.p child is also an ancestor of 𝑥
04 while y NIL and x == y.right
05 x = y
06 y = y.p
07 return y
26
Predecessor
• TREE-PREDECESSOR is symmetric to TREE-SUCCESSOR.
27
Example
• Find the successor of the node with key value 15.
15
• Answer: Key value 17
• Find the successor of the node with key value 6.
• Answer: Key value 7 6 18
• Find the successor of the node with key value 4.
• Answer: Key value 6 3 7 17 20
• Find the successor of the node with key value 2.
2 4 13
• Answer: Key value 3
• Find the predecessor of the node with key value 6. 9
• Answer: Key value 4
29
BST Insertion and Deletion
30
BST Insertion
• The basic idea is similar to searching:
• take an element 𝑧 (whose left and right children are NIL) and insert
it into 𝑇
• find place in 𝑇 where z belongs (as if searching for z.key()),
• and, add 𝑧 there
• The running on a tree of height ℎ is 𝑂(ℎ)
31
BST Insertion Pseudo Code
TreeInsert(T,z)
01 y = NIL
02 x = T.root
03 while x NIL
04 y = x
05 if z.key() < x.key()
06 x = x.left()
07 else x = x.right()
08 z.p = y
09 if y == NIL
10 T.root = z // tree T was empty
11 else if z.key < y.key
12 y.left = z
13 else y.right = z
32
BST Insertion Example (1)
• Insert 8
33
BST Insertion Example (2)
• Inserting an item with key 13 into a binary search tree
• Lightly shaded nodes indicate the simple path from the root
down to the position where the item is inserted. The dashed line
indicates the link in the tree that is added to insert the item.
34
BST Insertion: Worst Case
• In what kind of sequence should the insertions be made to
produce a BST of height n?
35
Deletion
• Delete node x from a tree T
• We can distinguish three cases:
• x has no children
• x has one child
• x has two children
36
Deletion Case 1
• If x has no children – just remove x
37
Deletion Case 1
• If z has no children – just remove z
38
Deletion Case 2
• If x has exactly one child, then to delete x, simply make x.p
point to that child
39
Deletion Case 2
• If z has exactly one child, then to delete z, simply make
z.parent point to that child
40
Deletion Case 3
• If x has two children, then to delete it we
have to:
• find its successor (or predecessor) 𝑦
• remove 𝑦 (note that 𝑦 has at most one child)
• replace 𝑥 with 𝑦
• Two possible resultant trees
41
Deletion Case 3
• If z has two children,
then to delete it we
have to:
• find its successor (or
predecessor) y
• remove y (note that y
has at most one child)
• replace z with y
42
Insertion & Deletion Anlysis
• The INSERT and DELETE can be made to run in O(h) time on
a binary search tree of height h.
44
Sorting
45
BST Sorting
• Idea: Use Insert and InorderTreeWalk to sort a list of n elements
• Example. Sort the following
numbers: 5, 10, 7, 1, 3, 1, 8
• Build a binary search tree:
• Call InorderTreeWalk:
1, 1, 3, 5, 7, 8, 10
46