You are on page 1of 2

BST:-

A BST is a binary tree T with the following


conditions:
a) Key of every node in the right sub-tree of T is
greater then the Key at t root.
b) Key of every node in the left sub-tree of T is
less then the Key at root .
c) All Keys are distinct.

BST * search (T key, BST * t){


if (empty_t(t))
return NULL;
else
if (key==tÆinfo)
return t;
else if (key < tÆinfo)
return (search (key,tÆleft));
else
return (search (key, tÆright));
}

• Average case complexity of search, insertion and


deletion operations is O(log 2 n), where n is the no of nodes in the tree.
• The height of a BST depends on the sequence of
insertion and deletion of keys.
• An extreme case:
Draw a BST for the following sequence of insertions:
1, 2, 3, 4, 5, 6, 7
The worst case complexity 0of search, insertion and deletion are O(n).
Remedy: Balanced tree.

AVL:-

AVL tree is a BST where at each node (including the root node) the left sub
sub-tree and the right sub-tree do not differ in height by more than one.
|h L -h R | <= 1

Balance Factor (BF) of a node is the


difference between the heights of its left and
right sub-trees.
BF = h L -h R
BF = 1 left high
BF = -1 right high
BF = 0 equal high

Right Rotation:-
avltree * rotate-right (avltree * t) {
avltree * temp;
temp= t Æ left;
t Æ left = temp Æ right;
temp Æ right = t;
return temp;
}
• Height of a height-balanced (AVL) Tree is guaranteed to be
O(log n) n being the no. of nodes.
• The insertion/deletion step takes at most O(log n) time.
• Each rebalancing step, i.e., rotation (possibly double rotation )
and updation of BF takes a constant amount of time.
• The rebalancing may go up to the root. Thus, there can be at
most O(log n) rebalancing steps.
• Thus the overall complexity of insertion/deletion is O(log n).

2,3 B-tree:-

1.All leaf nodes are at the same level.


2.All non leaf nodes are either binary or ternary.
3.Binary nodes are like BST nodes with single key k.
4.Ternary nodes have two keys k1 and k2 and
three children pointers p1,p2,p3.
5.All keys < k1 reside in sub-tree pointed to by
p1. . All keys y > k2 reside in sub-tree p pointed to
by p3. . Others reside in the sub-tree pointed to
by p2. All keys are distinct .

typedef struct nt {
T k1, k2;
int binary;
struct nt * p1,* p2,* p3;
}23treenode;

If h is the height of a 2-3 tree having n nodes,


then 2^h -1 <= n <= 3^h -1
Thus, log 3 (n+1) <= h <= log 2 (n+1)

B-tree:-

1.Each node ( except possibly the root node ) contains at most d records and at
least d/2records.
2.The root node can have at most d records and as few as one record.
3.An internal node containing k records (1 <= k <= d)
with key values k 1 to k k , have pointers to k+1 subintervals of keys stored in
sub-trees.
4.A leaf node has empty sub-tree sub tree below it. All leaf nodes are at the same
level.

typedef struct btnode {


int k;
T datalist[maxkeyno];
struct btnode * ptr[maxkeynode+1];
}
Height of a B-Tree of order d containing n nodes is given by
 log g  d/2 + 1 ( (n+1)) >= h >= log g d + 1 ( (n+1)
2-3 tree is a B-Tree of order 2

You might also like