CompSci 105 SS 2005 Principles of Computer Science
description
Transcript of CompSci 105 SS 2005 Principles of Computer Science
![Page 1: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/1.jpg)
1CompSci 105 SS 2005
Principles of Computer Science
Lecture 22: Efficiency of BST Operations
Lecturer: Santokh Singh
![Page 2: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/2.jpg)
2
Inorder Traversalvoid printTree( TreeNode root)
if ( root != null )
printTree( root.getLeft () );
println(root.getRootItem());
printTree( root.getRight() );
}
Textbook, p. 434
M
B Q
J UO
![Page 3: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/3.jpg)
3
Deleting the root
M M
B
J
M
Q
UO
M
B Q
UOJ
root root root root
No children
No rightchild
No leftchild
TwochildrenTextbook, p. 462
![Page 4: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/4.jpg)
4
Exercise
• Draw the BST that results from inserting the values D, C, G, B, E, F in order. Now draw the result of deleting the root.
![Page 5: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/5.jpg)
5
Treesort
Please refer to the prescribed Textbook, pages 478-479, now.
![Page 6: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/6.jpg)
6
ADT Table
City Country Population
Auckland New Zealand
1,300,000
London United Kingdom
9,400,000
Shanghai China 13,053,000
Textbook, p. 498-499
![Page 7: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/7.jpg)
7
ADT Table Operations
void create ()
int length()
void insert( item)
void delete( key )
item find(key)
City Country Population
Auckland New Zealand
1,300,000
London United Kingdom
9,400,000
Shanghai China 13,053,000
Textbook, p. 498-499
![Page 8: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/8.jpg)
8
ADT Table
UnsortedArray
SortedArray
UnsortedLinked List
SortedLinked List
BinarySearch Tree
ADT Table
Program that
uses a table
Textbook, p. 504-517
![Page 9: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/9.jpg)
9
BST Efficiency
M
B Q
J UO
Textbook, p. 474-478
![Page 10: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/10.jpg)
10
BST Efficiency
M
B Q
J UO
U
Q
O
M
J
B
Textbook, p. 474-478
![Page 11: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/11.jpg)
11
Average Case?
M
B Q
J UO
U
Q
O
M
J
B
Textbook, p. 474-478
![Page 12: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/12.jpg)
12
Full and Complete Trees
M
Q
UO
G
JB
Textbook, p. 427
A full tree
![Page 13: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/13.jpg)
13
Full and Complete Trees
M
Q
UO
G
JB
M
QG
B
Textbook, p. 427
A full tree A complete tree
![Page 14: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/14.jpg)
14
Minimising Tree Height
Textbook, p. 480-481
M
B Q
OJA
![Page 15: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/15.jpg)
15
ADT Priority Queue
To do:
1. Study for 105 Exam
2. Play
3. Eat
4. Sleep
Textbook, p. 517
![Page 16: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/16.jpg)
16
Priority Queue Operations
void create ()
boolean isEmpty()
voidinsert( item)
item delete()
To do:
1. Study for 105 Exam
2. Play
3. Eat
4. Sleep
Textbook, p. 518
![Page 17: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/17.jpg)
17
ADT Table
UnsortedArray
SortedArray
UnsortedLinked List
SortedLinked List
BinarySearch Tree
ADTPriorityQueue
Program that uses a priority
queue
![Page 18: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/18.jpg)
18
Heap
Like a binary search tree, but
• Always keep it a complete tree
• Don’t completely sort data
Textbook, p. 520
![Page 19: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/19.jpg)
19
Heap
Like a binary search tree, but
• Always keep it a complete tree
• Don’t completely sort data … just make sure that each node’s key is bigger than its children’s keys.
Textbook, p. 520
![Page 20: CompSci 105 SS 2005 Principles of Computer Science](https://reader035.fdocuments.in/reader035/viewer/2022081603/568139d2550346895da18534/html5/thumbnails/20.jpg)
20
M
Q
U
G
JB
BST heap
BST’s vs Heaps
U
JQ
MB G