Data Structure and Algorithms: University of Engineering and Technology Lahore Pakistan
Data Structure and Algorithms: University of Engineering and Technology Lahore Pakistan
Fall 2024
eduko.spikotech.com
1 2 3 4 5 7 8
• Linked lists:
HEAD 3 2 1 8 5 7 4
• 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
(Balanced)
Binary Search
Sorted Arrays Linked Lists
Trees
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…
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.
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
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
• inOrderTraversal(x):
• if x!= NIL: 5
• inOrderTraversal( x.left )
• print( x.key ) 3 7
• inOrderTraversal( x.right )
2 4
NIL NIL
• 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
3 7
2 4 8
1
Data Structures and Algorithms
Back to the goal
Fast SEARCH/INSERT/DELETE
Can we do these?
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
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!
3 7
Trees have depth
O(log(n)). Done! Wait a
2 4 6 8 second…
What to do?
It’s actually pretty often!
• 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….