Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Binary Search
Searching for X in Binary Tree L.
Linear search: O(N)
Difference between O(logN) and O(N) is very significant for large N. N = 2 billion,
linear search: 1 billion comparisons, binary search: 32 comparisons.
The 1st thing we need is a pointer to the middle element: 6. What should 6 point to?
There are 2 possible outcomes that involve further search: X < 6 and X > 6.
In the second level, similarly, there are 2 possible outcomes for 6 and 9, respectively:
1
Data Structure – Search Tree and Multiway Tree
2
Data Structure – Search Tree and Multiway Tree
Balanced Trees
3
Data Structure – Search Tree and Multiway Tree
AVL Trees
Search, Insertion, Deletion are guaranteed O(logN) provided height balance can be
maintained in O(logN).
Insertion:
BST insertion
check height-balance, and rebalance if necessary by changing the tree's shape.
4
Data Structure – Search Tree and Multiway Tree
Insert 3
Insert 6
Insert 2
Insert 1
Insert 0
Rotation Algorithm
Pivot: deepest imbalanced node.
Rotator: root of pivot's taller subtree.
5
Data Structure – Search Tree and Multiway Tree
Example
6
Data Structure – Search Tree and Multiway Tree
Step 4
Step 5
Step 6
Example
Insert 3
7
Data Structure – Search Tree and Multiway Tree
Rotate
After Insert 3
Rotate Inside
Main Rotation
8
Data Structure – Search Tree and Multiway Tree
4. The new value was inserted in one of the rotator's subtrees (the taller one).
If in rotator's outside subtree: 1 rotation will suffice.
In in rotator's inside subtree: first rotate that subtree, then perform main
rotation.
Example
Insert 2
Rotate inside
9
Data Structure – Search Tree and Multiway Tree
Main rotation
Deletion
Given a value X and an AVL tree T, delete the node containing X and rebalance the
result.
Deleting X upsets A.
After rotating B up, E is upset.
After rotating F up, D's left subtree is shorter. D is upset.
After rotating M up (D down), we get a 5-5 balance at D. So now it is 1 shorter
and its parent is out of balance.
This can propagate all the way to the top of the tree.
10
Data Structure – Search Tree and Multiway Tree
In fact, each may contain up to M - 1 values. A node with k values must have k + 1
subtrees.
In a node, values are stored in ascending order:
V1 < V2 < ... < Vk
The subtrees are placed between adjacent values: each value has a left and right
subtree.
V(i)'s right subtree = V(i+1)'s left subtree.
All the values in V(i)'s left subtree are < V(i).
All the values in V(i)'s right subtree are > V(i).
11
Data Structure – Search Tree and Multiway Tree
B-Trees
A B-tree is a M-way search tree such that:
It is perfectly balanced.
Every node, except perhaps the root, is at least half full (has >= M values).
Example:
Insert 17
13
Data Structure – Search Tree and Multiway Tree
The tree-insertion algorithms we have previously seen add new nodes at the bottom of
the tree, and then have to worry about whether they have created an imbalance. The
B-tree insertion algorithm is just the opposite: it adds nodes at the top. All nodes
become 1 level deeper: the tree remains balanced.
14
Data Structure – Search Tree and Multiway Tree
15
Data Structure – Search Tree and Multiway Tree
Delete 3 from:
Result:
Note: the parent has 1 fewer value. It might underflow! The repair strategy may have
to be applied repeatedly at successive levels.
If the root underflows: it must have originally contained just 1 value, now removed.
if the root was a leaf: now empty
else, value was consumed by case 2: the resulting combined node can be used as
new root.
16
Data Structure – Search Tree and Multiway Tree
Delete 7 from:
Root underflows:
17