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

Binary Search Trees (BST'S)

The document discusses binary search trees (BSTs) and their use for implementing an efficient runway reservation system. A BST allows reservations (requests) to be inserted in O(log n) time by performing a binary search. This is faster than other data structures that may require O(n) time for insertion. The document provides examples of BST operations like search, minimum, maximum, successor and predecessor that can all be performed in O(h) time, where h is the height of the tree. It also discusses implementing BSTs using linked data structures and pointers.

Uploaded by

Ankur Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Binary Search Trees (BST'S)

The document discusses binary search trees (BSTs) and their use for implementing an efficient runway reservation system. A BST allows reservations (requests) to be inserted in O(log n) time by performing a binary search. This is faster than other data structures that may require O(n) time for insertion. The document provides examples of BST operations like search, minimum, maximum, successor and predecessor that can all be performed in O(h) time, where h is the height of the tree. It also discusses implementing BSTs using linked data structures and pointers.

Uploaded by

Ankur Sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Binary Search Trees (BST’s)

- Motivation: Runway Reservation System


Instructor: Prof. Ashwin Ganesan
International School of Engineering (INSOFE)

http://www.insofe.edu.in Data Science Education and Research


Runway reservation system
● A busy airport has only one runway
● Before landing, plane makes a reservation request by specifying landing time t
● R:= set of (accepted) reservation requests for future landings
● Add t to R if no other landings are scheduled within < k minutes of t
● GOAL: Implement InsertWithCheck(t, R) efficiently
○ Want O(log n), where n = |R|

Data Science Education and Research www.insofe.edu.in 2


Example
● Suppose
○ k=3
○ R = {41, 46, 49, 56}
○ Current time is 37
● Then,
○ Request t = 44 - rejected
○ Request t = 53 - accepted, inserted; R = {41, 46, 49, 56, 53}
○ Request t = 52 - rejected
○ Request t = 20 - rejected (already past)

Data Science Education and Research www.insofe.edu.in 3


Implementations / data structures
● Sorted array

¥
○ Can find location to insert in O(log n) using binary search
○ Check takes O(1) time
○ Insert takes O(n)
● Unsorted array
○ Check takes O(n)
○ Insert is O(1)

Data Science Education and Research www.insofe.edu.in 4


Implementations / data structures
● Linked list (sorted)
○ Finding insertion point is O(n)
● Linked list (unsorted)
○ Can insert at beginning in O(1)
○ Check takes O(n)
● Maxheap
○ Check takes O(n)

● If we can do fast insertion into a sorted array, we’ll have


O(log n). Can do this with (balanced) binary search trees
Data Science Education and Research www.insofe.edu.in 5
Binary Search Trees (BST’s)
- Introduction
Instructor: Prof. Ashwin Ganesan
International School of Engineering (INSOFE)

http://www.insofe.edu.in Data Science Education and Research


Linked data structure and pseudocode conventions
● Will represent a BST by a linked data structure
● Each node is an object that contains
○ A key and satellite data
○ Attributes leftchild, rightchild, and parent, corresponding to pointer to left child,
pointer to right child, and pointer to parent, respectively
● If a child or parent is missing, the appropriate pointer contains the value NIL
● Root node is the only node whose parent is NIL

Data Science Education and Research www.insofe.edu.in 7


Implementation in C using pointers
● Can implement BST’s using pointers
○ Here, data is stored as an actual tree (unlike a heap, which is stored in an array
and just visualized as a tree) How L=[10,7,18,15,12,14,5] is stored as
a BST data structure:

struct cell{

}
Data Science Education and Research
int element;
struct cell* leftchild;
struct cell* rightchild;
struct cell* parent;

inorderlistng
: sorted :

www.insofe.edu.in
¥¥ ! £ # ¥
kg
8
Binary search trees (BST’s)
● A binary search tree (BST) is a binary tree which
satisfies the following property (called the BST
property or search property):

For each node x,


○ If y is a node in the left subtree of x,
then y.key ≤ x.key
○ If y is a node in the right subtree of x,
then y.key ≥ x.key

Data Science Education and Research www.insofe.edu.in 9


Example
● Two BST’s for the set {5, 10, 7, 12, 14, 15, 18} are shown below.

÷
Data Science Education and Research www.insofe.edu.in 10
Exercise
● Is the tree below a BST?

320
Id
be
53/20
-

but

Data Science Education and Research www.insofe.edu.in 11


Performance
● Worst-case running time for most search-tree operations is proportional to the height
of the tree, defined as # edges in a longest path from the root to a leaf
● Different binary search trees can represent the same set of values
○ Left figure: a binary search tree on 6 nodes with height 2
○ Right figure: a less efficient binary search tree -
with height 4 that contains the same set of keys

" ,
-
int ht -4
-

'
i

Data Science Education and Research www.insofe.edu.in 12


Inorder walk
● Suppose a set S is stored as a binary search tree data structure
● To print the elements of S in sorted order, just do an in-order walk of the tree!
○ Takes time O(n)
Example: An in order traversal of
the tree below prints 2, 5, 5, 6, 7, 8.

Data Science Education and Research www.insofe.edu.in 13


Binary Search Trees (BST’s)
- Querying a Binary Search Tree
Instructor: Prof. Ashwin Ganesan
International School of Engineering (INSOFE)

http://www.insofe.edu.in Data Science Education and Research


Search operations on a dynamic set S
● Want to support the following queries on a dynamic set S:
○ Search(x,k): given a pointer x to the root node of tree and a key k, return a pointer
to the node containing key k (or NIL if no such node exists)
○ Minimum(x): returns pointer to the minimum element in subtree rooted at x
○ Maximum(x): returns pointer to the maximum element in subtree rooted at x
○ Successor(x): returns pointer to the successor of x in the sorted order determined
by an inorder tree walk
○ Predecessor(x): returns pointer to the predecessor of x in the sorted order
determined by an inorder tree walk
● A binary search tree with height h supports each of the above operations in O(h) time

Data Science Education and Research www.insofe.edu.in 15


Search
● Given a pointer to root x of the tree and a key k,
return a pointer to a node with key k if one exists;
otherwise, return NIL
○ Begin search at root x and trace a path
downward in the tree
○ For each node x, compare x.key with k
● If equal, search terminates Takes
● If k < x.key, search continues in left O(h) time
subtree of x
● If k > x.key, search continues in right
subtree of x

Data Science Education and Research www.insofe.edu.in 16


Example
¥!
← To search for 13 in this tree,
follow the path:
15 → 6 → 7 → 13

Data Science Education and Research www.insofe.edu.in 17


Iterative version
● Can “unroll” the recursion into a while loop.
● Iterative version of search is generally a constant factor faster

Takes
O(h) time

Data Science Education and Research www.insofe.edu.in 18


Minimum
● To find an element in a binary search tree whose key is minimum, keep following left-
child pointers from root until we encounter NIL

Example: The minimum key in tree is 2, which is


found by following left pointers from the root.


E
K

Data Science Education and Research www.insofe.edu.in 19


Maximum
● To find an element in a binary search tree whose key is maximum, keep following
right-child pointers from root until we encounter NIL

Example: The maximum key in tree is 20, which


is found by following right pointers from the root.

Data Science Education and Research www.insofe.edu.in 20


Successor
● The successor of a node x is the node whose key
is listed immediately after x when all keys are
printed in sorted order
● Example: In tree shown below
○ Successor of 15 is 17
○ Successor of 13 is 15
● Structure of a BST allows determining successor
and predecessor of a node without ever
comparing keys

Data Science Education and Research www.insofe.edu.in 21


Pseudocode
● Two cases to consider when determining
successor of node x
○ If right subtree of x is nonempty, find smallest
element in this right subtree
○ If right subtree of x is empty and x has a
successor y, can show y is the lowest
ancestor of x whose left child is also an
ancestor of x

Data Science Education and Research www.insofe.edu.in 22


Summary
● Suppose we implement a dynamic set S using a binary search tree data structure
● Then, we can support each of the the operations Search, Minimum, Maximum,
Successor, and Predecessor in O(h) time on a binary search tree of height h

Can show h -0110gal


-

is achievable

Data Science Education and Research www.insofe.edu.in 23


Binary Search Trees (BST’s)
- Insertion and Deletion
Instructor: Prof. Ashwin Ganesan
International School of Engineering (INSOFE)

http://www.insofe.edu.in Data Science Education and Research


Insertion
● The insert operation causes the dynamic set represented by a BST to change
● Goal: to insert a new node in the binary search tree, but in such a way that the BST
property continues to hold
● Algorithm: To insert a new value v into a binary search tree
○ Search for v in the tree using the search procedure
○ When a NIL pointer is encountered, insert v in that position
● Thus, we insert the new value in the position where it would have been if it
was already present in the tree

Data Science Education and Research www.insofe.edu.in 25


Pseudocode
● Input: a. ☐→ Takes
○ A new node z with key z.key=v, z.left=NIL O(h) time
and z.right=NIL
○ A tree T in which to insert the new node
● Output:
○ Modify tree T such that z is inserted into an
appropriate position in the tree

Data Science Education and Research www.insofe.edu.in 26


Example

● To insert 13 into the tree
z
7
○ Search for 13 in the tree until a NIL pointer
is encountered:
12 → 18 → 15
1-

z.FI#paI+
○ Insert 13 in that position

¥F¥
Data Science Education and Research www.insofe.edu.in 27
Exercise
● Insert the elements
7, 2, 9, 0, 5, 6, 8, 1
into an initially empty BST. Show the final BST.

¥¥
Data Science Education and Research www.insofe.edu.in 28
Degenerate BST’s
● Insert the elements A zigzag tree:
0,1,2,5,6,7,8,9
into an initially empty BST. Show the final BST.

Data Science Education and Research www.insofe.edu.in 29


Balanced BST 's (AHL trees
R

Insert 0.1.2 5,6 7,8 9 ,

¥&@⑥÷¥p¥
, ,


rotation
la balancing
µ
② operation
moot node is

doubly right heavy An Byc


-

in
order :

lie / htlrightsvbtreelhtyeftsubtree.tl
Data Science Education and Research
""

www.insofe.edu.in
A
7¥ }
5 ,

CCRS Exl
balanced
O
'
12 6µg
This ( .

height
.

g
-

node is ,
If every
-016gal )

,
h ,
then
01h
I \

c- i z

( n >
then :
'
t
o I 61^8
10=1-1B£
-

Data Science Education and Research www.insofe.edu.in


Deletion
● To delete a node z from a binary search tree T, consider 3 cases
○ (1) If z has no children, then just remove z from the tree
○ (2) If z has only one child, elevate that child to take z’s position in the tree; for
illustrate, see (a) and (b) in Figure

Data Science Education and Research www.insofe.edu.in 30


Deletion
○ (3) If z has two children,
replace z by its
successor y
● If y is z’s right child,
replace z by y; see
figure (c)
● Otherwise, replace y
by its own right
children, and then
replace z by y; see
figure (d)

Data Science Education and Research www.insofe.edu.in 31


Example
● Starting with the BST shown below, delete the
elements 20, 30 and 50, in that order. Show the 50
BST that results after each delete operation.
50 464-0
/
7-0 ¥41301 /
to
deT¥l20 ) %É
Go

6
! to
4) ✓ delete 1501

4,9° -7°
-
go

Data Science Education and Research www.insofe.edu.in 32


Example
● Starting with the BST shown below, delete the
element 10. Show the resulting BST. 12
- \
12
¥210
/
5 15

elevate 13
2
I
\g /
13
Tg
,
6
Xp \

7-

Data Science Education and Research www.insofe.edu.in 33


Summary
● Suppose we implement a dynamic set S using a BST data structure
● Then, we can support insert and delete operations in O(h) time on a BST of height h

Data Science Education and Research www.insofe.edu.in 34


*Achieving O(log n) worst case


Complexity of above BST operations is O(h) ¥! ,
I :*
Issue: Height h of BST can be linear in n, and so worst-case running time of BST
operations is O(n)
● Goal: worst-case running time O(log n).
● Solution: balanced BST’s
○ Balanced BST’s (eg AVL trees) have height h=O(log n)
○ A node is balanced if height of its left and right subtrees differ by at most 1
○ After each BST-insert operation, rebalance all nodes in upward path to root using
so-called rotation operations
○ Can prove that if all nodes are balanced then h=O(log n)
Data Science Education and Research www.insofe.edu.in 35
*Dictionary operations in O(log n) time
● Recall that ADT’s are the specs, and data structure is how you do it (implementation)
● Table below gives worst-case performance of various ADT operations for three
different data structure; * denotes the operation is not support by the data structure
ADT operations minheap BST Balanced BST (eg AVL trees)

Insert O(log n) O(h) O(log n)


DeleteMin O(log n) O(h) O(log n)
Search *O(n) O(h) O(log n)
Min O(1) O(h) O(log n)
Successor/Pred *O(n) O(h) O(log n)
Max *O(n) O(h) O(log n)
DeleteMax *O(n) O(h) O(log n)

● Summary: can achieve dictionary operations (insert, search and delete) in O(log n)
time using balanced BST’s.
Data Science Education and Research www.insofe.edu.in 36
References and Acknowledgements
● Many of the examples, images and other content in these slides are taken
(often verbatim) from the following sources:
○ Thomas Cormen, Charles Leiserson, Ronald Rivest, and Clifford Stein,
“Introduction to Algorithms, Third Edition”, MIT Press, 2009.

÷÷¥¥

mmds.org
.

www.insofe.edu.in 37

You might also like