Lecture 21: Binary Search Tree
-
Upload
andrew-james -
Category
Documents
-
view
25 -
download
0
description
Transcript of Lecture 21: Binary Search Tree
1
Lecture 21: Binary Search Tree
CompSci 105 SS 2006
Principles of Computer Science
2
As with a Binary Tree, except
Root node has a special data element called a key
Nodes in left subtree have keys < the root’s key
Nodes in right subtree have keys > the root’s key.
Binary Search Trees (BSTs)
K
>K<K
3
Searching
M
B Q
J UO
4
Add as a leaf
M
B Q
J UO
5• Draw the BST that results from inserting the values 4, 3, 2, 7, 5, 6 in order.
• Draw as many different BST’s shapes as possible with nodes A, B, and C.
B
CA
C
B
A
C
A
B
A
B
C
A
C
B
4
3
2
7
5
6
6
Inorder Traversalvoid printTree( TreeNode root)
if ( root != null )
printTree( root.getLeft () );
println(root.getRootItem());
printTree( root.getRight() );
}M
B Q
J UO
7
Inorder Traversalvoid printTree( TreeNode root)
if ( root != null )
printTree( root.getLeft () );
println(root.getRootItem());
printTree( root.getRight() );
}
B
CA
C
B
A
C
A
B
A
B
C
A
C
B
8
Preorder Traversalvoid printTree( TreeNode root)
if ( root != null )
println( root.getRootItem());
printTree( root.getLeft () );
printTree( root.getRight() );
}M
B Q
J UO
9
Postorder Traversalvoid printTree( TreeNode root)
if ( root != null )
printTree( root.getLeft () );printTree( root.getRight() );
println(root.getRootItem());
}M
B Q
J UO
10
Exercise
• For each of the trees below, what is the preorder traversal? What is the postorder traversal?
B
CA
C
B
A
C
A
B
A
B
C
A
C
B
11
Level Order Traversal
• Difficult
• print nodes as if reading tree from a book (left to right, top to bottom)
• Use a queue to keep track...”homework”M
B Q
J UO
12
BST Delete
M
B Q
J UO
13
Deleting the root
M M
B
J
M
Q
UO
M
B Q
UOJ
root root root root
No children
No rightchild
No leftchild
Twochildren
14
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.
15
BST Efficiency
M
B Q
J UO
16
BST Efficiency
M
B Q
J UO
U
Q
O
M
J
B
17
Average Case?
M
B Q
J UO
U
Q
O
M
J
B
18
Full and Complete Trees
M
Q
UO
G
JB
A full tree
19
Full and Complete Trees
M
Q
UO
G
JB
M
QG
B
A full tree A complete tree
20
Minimising Tree Height
M
B Q
OJA
21
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
22
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
23
ADT Table
UnsortedArray
SortedArray
UnsortedLinked List
SortedLinked List
BinarySearch Tree
ADTPriorityQueue
Program that uses a priority
queue
24
M
Q
U
G
JB
BST heap
BST’s vs Heaps
U
JQ
MB G
25
Heap
• A complete binary tree
• Each node’s key is bigger than its children’s keys.
Textbook, p. 520
U
JQ
MB G
Maxheap vs. minheap
26
Which are maxheaps?M
Q
UO
G
JB
8
5
4
7
21
M
J
C
G
BA
J
EC
DA
27
M
Q
U
G
JB
BST heap
BST’s vs Heaps
U
JQ
MB G