Download presentation
Presentation is loading. Please wait.
Published byEdgar Stanley Modified over 6 years ago
1
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 tree delete(T, p) removes the node pointed to by p from the tree T insert(T, x) returns T with x 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 x is not found traverse(T) prints the contents of T in order isEmptyTree(T) returns true if T is empty and false if it is not
2
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.
3
AVL Tree 54 +1 21 72 +1 -1 5 30 60 84 10 25 79 86
4
AVL Tree The five functions are the same.
Except that the tree needs to be rebalanced after insertion or deletion. Keep track of the path used to insert/delete. Balance starting at the parent of the leaf inserted or deleted. Work up to the root.
5
Insertion Case 1
6
Insertion Case 1 -1
7
Insertion Case 2 +1
8
Insertion Case 2
9
Insertion Case 3 +1
10
Insertion Case 3 +2
11
Deletion Case 1 -1
12
Deletion Case 1
13
Deletion Case 2
14
Deletion Case 2 +1
15
Deletion Case 3 +1
16
Deletion Case 3 +2
17
Single Rotation +2 +1
18
Double Rotation +2 -1
19
Single Rotation +2 +1
20
Single Rotation A +2 B +1
21
Single Rotation A B
22
Single Rotation A B +2 +1
23
Single Rotation A B
24
Single Rotation +2 +1
25
Single Rotation +1
26
Single Rotation +1
27
Single Rotation +1
28
Single Rotation +1
29
Single Rotation
30
Double Rotation +2 -1
31
Double Rotation +1
32
Double Rotation1 A +2 C -1 B -1
33
Double Rotation1 A B C +2 -1 -1
34
Double Rotation1 A +2 C -1 B -1
35
Double Rotation1 A +2 C -1 B -1
36
Double Rotation1 A +1 C +1 B -1
37
Double Rotation1 A +1 B C -1 +1
38
Double Rotation1 A +1 B C -1 +1
39
Double Rotation1 A +1 B -1 C +1
40
Double Rotation1 A +2 B +1 C +1
41
Double Rotation1 A B +1 C +1
42
Double Rotation1 A B +1 C +1
43
Double Rotation1 B +1 A C +1
44
Double Rotation1 B A C +1
45
Double Rotation1 B A C +1
46
Double Rotation2 A +2 C -1 B +1
47
Double Rotation2 A +2 C -1 B +1
48
Double Rotation2 A +2 C B +1
49
Double Rotation2 A +2 B C +1
50
Double Rotation2 A +2 B +2 C
51
Double Rotation2 A -1 B +2 C
52
Double Rotation2 A B -1 +2 C
53
Double Rotation2 B A -1 C
54
Double Rotation2 B A C -1
55
Binary Search Tree -- Array
1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child
56
Binary Search Tree -- Array
1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child
57
Binary Search Tree -- Array
1 11 2 3 4 5 6 7 8 9 10 2 * i + 1 is the left child 2 * i + 2 is the right child
58
Binary Search Tree -- Array
Advantages fast can access the parent from a child Disadvantages fixed size standard AVL rotates greater than O(lg n)
59
Priority Queue A Priority Queue Set S is made up of n elements: x0 x1 x2 x3 … xn-1 Functions: createEmptySet() returns a newly created empty priority queue findMin(S) returns the minimum node with respect to ordering insert(S, x) returns S with x added deleteMin(S) returns S with the minimum node removed isEmptySet(S) returns true if S is empty and false if it is not
60
Homework 6 Describe how to implement a priority queue that has a worst case findMin in O(1) time and insert and deleteMin in no more than O(lg n) time. You can assume that n is always less than In other words, there is a max of 127 elements that can be stored in the queue. Do the five Priority Queue functions.
61
Priority Queue -- Lists
Ordered Array Find in constant time. Insert and delete in O(n). Unordered Array Insert in constant time. Find and delete in O(n).
62
Priority Queue -- Lists
Ordered Linked List Find and delete in constant time. Insert in O(n). Unordered Linked List Insert in constant time. Find and delete in O(n).
63
Priority Queue Trees Binary Search Tree AVL Tree
Find can be more than O(lg n) if out of balance. Insert and delete can be more than O(lg n). AVL Tree Find is O(lg n) time. Insert and delete are O(lg n).
64
Priority Queue Trees AVL Tree with pointer to smallest
Find is O(1) time. Insert and delete are O(lg n). Works, but is way too complicated for the task We need a simpler solution
65
Homework 6 Describe how to implement a priority queue that has a worst case findMin in O(1) time and insert and deleteMin in no more than O(lg n) time. You can assume that n is always less than In other words, there is a max of 127 elements that can be stored in the queue. Do the five Priority Queue functions.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.