Professional Documents
Culture Documents
Like a binary search, an interpolation search repeatedly subdivides the list to locate an item
Searching for the the value 11, linear search examines 17, 23, 5, and 11 -> Found Searching for the the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3 -> Not Found
Then iterate!
2.
If the middle element is the correct value, done. Otherwise, go to step 1, using only the half of the array that may contain the correct value.
Continue steps 1 and 2 until either the value is found or there are no more elements to examine.
Searching for the value 11, binary search examines 11 and stops. Found. Searching for the value 7, binary search examines 11,3,5,and stops. Not Found.
Disadvantage
Requires that array elements be sorted.
Values are fairly evenly distributed, interpolation can be used to eliminate more values at each step.
If searching is still not finish, continue searching with new l and r. Stop searching when Found or x<a[l] or x>a[r].
1: l=1, r=20 -> m=1+(30-1)*(20-1)/(70-1) = 9 a[9]=19<30=x -> l=10 2: l=10, r=20 -> m=10+(30-21)*(20-10)/(7021) = 12 a[12]=32>30=x -> r = 11 3: l=10, r=11 -> m=10+(30-24)*(11-10)/(2421) = 12 m=12>11=r: Not Found
Search Operation
Operation Insert
Worst Case
Performance
Depend on the shape of the tree Best Case:
Perfectly balanced tree, log N nodes from root to leave
Worst Case:
N nodes in a search path
Average Case:
1.39 log N comparisons for N keys
Balanced Tree
Tree structures support various basic dynamic set operations in time proportional to the height
of the tree e.g.: Search, Predecessor, Successor, Minimum, Maximum, Insert, and Delete
Ideally, a tree will be balanced and the height will be log n where n is the number of nodes in the tree To ensure that the height of the tree is as small as possible and therefore provide the best running time
Balanced BST
BST Worst case O(N)
Need to be balanced
Approach:
rebalance the BST explicitly Recursive and linear time However, insertion cost quadratic
Frequently rebalancing
Red-Black Tree
A red-black tree is a binary search tree with the following properties:
edges are colored red or black no two consecutive red edges on any root-leaf path same number of black edges on any root-leaf path (= black height of the tree) edges connecting leaves are black
Red-Black Tree
2.
3. 4.
Perform a standard search to find the leaf where the key should be added Replace the leaf with an internal node with the new key Color the incoming edge of the new node red Add two new leaves, and color their incoming edges black
Restructuring
Case 2: Incoming edge of p is red, and its sibling is black
Double Rotation
What if the new node is between its parent and grandparent in the inorder sequence? We must perform a double rotation (which is no more difficult than a single one)
Bottom-Up Rebalancing
Case 3: Incoming edge of p is red and its sibling is also red
We call this a promotion Note how the black depth remains unchanged for all of the descendants of g This process will continue upward beyond g if necessary: rename g as n and repeat.
Summary of Insertion
If two red edges are present, we do either
a restructuring (with a simple or double rotation) and stop, or a promotion and continue
A restructuring takes constant time and is performed at most once. It reorganizes an offbalanced section of the tree. Promotions may continue up the tree and are executed O(log N) times. The time complexity of an insertion is O(logN).