0% found this document useful (0 votes)
12 views40 pages

Data Structure and Algorithms: University of Engineering and Technology Lahore Pakistan

Uploaded by

talhacanyon98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views40 pages

Data Structure and Algorithms: University of Engineering and Technology Lahore Pakistan

Uploaded by

talhacanyon98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Advanced DS Data Structure and Algorithms

Fall 2024

Binary Search Tree

eduko.spikotech.com

University of Engineering and Technology


Lahore Pakistan
Binary Search Tree
Course Reference: CS161 by Stanford

Data Structures and Algorithms


Some data structures
for storing objects like 5 (aka, nodes with keys)
• (Sorted) arrays:

1 2 3 4 5 7 8

• Linked lists:

HEAD 3 2 1 8 5 7 4

• Some basic operations:


• INSERT, DELETE, SEARCH
Data Structures and Algorithms
Sorted Arrays 1 2 3 4 5 7 8

• O(n) INSERT/DELETE:
• First, find the relevant element (we’ll see how below), and
then move a bunch elements in the array:

1 2 3 4 54.5 7 8
eg, insert 4.5
• O(log(n)) SEARCH:

1 2 3 4 5 7 8
eg, Binary search to see if 3 is in A.
Data Structures and Algorithms
(Not necessarily sorted)
Linked lists
7 5 3 4 1 2 8
• O(1) INSERT:

eg, insert 6
HEAD 7 5 3 4 1 2 8

6
• O(n) SEARCH/DELETE:
HEAD 7 5 3 4 1 2 8

eg, search for 1 (and then you Data


could delete
Structures it by manipulating pointers).
and Algorithms
Motivation for Binary Search Trees

(Balanced)
Binary Search
Sorted Arrays Linked Lists
Trees

Search O(log(n)) O(n) O(log(n))

Delete O(n) O(n) O(log(n))

Insert O(n) O(1) O(log(n))


Data Structures and Algorithms
For today all keys are distinct.

Binary tree terminology This is a node.


It has a key (7).
Each node has two children. This node is
the root
The left child of 3 is 2
5
The right child of 3 is 4

The parent of 3 is 5 3 7
2 is a descendant of 5
Each node has a pointer to its 2 4 8
left child, right child, and parent.
Both children of 1 are NIL.
(I won’t usually draw them). 1
These nodes
The height of this tree is 3.
(Max number of edges from the NIL NIL are leaves.
Data Structures and Algorithms
root to a leaf).
From your pre-lecture exercise…

Binary Search Trees


• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
• Example of building a binary search tree:

3 4
5

8 7
1
2
Data Structures and Algorithms
Binary Search Trees
• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
• Example of building a binary search tree:

3 4
5

8 7
1
2
Data Structures and Algorithms
Binary Search Trees
• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
• Example of building a binary search tree:

3 5
1
4 7
2
8
Data Structures and Algorithms
Binary Search Trees
• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
• Example of building a binary search tree:

3 7
1
2 4 8
Data Structures and Algorithms
Binary Search Trees
• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.
• Example of building a binary search tree:
Q: Is this the only
5 binary search tree I
could possibly build
with these values?

3 7 A: No. I made
choices about
which nodes to
choose when. Any
2 4 8 choices would
have been fine.

1 Data Structures and Algorithms


Aside: this should look familiar
kinda like QuickSort

3 4
5

8 7
1
2
Data Structures and Algorithms
Which of these is a BST?
1 minute Think-Pair-Share
Binary Search Trees
• A BST is a binary tree so that:
• Every LEFT descendant of a node has key less than that node.
• Every RIGHT descendant of a node has key larger than that node.

5 5

3 7 3 7

2 4 8 2 4 8
NOT a Binary
1 Binary Search Tree
Data Structures and Algorithms
1 Search Tree
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4

Data Structures and Algorithms


Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4

Data Structures and Algorithms


Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL

Data Structures and Algorithms


Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL

Data Structures and Algorithms


Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL

2
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL

2
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL

2
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4 5
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
2 3 4 5 7
Data Structures and Algorithms
Aside: In-Order Traversal of BSTs
• Output all the elements in sorted order!

• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
• Runs in time O(n). 2 3 4 5 7 Sorted!
Data Structures and Algorithms
Tree Minimum and Tree Maximum

3 7

2 4 8

1
Data Structures and Algorithms
Tree Successor
5

3 7

2 4 8

Data Structures and Algorithms


Tree Predecessor

3 7

2 4 8

1
Data Structures and Algorithms
Back to the goal

Fast SEARCH/INSERT/DELETE
Can we do these?

Data Structures and Algorithms


SEARCH in a Binary Search Tree
definition by example

EXAMPLE: Search for 4.


5
EXAMPLE: Search for 4.5
• It turns out it will be convenient
3 7 to return 4 in this case
• (that is, return the last node
before we went off the tree)

2 4 8
Write pseudocode
1 (or actual code) to
implement this!
How long does this take?
O(length of longestDatapath)
Structures=and
O(height)
Algorithms Ollie the over-achieving ostrich
INSERT in a Binary Search Tree
EXAMPLE: Insert 4.5
5
• INSERT(key):
• x = SEARCH(key)
3 7 • Insert a new node with
desired key at x…

2 4 8
x= 4

1 4.5

Data Structures and Algorithms


INSERT in a Binary Search Tree
EXAMPLE: Insert 4.5
5
• INSERT(key):
• x = SEARCH(key)
3 7 • if key > x.key:
• Make a new node with the
correct key, and put it as the

2 4 8 right child of x.
• if key < x.key:
• Make a new node with the
x= 4 correct key, and put it as the
1 4.5 left child of x.
• if x.key == key:
• return
Data Structures and Algorithms
DELETE in a Binary Search Tree
EXAMPLE: Delete 2
5 • DELETE(key):
• x = SEARCH(key)
3 7 • if x.key == key:
• ….delete x….

2 4 8
x= 2

1
Data Structures and Algorithms
DELETE in a Binary Search Tree
several cases (by example)
say we want to delete 3

5 5 5 5
3 3 2
Case 1: if 3 is a leaf,
just delete it.
2
This triangle
is a cartoon
for a subtree
Write pseudocode for all of these!

Case 2: if 3 has just one child,


move that up.
Siggi the Studious Stork Data Structures and Algorithms
DELETE in a Binary Search Tree
ctd.
Case 3: if 3 has two children,
replace 3 with it’s immediate successor. • Does this maintain the BST
(aka, next biggest thing after 3) property?
• Yes.
• How do we find the
5 5 immediate successor?
• SEARCH for 3 in the subtree
3 3.1 under 3.right
• How do we remove it when
we find it?
2 4 2 4 • If [3.1] has 0 or 1 children,
do one of the previous cases.
• What if [3.1] has two
children?
3.1 • It doesn’t.
Data Structures and Algorithms
How long do these operations take?
• SEARCH is the big one.
• Everything else just calls SEARCH and then does some
small O(1)-time operation.

5 Time = O(height of tree)

3 7
Trees have depth
O(log(n)). Done! Wait a
2 4 6 8 second…

How long does search take?


1 minute think; 1 minute pair+share

Lucky the Plucky the


Data Structures and Algorithms
lackadaisical lemur. Pedantic Penguin
Search might take time O(n).
2 • This is a valid binary search tree.
3 • The version with n nodes has
depth n, not O(log(n)).
4
5
6
7
8
Data Structures and Algorithms
How often is “every so
often” in the worst case?

What to do?
It’s actually pretty often!

Ollie the over-achieving ostrich

• Goal: Fast SEARCH/INSERT/DELETE


• All these things take time O(height)
• And the height might be big!!! 

• Idea 0:
• Keep track of how deep the tree is getting.
• If it gets too tall, re-do everything from scratch.
• At least Ω(n) every so often….

Data Structures and Algorithms

You might also like