0% found this document useful (0 votes)
50 views

Binary Search Tree

Binary search trees have the property that all values in the left subtree are smaller than the root node value, and all values in the right subtree are larger. They allow for efficient insertion, deletion, and search operations that take O(log n) time on average for a balanced tree with n nodes. However, standard binary search trees may become unbalanced over many insertions and deletions. AVL trees address this by ensuring that the heights of the left and right subtrees of any node differ by at most one, maintaining an overall tree height of O(log n).

Uploaded by

Aditya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Binary Search Tree

Binary search trees have the property that all values in the left subtree are smaller than the root node value, and all values in the right subtree are larger. They allow for efficient insertion, deletion, and search operations that take O(log n) time on average for a balanced tree with n nodes. However, standard binary search trees may become unbalanced over many insertions and deletions. AVL trees address this by ensuring that the heights of the left and right subtrees of any node differ by at most one, maintaining an overall tree height of O(log n).

Uploaded by

Aditya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Binary Search Trees

 Key property
 Value at node
 Smaller values in left subtree
 Larger values in right subtree
 Example
X
 X>Y
 X<Z

Y Z
Binary Search Trees
 Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10

25
Binary Not a binary
search trees search tree
Binary Tree Implementation

Class Node {
int data; // Could be int, a class, etc
Node *left, *right; // null if empty

void insert ( int data ) { … }


void delete ( int data ) { … }
Node *find ( int data ) { … }

}
Iterative Search of Binary Tree
Node *Find( Node *n, int key) {
while (n != NULL) {
if (n->data == key) // Found it
return n;
if (n->data > key) // In left subtree
n = n->left;
else // In right subtree
n = n->right;
}
return null;
}
Node * n = Find( root, 5);
Recursive Search of Binary Tree

Node *Find( Node *n, int key) {


if (n == NULL) // Not found
return( n );
else if (n->data == key) // Found it
return( n );
else if (n->data > key) // In left subtree
return Find( n->left, key );
else // In right subtree
return Find( n->right, key );
}
Node * n = Find( root, 5);
Example Binary Searches
 Find ( root, 2 )

root
10 5
10 > 2, left 5 > 2, left
5 30 5 > 2, left 2 45 2 = 2, found
2 = 2, found
2 25 45 30

10

25
Example Binary Searches
 Find (root, 25 )

10 5
10 < 25, right 5 < 25, right
5 30 30 > 25, left 2 45 45 > 25, left
25 = 25, found 30 > 25, left
2 25 45 30
10 < 25, right
10 25 = 25, found

25
Types of Binary Trees
 Degenerate – only one child
 Full – always two children
 Balanced – “mostly” two children
 more formal definitions exist, above are intuitive ideas

Degenerate Balanced Full binary


binary tree binary tree tree
Binary Trees Properties
 Degenerate  Balanced
 Height = O(n) for n  Height = O( log(n) )
nodes for n nodes
 Similar to linked list  Useful for searches

Degenerate Balanced
binary tree binary tree
Binary Search Properties

 Time of search
 Proportional to height of tree
 Balanced binary tree
 O( log(n) ) time
 Degenerate tree
 O( n ) time
 Like searching linked list / unsorted array
Binary Search Tree Construction

 How to build & maintain binary trees?


 Insertion
 Deletion
 Maintain key property (invariant)
 Smaller values in left subtree
 Larger values in right subtree
Binary Search Tree – Insertion

 Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left subtree for
Y
4. If X > Y, insert new leaf X as new right subtree
for Y
 Observations
 O( log(n) ) operation for balanced tree
 Insertions may unbalance tree
Example Insertion

 Insert ( 20 )
10 10 < 20, right
30 > 20, left
5 30
25 > 20, left
2 25 45 Insert 20 on left

20
template<class E, class K>
class BSTree public: Binarytree<E>
{
public:
bool search(const K &k, E &e);
BSTree<E, K> &insert(E &e);
BSTree<E,K> &delete(K &k, E &e);
};
template<class E, class K>
BSTree<E,K> &BSTree<E,K>::insert(E &e)
{
BinaryTreeNode<E> *p=root;
BinaryTreeNode<E> *pp=0;
while (p)
{pp=p;
if (e<pdata) p=pleftchild;
else if (e>pdata) p=prightchild;
else throw badinput();
}
BinaryTreeNode<E> *r=new BinaryTreeNode<E> (e)
if (root) {
if (e<ppdata) ppleftchild=r;
else pprightchild=r;}
else root=r;
return *this;
}
Binary Search Tree – Deletion

 Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
 O( log(n) ) operation for balanced tree
 Deletions may unbalance tree
Example Deletion (Leaf)

 Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
Example Deletion (Internal Node)

 Delete ( 10 )
10 5 5

5 30 5 30 2 30

2 25 45 2 25 45 2 25 45

Replacing 10 Replacing 5 Deleting leaf


with largest with largest
value in left value in left
subtree subtree
After Deleting 10
Example Deletion (Internal Node)

 Delete ( 10 )
10 25 25

5 30 5 30 5 30

2 25 45 2 25 45 2 45

Replacing 10 Deleting leaf Resulting tree


with smallest
value in right
subtree
Indexed Binary Search Tree

 Binary search tree.


 Each node has an additional field.
 leftSize = number of nodes in its left subtree+1
Example: Indexed Binary Search
Tree 8
20

5 4
10 40

2 1 2 30
6 15

1 1 1 1
2 18
2 8 25 35

1
7 leftSize values are in red

Also called Rank!


Balanced Search Trees

 Height balanced.
 AVL (Adelson-Velsky and Landis, 1962) trees
 Weight Balanced.
 Degree Balanced.
 2-3 trees
 2-3-4 trees
 red-black trees
 B-trees
AVL Tree

 binary tree
 for every node x, define its balance factor
balance factor of x = height of left subtree of x
– height of right subtree of x
 balance factor of every node x is – 1, 0, or
1
AVL Trees

An empty binary Tree is an AVL Tree. If T is a


non-empty Binary tree with TL and TR as its
left and right subtrees, then T is an AVL tree
iff (1) TL and TR are AVL trees and (2) hL  hR  1
where hL and hR are the heights of TL and TR
Balance Factors
-1

1 1

0 1 -1
0

0
0 0 -1 0

This is an AVL tree.


AVL Search Tree

 Binary Search Tree+AVL


Indexed AVL Search Tree

 Indexed Binary Search Tree+AVL


Height Of An AVL Tree

The height of an AVL tree that has n nodes is


at most 1.44 log2 (n+2).

The height of every n node binary tree is at


least log2 (n+1).

log2 (n+1) <= height <= 1.44 log2 (n+2)


Proof Of Upper Bound On Height

 Let Nh = min # of nodes in an AVL tree


whose height is h.
 N0 = 0.
 N1 = 1.
 In the worst case the height of one of the
subtrees is h-1, and the height of the other
is h-2.
Nh , h > 1

L R

 Both L and R are AVL trees.


 The height of one is h-1.
 The height of the other is h-2.
 The subtree whose height is h-1 has Nh-1 nodes.
 The subtree whose height is h-2 has Nh-2 nodes.
 So, Nh = Nh-1 + Nh-2 + 1.
Fibonacci Numbers

 F0 = 0, F1 = 1.
 Fi = Fi-1 + Fi-2 , i > 1.
 N0 = 0, N1 = 1.
 Nh = Nh-1 + Nh-2 + 1, i > 1.
 Nh = Fh+2 – 1.
 Fi ~ i/sqrt(5).
 sqrt
AVL Tree
-1
1
0
1 1
7 4
0 -1
0 0 1
4
3 8 3
5
0 0
0 0 -1 0 6
3
1 5 2 0
5
0
0
2
5
Balanced Search Trees

 Kinds of balanced binary search trees


 height balanced vs. weight balanced
 “Tree rotations” used to maintain balance on insert/delete
 Non-binary search trees
 2/3 trees
 each internal node has 2 or 3 children
 all leaves at same depth (height balanced)
 B-trees
 Generalization of 2/3 trees
 Each internal node has between k/2 and k children
 Each node has an array of pointers to children
 Widely used in databases

You might also like