Upload
lynton
View
33
Download
0
Embed Size (px)
DESCRIPTION
CompSci 105 SS 2005 Principles of Computer Science. Lecture 22: Efficiency of BST Operations. Lecturer: Santokh Singh. Inorder Traversal. void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); }. - PowerPoint PPT Presentation
Citation preview
1CompSci 105 SS 2005
Principles of Computer Science
Lecture 22: Efficiency of BST Operations
Lecturer: Santokh Singh
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
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
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.
5
Treesort
Please refer to the prescribed Textbook, pages 478-479, now.
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
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
8
ADT Table
UnsortedArray
SortedArray
UnsortedLinked List
SortedLinked List
BinarySearch Tree
ADT Table
Program that
uses a table
Textbook, p. 504-517
9
BST Efficiency
M
B Q
J UO
Textbook, p. 474-478
10
BST Efficiency
M
B Q
J UO
U
Q
O
M
J
B
Textbook, p. 474-478
11
Average Case?
M
B Q
J UO
U
Q
O
M
J
B
Textbook, p. 474-478
12
Full and Complete Trees
M
Q
UO
G
JB
Textbook, p. 427
A full tree
13
Full and Complete Trees
M
Q
UO
G
JB
M
QG
B
Textbook, p. 427
A full tree A complete tree
14
Minimising Tree Height
Textbook, p. 480-481
M
B Q
OJA
15
ADT Priority Queue
To do:
1. Study for 105 Exam
2. Play
3. Eat
4. Sleep
Textbook, p. 517
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
17
ADT Table
UnsortedArray
SortedArray
UnsortedLinked List
SortedLinked List
BinarySearch Tree
ADTPriorityQueue
Program that uses a priority
queue
18
Heap
Like a binary search tree, but
• Always keep it a complete tree
• Don’t completely sort data
Textbook, p. 520
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
20
M
Q
U
G
JB
BST heap
BST’s vs Heaps
U
JQ
MB G