Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Design and Analysis of Algorithms

Binary Search Tree


Dr. Lê Nguyên Khôi
The VNU University of Engineering and Technology
Contents

 Binary Search Tree (BST)


 Randomly built BST
 Balanced BST
 Red-Black tree
 AVL tree
 Treap

Design and Analysis of Algorithms 1


Definition
 Binary search tree:
 Binary tree
 Keysat internal node
 Empty leaves

 Satisfy the property:

 in the left subtree of


 in the right subtree of 6

2 9

1 4 8

Design and Analysis of Algorithms 2


Main Operations

 Queries: do not alter the structure


 Search (SEARCH)
 Find min (MINIMUM)
 Find max (MAXIMUM)
 Find predecessor (PREDECESSOR)
 Find successor (SUCCESSOR)

 Modifications: do alter the structure


 Insertion (INSERT)
 Deletion (DELETE)

Design and Analysis of Algorithms 3


Properties
 in left subtree of , in right subtree of

 In-order traversal of BST, visit keys in an


ascending order
 Usage
 Dictionary
 Heap
 Search tree
 Binary tree
 not binary search tree
 Priorit queue

Design and Analysis of Algorithms 4


Complexity

 is the height of the tree


 is the size of the tree (no. of internal nodes)
 Complexity , for main operations
 Complete BST
 One–chain BST of nodes

6 6

2 9 9

1 4 8 10 8

Design and Analysis of Algorithms 5


Queries – Search
Tree-Search( , )
1 if or
2 return
3 if
4 return Tree-Search( , )
5 else return Tree-Search( , )
6
:
2 9
:
1 4 8
:

Design and Analysis of Algorithms 6


Queries – Search
Iterative-Tree-Search( , )
1 while and
2 if
3
4 else
5 return
6
:
2 9
:
1 4 8
:

Design and Analysis of Algorithms 7


Queries – Find Min/Max
Tree-Minimum( )
1 while
2
3 return 6

Minimum: 2 9

1 4 8
Tree-Maximum( )
1 while
2
3 return
Maximum:

Design and Analysis of Algorithms 8


Queries – Find Predecessor
Tree-Predecessor( )
1 if
2 return Tree-Maximum( )
3
4 while and 6
5
2 9
6
1 4 8
7 return

Predecessor: :
Predecessor: :

Design and Analysis of Algorithms 9


Queries – Find Successor
Tree-Succesor( )
1 if
2 return Tree-Minimum( )
3
4 while and 6
5
2 9
6
1 4 8
7 return

Successor: :
Successor: :

Design and Analysis of Algorithms 10


Modification – Insertion
Tree-Insert( , )
1
2
6
3 while
4 2 9
5 if
1 4 8
6
7 else 7
8
9 if
10
11 elseif
12
13 else

Design and Analysis of Algorithms 11


Modification – Deletion
 Delete node from BST , 3 cases
 does not have any child
 delete

 has exactly 1 child (either left or right)


 Replace by this child
 has both left and right child
 Replace by either the maximum of its left subtree
or the minimum of its right subtree (node )
 delete

 Note: by deleting node the height of the tree


should be reduced
Design and Analysis of Algorithms 12
BST Sort

for to do
Tree-Insert
Perform an in-order tree walk to
6
Example
2 9

Tree-walk time 1 4 8

how long does it 7


take to build the BST

Design and Analysis of Algorithms 13


Analysis of BST Sort
 BST sort performs the same comparisons as
quicksort, but in a different order!
6

2 9

1 4 8

 The expected time to build the tree is


asymptotically the same as the running time of
quicksort

Design and Analysis of Algorithms 14


Node Depth

The depth of a node is the number of comparisons


made during Tree-Insert. Assuming all input
permutations are equally likely, we have:

Average node depth

Design and Analysis of Algorithms 15


Randomly Built BST
 Complexity of main operations
 Build BST with minimum height ( )
 Build BST:
 Insert keys into BST (start with an empty tree)
 At each node (balance left and right subtree)
 Choose with key for node
 Similarproblem as choosing the pivot element for
the partition procedure in Quick Sort
 Choose random key to insert
 Average height

Design and Analysis of Algorithms 16


Other
Tree-Insert( , )
1
 How does Tree-Insert 2
handle duplicate keys 3 while
4
 Case 1: insert randomly 5 if
into left/right subtree 6
7 else
 Case 2: use to 8
choose the subtree 9 if
10
 Case 3: use linked list 11 elseif
12
13 else

Design and Analysis of Algorithms 17


Balanced Binary Search Tree

 Minimum height BST allows quick operations


 Balanced BST allows operations in
 Balance property could be violated by
modification operations
 Modify the tree structure by rotation
 Types of balanced BST
 Red-Black tree
 AVL tree
 Treap

Design and Analysis of Algorithms 18


Red-Black Tree
 Each node needs an extra one-bit color field
 Properties:
 Every node is either red or black
 The root and leaves ( ) are black
 If a node is red, then its parent is black
 All simple paths from any node to a
descendant leaf have the same number of
black nodes
 No path is twice longer than other paths
 The height (with nodes)

Design and Analysis of Algorithms 19


Red-Black Tree – Example

Design and Analysis of Algorithms 20


AVL Tree

 Georgy Adelson-Velsky & Evgenil Landis


proposed in 1962
 The heights of the left and right subtrees
of any node differ by at most 1
 Complexity of for operations in
both worst and average cased
 Balance the tree by rotation

Design and Analysis of Algorithms 21


Treap

 Randomly built BST tend to be balanced


 However, if data is received one at a
time, keys cannot be randomly chosen
 Treap could provide a solution
 A combination of BST and min-heap
 Key satisfies BST properties
 Priority satisfies min-heap properties

Design and Analysis of Algorithms 22


Balanced BST – Rotation
 More detail section 13.2 p.312

Design and Analysis of Algorithms 23


Exercises
 12.1-1 p.289
 heights 2, 3, 4, 5, 6
 12.2-2 p.293
 Recursive version of tree min/max
 12.3-1 p.299
 Recursive version of tree insertion

Design and Analysis of Algorithms 24

You might also like