Upload
jack-stephenson
View
20
Download
2
Embed Size (px)
DESCRIPTION
Binary Search Trees. Speed of List Search. O(n) when implemented as a linked-list O(lg n) possible if implemented as an array. Binary Search Tree. 54. 21. 72. 5. 30. 60. 84. 10. 25. 35. 79. 86. 44. 93. Binary Search Tree. - PowerPoint PPT Presentation
Citation preview
Binary Search Trees
Speed of List Search
• O(n) when implemented as a linked-list
• O(lg n) possible if implemented as an array
Binary Search Tree
54
30
93
25 7910
5 60
86
44
72
35
84
21
Binary Search Tree
Tree T is a binary search tree made up of n elements: x0 x1 x2 x3 … xn-1
Functions:createEmptyTree() returns a newly created empty binary treedelete(T, p) removes the node pointed to by p from the tree Tinsert(T, p) returns T with the node pointed to by p added in
the proper location search(T, key) returns a pointer to the node in T that has a key that matches key returns null if the node is not found traverse(T) prints the contents of T in orderisEmptyTree(T) returns true if T is empty and false if it is not
Homework 4
• Describe how to implement a binary search tree using a set of nodes (this tree will have no number of element limit).
• Do the six BST functions.• Can you determine how to implement a
binary search tree using an array (assume it will never have more than 100 elements)?
• Consider the six BST functions.
Binary Search Tree
54
30
93
25 7910
5 60
86
44
72
35
84
21
Binary Search Tree – Linked List
null
createEmptyTree()
t
null
isEmptyTree(t)
return t == null
search(t, key)
search(t, key)
if t == null return null
else if key == t.key
return t
else if key < t.key
return search(t.left, key)
else
return search(t.right, key)
search(t, key)
null
t52
35
20 48
40
45
10
15
30
80
65
58
60
70
75
insert(t, p)
insert(t, p)
if t == null
t = p
else
insert2(t, p)
return t
insert2(t, p)
insert2(t, p) if p.key < t.key if t.left == null
t.left = p else
insert2(t.left, p) else if t.right == null
t.right = p else
insert2(t.right, p)
traverse(t)
traverse(t)
if t != null
traverse(t.left)
print t.data
traverse(t.right)
delete(t, p) -- leaf
t
52
35
null
p
null
delete(t, p) -- leaf
t
52
35
null
p
null
t.left = nullnull
delete(t, p) – single parent
t
52
35
20 null
p
delete(t, p) – single parent
t
52
35
20 null
p
t.left = p.left
delete(t, p)t
52
35
20
p
41
46
delete(t, p)t
52
35
20
p
41
46
successor
successor(t, p)
null
t50
36
20 48
40
45
10
15
30
80
65
58
60
70
7539
delete(t, p)t
52
35
20
p
41
46
successor
delete(t, p)t
52
35
20
p
41
46
successor
delete(p, successor)
delete(t, p)t
52
35
20
p
41
46
successor
t.left = successor
delete(t, p)t
52
35
20
p
41
46
successor
successor.left = p.leftsuccessor.right = p.right
delete(t, p)
null
t50
36
20 48
40
45
10
15
30
80
65
58
60
70
7539
Binary Search Tree -- Array
10 112 3 4 5 6 7 8 9 10
2 * i + 1 is the left child2 * i + 2 is the right child
Binary Search Tree -- Array
10 112 3 4 5 6 7 8 9 10
2 * i + 1 is the left child2 * i + 2 is the right child
Binary Search Tree -- Array
10 112 3 4 5 6 7 8 9 10
2 * i + 1 is the left child2 * i + 2 is the right child
Binary Search Tree -- Array
• Advantages– fast– can access the parent from a child
• Disadvantages– fixed size
Speed of Binary Search Trees
In the worst case a BST is just a linked list.t
52
3520
48
8065
null
O(n)
How can we guaranteeO(lg n)
O(lg n) Search Tree
Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the worst case.
Functions:createEmptyTree() returns a newly created empty binary treedelete(T, p) removes the node pointed to by p from the tree Tinsert(T, p) returns T with the node pointed to by p added in the proper location search(T, key) returns a pointer to the node in T that has a key that matches key returns null if the node is not found traverse(T) prints the contents of T in orderisEmptyTree(T) returns true if T is empty and false if it is not
Homework 5
• Describe how to implement a search tree that has a worst time search, insert, and delete time of no more than O(lg n). This tree should have no number of element limit.
• Do the six Search Tree functions.• Discuss how you would implement this if there
was a maximum to the number of elements.