0% found this document useful (0 votes)
421 views

Binary Search Tree

This document discusses binary search trees (BSTs). It explains that BSTs can support dynamic set operations like search, minimum, maximum, predecessor, successor, insert, and delete in O(h) time where h is the height of the tree. The tree maintains the binary search tree property where all left descendants of a node are less than or equal to the node's key, and all right descendants are greater than or equal. Insertion into a BST involves finding the correct place in the tree for the new node while maintaining this property.

Uploaded by

Shiva Thavani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
421 views

Binary Search Tree

This document discusses binary search trees (BSTs). It explains that BSTs can support dynamic set operations like search, minimum, maximum, predecessor, successor, insert, and delete in O(h) time where h is the height of the tree. The tree maintains the binary search tree property where all left descendants of a node are less than or equal to the node's key, and all right descendants are greater than or equal. Insertion into a BST involves finding the correct place in the tree for the new node while maintaining this property.

Uploaded by

Shiva Thavani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Binary Search Trees

Binary Trees
• Recursive definition
1. An empty tree is a binary tree
2. A node with two child subtrees is a binary tree
3. Only what you get from 1 by a finite number of
applications of 2 is a binary tree. 56

26 200

Is this a binary tree? 18 28 190 213

12 24 27

September 25, 2019 UCS406: Data Structures and Algorithms Intro 2


Binary Search Trees
• View today as data structures that can support
dynamic set operations.
– Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
• Can be used to build
– Dictionaries.
– Priority Queues.
• Basic operations take time proportional to the
height of the tree – O(h).

September 25, 2019 UCS406: Data Structures and Algorithms Intro 3


BST – Representation
• Represented by a linked data structure of
nodes.
• root(T) points to the root of tree T.
• Each node contains fields:
– key
– lef – pointer to left child: root of left subtree.
– right – pointer to right child : root of right subtree.
– p – pointer to parent. p[root[T]] = NIL (optional).

September 25, 2019 UCS406: Data Structures and Algorithms Intro 4


Binary Search Tree Property
• Stored keys must satisfy
the binary search tree 56
property.
–  y in left subtree of x, 26 200
then key[y]  key[x].
–  y in right subtree of x,
18 28 190 213
then key[y]  key[x].

12 24 27

September 25, 2019 UCS406: Data Structures and Algorithms Intro 5


Inorder Traversal
The binary-search-tree property allows the keys of a binary search
tree to be printed, in (monotonically increasing) order, recursively.

Inorder-Tree-Walk
Inorder-Tree-Walk(x) (x)
1. ififxxNIL
1. NIL 56

2.
2. thenthenInorder-Tree-Walk(lef[p])
Inorder-Tree-Walk(lef[p])
26 200

3.
3. print
printkey[x]
key[x] 18 28 190 213

4.
4. Inorder-Tree-Walk(right[p])
Inorder-Tree-Walk(right[p]) 12 24 27

September 25, 2019 UCS406: Data Structures and Algorithms Intro 6


Querying a Binary Search Tree
• All dynamic-set search operations can be supported in
O(h) time.
• h = (lg n) for a balanced binary tree (and for an
average tree built by adding nodes in random order.)
• h = (n) for an unbalanced tree that resembles a
linear chain of n nodes in the worst case.

September 25, 2019 UCS406: Data Structures and Algorithms Intro 7


Tree Search
Tree-Search(x,
Tree-Search(x,k) k)
1.
1. ififxx==NIL
NILor
orkk==key[x]
key[x]
2.
2. then thenreturn
returnxx
3.
3. ififkk<<key[x]
key[x]
4.
4. then thenreturn
returnTree-Search(lef[x],
Tree-Search(lef[x],k)
k) 56
5.
5. else elsereturn
returnTree-Search(right[x],
Tree-Search(right[x],k)
k) 26 200

18 28 190 213
Running time: O(h)

12 24 27

September 25, 2019 UCS406: Data Structures and Algorithms Intro 8


Iterative Tree Search
Iterative-Tree-Search(x,
Iterative-Tree-Search(x,k) k) 56
1. whilexxNIL
1. while andkkkey[x]
NILand key[x] 26 200
2.
2. do doififkk<<key[x]
key[x]
3.
3. thenxx
then lef[x]
lef[x] 18 28 190 213

4.
4. elsexx
else right[x]
right[x]
5.
5. return
returnxx
12 24 27

The iterative tree search is more efficient on most computers.


The recursive tree search is more straightforward.

September 25, 2019 UCS406: Data Structures and Algorithms Intro 9


Finding Min & Max
The binary-search-tree property guarantees that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.

Tree-Minimum(x)
Tree-Minimum(x) Tree-Maximum(x)
Tree-Maximum(x)
1.
1. while lef[x]NIL
whilelef[x] NIL 1.
1. while right[x]NIL
whileright[x] NIL
2.
2. do doxxlef[x]
lef[x] 2.
2. doxx
do right[x]
right[x]
3.
3. return
returnxx 3.
3. return
returnxx

Q: How long do they take?

September 25, 2019 UCS406: Data Structures and Algorithms Intro 10


Predecessor and Successor
• Successor of node x is the node y such that key[y] is the
smallest key greater than key[x].
• The successor of the largest key is NIL.
• Search consists of two cases.
– If node x has a non-empty right subtree, then x’s successor is
the minimum in the right subtree of x.
– If node x has an empty right subtree, then:
• As long as we move to the left up the tree (move up through right
children), we are visiting smaller keys.
• x’s successor y is the node that x is the predecessor of (x is the
maximum in y’s left subtree).
• In other words, x’s successor y, is the lowest ancestor of x whose left
child is also an ancestor of x.
September 25, 2019 UCS406: Data Structures and Algorithms Intro 11
Pseudo-code for Successor
Tree-Successor(x)
Tree-Successor(x)
•• right[x]NIL
ififright[x] NIL
2.
2. then
thenreturn
returnTree-Minimum(right[x])
Tree-Minimum(right[x])
3. yy
3. p[x]
p[x]
4. whileyyNIL
4. while NILand
andxx==right[y]
right[y]
5. doxx
5. do yy 56
6.
6. yyp[y]
p[y]
26 200
7.
7. return
returnyy
18 28 190 213
Code for predecessor is symmetric.

Running time: O(h) 12 24 27

September 25, 2019 UCS406: Data Structures and Algorithms Intro 12


BST Insertion – Pseudocode
• Change the dynamic set Tree-Insert(T,
Tree-Insert(T,z)z)
represented by a BST. 1. yy
1. NIL
NIL
• Ensure the binary- 2. xx
2. root[T]
root[T]
search-tree property 3. whilexxNIL
3. while NIL
holds after change. 4.
4. doyy
do xx
• Insertion is easier than 5.
5. ififkey[z]
key[z]<<key[x]
key[x]
deletion. 56
6.
6. thenxx
then lef[x]
lef[x]
7.
7. elsexx
else right[x]
right[x]
26 200 8. p[z]
8. p[z] yy
9.
9. ififyy==NILNIL
18 28 190 213
10.
10. then root[t]
thenroot[t] zz
11.
11. elseelseififkey[z]
key[z]<<key[y]
key[y]
12.
12. then lef[y]
then lef[y] zz
13. else right[y]
elseright[y] zz
12 24 27
13.
September 25, 2019 UCS406: Data Structures and Algorithms Intro 13
Analysis of Insertion
Tree-Insert(T,
Tree-Insert(T,z)z)
• Initialization: O(1) 1. yy
1. NIL NIL
2. xx
2. root[T]
root[T]
• While loop in lines 3-7 3. whilexxNIL
3. while NIL
searches for place to 4.
4. doyy
do xx
insert z, maintaining 5.
5. ififkey[z]
key[z]<<key[x]
key[x]
parent y. 6.
6. thenxx
then left[x]
left[x]
This takes O(h) time. 7.
7. elsexx
else right[x]
right[x]
• Lines 8-13 insert the 8. p[z]
8. p[z] yy
value: O(1) 9.
9. ififyy==NILNIL
10.
10. then root[t]
thenroot[t] zz
 TOTAL: O(h) time to 11.
11. else
elseififkey[z]
key[z]<<key[y]
key[y]
insert a node. 12.
12. then left[y]
then left[y] zz
13.
13. else right[y]
elseright[y] zz
September 25, 2019 UCS406: Data Structures and Algorithms Intro 14
Exercise: Sorting Using BSTs
Sort (A)
for i  1 to n
do tree-insert(A[i])
inorder-tree-walk(root)

– What are the worst case and best case running


times?
– In practice, how would this compare to other
sorting algorithms?

September 25, 2019 UCS406: Data Structures and Algorithms Intro 15


Tree-Delete (T, x)
if x has no children  case 0
then remove x
if x has one child  case 1
then make p[x] point to child
if x has two children (subtrees)  case 2
then swap x with its successor
perform case 0 or case 1 to delete it

 TOTAL: O(h) time to delete a node


September 25, 2019 UCS406: Data Structures and Algorithms Intro 16
Deletion – Pseudocode
Tree-Delete(T,
Tree-Delete(T,z)z)
/*
/*Determine
Determinewhich whichnode
nodetotosplice
spliceout:
out:either
eitherzzor
orz’s
z’ssuccessor.
successor.
*/*/
•• ififlef[z]
lef[z]==NIL
NILororright[z]
right[z]==NIL
NIL
•• thenyy
then zz
•• elseyy
else Tree-Successor[z]
Tree-Successor[z]
/*
/*Set
Setxxto toaanon-NIL
non-NILchild
childof
ofx,x,or
orto
toNIL
NILififyyhas
hasno
nochildren.
children.*/*/
4. lef[y]NIL
4. ifif lef[y] NIL
5.
5. thenxx
then lef[y]
lef[y]
6.
6. elsexx
else right[y]
right[y]
/*
/*yyisisremoved
removedfromfromthethetree
treebybymanipulating
manipulatingpointers
pointersofof p[y]
p[y]
and
andxx*/ */
7. ififxxNIL
7. NIL
8.
8. then p[x]
thenp[x] p[y]
p[y]
/*
/*Continued
Continuedon onnext
nextslide
slide*/
*/
September 25, 2019 UCS406: Data Structures and Algorithms Intro 17
Deletion – Pseudocode
Tree-Delete(T,
Tree-Delete(T,z)z)(Contd.
(Contd.from
fromprevious
previousslide)
slide)
9.
9. ififp[y]
p[y]==NILNIL
10.
10. then root[T]
thenroot[T] xx
11.
11. elseifif yy
else lef[p[i]]
lef[p[i]]
12.
12. then lef[p[y]]
thenlef[p[y]] xx
13.
13. else right[p[y]]
elseright[p[y]] xx
/*
/*IfIfz’s
z’ssuccessor
successorwas wasspliced
splicedout,
out,copy
copyits
itsdata
datainto
intozz*/
*/
14. ififyyzz
14.
15.
15. then key[z]
then key[z] key[y]
key[y]
16.
16. copy
copyy’s
y’s data
datainto
intoz.z.
17.
17. return
returnyy

September 25, 2019 UCS406: Data Structures and Algorithms Intro 18


Binary Search Trees
• View today as data structures that can support
dynamic set operations.
– Search, Minimum, Maximum, Predecessor,
Successor, Insert, and Delete.
• Can be used to build
– Dictionaries.
– Priority Queues.
• Basic operations take time proportional to the
height of the tree – O(h).

September 25, 2019 UCS406: Data Structures and Algorithms Intro 19


END

September 25, 2019 UCS406: Data Structures and Algorithms Intro 20

You might also like