Unbalanced Binary Search Trees - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Unbalanced Binary Search Trees

Description:

The access time is O(h), where h is the height of the tree. When a binary search tree (BST) is balanced with n nodes, h = O(log n) ... – PowerPoint PPT presentation

Number of Views:168
Avg rating:3.0/5.0
Slides: 16
Provided by: toshi155
Category:

less

Transcript and Presenter's Notes

Title: Unbalanced Binary Search Trees


1
Unbalanced Binary Search Trees
A major advantage of using the tree structure is
to be able to access data very efficiently.
The access time is O(h), where h is the height of
the tree.
When a binary search tree (BST) is balanced with
n nodes, h O(log n).
So the access time is O(log n), much better than
O(n) provided by a linear data structure such as
an array or a linked list.
But a binary search tree may degenerate into a
linear structure If not balanced.
1
Start with an empty tree, insert 1, 2, 3, 4, 5
2
3
Use balanced search trees!
4
5
2
B-Trees
Every B-tree depends on a number MIN and follows
six rules
1. ? MIN entries stored at each node (except the
root).
The root can have one or zero entry.
2. ? 2 MIN entries at each node.
3. Entries in each node sorted in a (partially
filled) array.
4. children at each node entries at that
node 1
Pointers to children are also stored in an array.
5. Entry i at a node gt all entries in child node
i Entry i at a node lt all entries in child
node i1
6. All leaves at the same depth.
3
An Example
Let MIN 2.
200
56
109
Is this a B-tree for MIN 3?
4
Class B-tree
template lttypename record, int MINgt struct
B_node int count // number of records
in the node record data2MIN1 // the
node may store 2MIN1 entries temporarily
// during
insertion B_nodeltrecord, MINgt
children2MIN1 // at most 2MIN1
children B_node() // default constructor
template lttypename record, int MINgt class
B_tree public // add public
methods private B_nodeltrecord, MINgt
root
B_treeltint, 2gt sample_tree
5
Search in B-Tree
Search for 10
Index i ? 1
Subtree 0
1
2
4
12
Index j ? 0
5
10
16
18
20
25
Return pairltB_nodeltrecord, MINgt , intgt.
6
Insertion
First search for the correct location and then
insert into a leaf.
Let MIN 2
Start with an empty B-tree
Insert 14, 20, 17, 12
Now, insert 16
Condition 2 violated!
(overflow)
7
Insertion (contd)
Insert 9, 18, 25
Now, insert 23
Overflow again!
8
Contd
16
20
9
Now insert 3, 24, 31, 7, 45
Next, insert 6, 8, 2
Overflow again!
9
Finish
Still overflow!
16
3
10
Storage
During insertion, rules 2 4 may be violated by
a surplus of 1
2. ? 2 MIN entries at each node.
4. children at each node entries at that
node 1
In implementation, at each node
11
Deletion
Depends on where the entry is stored.
Case 1 removal of a leaf (MIN 2)
Remove 9
Easy!
12
Deletion (contd)
Now, remove 12.
Underflow!
3
18
(rule 1 violated)
But, a neighboring sibling has an extra entry
(18).
Redistribute move the extra entry up to the
parent and move the common parent value
(16) down to the underflow node.
13
Contd
Next, remove 42.
Underflow again!
No neighboring sibling has an extra entry for
redistribution.
Concatenate Join the underflow node (75) with
one of its neighbors and bring the
common parent value (30) down into the joined
node.
underflow
Propagate the process upward until no more
underflow.
If all the way to the root, then the last
concatenated node becomes root.
14
More on Deletion
Case 2 removal of a non-leaf (MIN 2)
Remove 75.
at a leaf
15
Contd
55
30
80

66
underflow
underflow
Write a Comment
User Comments (0)
About PowerShow.com