0% found this document useful (0 votes)
3 views32 pages

DS 10Binary Search Tree

The document provides an overview of binary trees, including definitions, types, and traversal methods. It explains the structure of binary search trees, how to search for elements, and the process of deleting nodes with different scenarios. Additionally, it includes code examples for implementing a binary search tree in C++.

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
3 views32 pages

DS 10Binary Search Tree

The document provides an overview of binary trees, including definitions, types, and traversal methods. It explains the structure of binary search trees, how to search for elements, and the process of deleting nodes with different scenarios. Additionally, it includes code examples for implementing a binary search tree in C++.

Uploaded by

kinshahra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 32

Data Structure

Video Lecture No. 10 Video Lecture No. 11 Video Lecture No. 12 Video Lecture No. 13

Lecture No. 10, 11, 12, 13

Binary Search Tree


Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chis
hti International Islamic University H-10, Islamabad, Pakistan
Binary Tree
 A binary tree is a finite set of elements, that is either empty or is partitioned
into three disjoint subsets. root
 The first subset contains a single element called
 the root of the tree. A
 The other two subsets are themselves
 binary trees called the left and B C
 right subtrees.
 Each element of a binary tree D E F
 is called a node of the tree.
G H I

Left subtree
Right subtree
Not a Tree A

 Structures that are not trees. B C

D E F

G H I

A A

B C B C

F D E F
D E

H I G H I
G
Binary Tree: Terminology

parent
A

Left descendant B C Right descendant

D E F

G H I

Leaf nodes Leaf nodes


Strictly Binary Tree
 If every non-leaf node in a binary tree has non-empty left and right subtrees,
the tree is termed a strictly binary tree.
 Level of a Binary Tree Node
 Root has level 0, root
 Level of any other node is one more than the level its parent (father).
 The depth of a binary tree is Level 0 A
 the maximum level of any leaf
 in the tree.
Level 1 B C

D E J F
Level 2

G K H I
Level 3
Complete Binary Tree
 A complete binary tree of depth d is the strictly binary all of whose leaves are
at level d.
 At level k, there are 2k leaf nodes.
 Total number of nodes in the tree of depth d:
d
 20+ 21+ 22 + ………. + 2d = ∑ 2i = 2d+1 – 1
i=0
A Level 0: 20 nodes
 In a complete binary tree
B C
 there are 2d leaf nodes Level 1: 21 nodes
 and (2d - 1) non-leaf (inner) nodes.
D E F G Level 2: 22 nodes

H I J K L M N O Level 3: 23 nodes
Complete Binary Tree
 If the tree is built out of ‘n’ nodes then
n = 2d+1 – 1 n + 1 = 2d+1
log2(n + 1) = log2(2)d+1 because log2(2) = 1
or log2(n+1) = d+1
or d = log2(n+1) – 1

 I.e., the depth of the complete binary tree built using ‘n’ nodes will be
log2(n+1) – 1.
 For example, for n=1,000,000, log2(1000001) is less than 20; the tree would be
20 levels deep.
 The significance of this shallowness will become evident later.
Binary Search Tree Construction
14 15 4 9 7 18 3 5 16 20 17

Root
Searching in Binary Search Tree
 Searching Number 16 Root

14

4 15

3 9 18

7 16 20

5 17
Searching in Binary Search Tree: Find_Min()
 Return the address of the smallest element in the tree Root
 Start at the root
 Go left as long as there is a left child 14

Tree_Node* Find_Min(Tree_Node *node)


4 15
{
if(node == NULL)
return NULL; 3 9 18
if(node->left == NULL)
return node;
7 16 20
return Find_Min(node->left);
}
5 17
Searching in Binary Search Tree: Find_Max()
 Return the address of the largest element in the tree Root
 Start at the root
 Go right as long as there is a right child 14

Tree_Node* Find_Max(Tree_Node *node)


4 15
{
if(node == NULL)
return NULL; 3 9 18
if(node->right == NULL)
return node;
7 16 20
return Find_Max(node->right);
}
5 17
Traversing a Binary Search Tree
 Pre Order Traversal Root
 Show the Current Tree Node
 Traverse the left subtree in Pre Order
14
 Traverse the right subtree in Pre Order

4 15

3 9 18

7 16 20

5 17
 Result: 14 4 3 9 7 5 15 18 16 17 20
Traversing a Binary Search Tree
 In Order Traversal Root
 Traverse the left subtree in In Order.
 Show the Current Tree Node
14
 Traverse the right subtree in In Order.

4 15

3 9 18

7 16 20

 can be used as a sorting algorithm. 5 17


 Result: 3 4 5 7 9 14 15 16 17 18 20
Traversing a Binary Search Tree
 Post Order Traversal Root
 Traverse the left subtree in Post Order.
 Traverse the right subtree in Post Order.
14
 Show the Current Tree Node

4 15

3 9 18

7 16 20

 Result: 3 5 7 9 4 17 16 20 18 15 14 5 17
Traversing a Binary Search Tree
 Level Order Traversal Root
 It uses a queue when traversing

14
1. Start from root node
2. Show the Current Tree Node
4 15
3. If left child is present, Add it in queue
4. If right child is present, Add it in queue
5. While Queue is not empty go to step 2 3 9 18

 As a result, it traverses the tree, level by level 7 16 20

5 17
Deleting a Leaf Node in BST Video Lecture

 It is common with many data structures, the hardest operation is deletion.


 Once we have found the node to be deleted, we need to consider several
possibilities.
 If the node is a leaf, it can be deleted immediately.

Root Root

14 14

4 15 4 15

3 9 18 9 18
Deleting a Leaf Node in BST

Root Root

14 14

4 15 4 15

3 9 18 3 9
Deleting a node with one child in BST
 If the node has one child, the node can be deleted after replacing this node
with its child node.

Root Root Root

14 14 14

By
4 15 4 15 4 pa
ss 15
link

node to be Bypass link


3 9 3 9 3 7
deleted

7 7 6 8

6 8 6 8
Deleting a node with one child in BST

Root Root
Root

14 14
14
4 15 4 15
4 Bypass link
15
3 node to 5 3 5
3 7
be deleted
Bypass lin
k
7 7 6 8

6 8 6 8
Deleting a node with two children
 When the node to be deleted has both left and right subtrees.
Root  Replace the data of this node Root
with the smallest data of the
Root
10 right subtree Root
10

node to be  And then recursively delete


deleted
3 11 that smallest node. 5 11
copy

1 6 1 6

Smallest
node in 5 8 8
right sub
tree
7 9 7 9
Deleting a node in BST

Root Root Root

Root
6 Root
6 Root
6

node to be
deleted 2 8 3 8 3 8
copy

1 5 1 5 1 5

3 Delete this 3 4
node
Smallest node
in right sub tree 4 4
Example 1: Implementing of Binary Search Tree
#include <iostream> class Queue{
using namespace std; private :
typedef int Type; Node *front, *rear ;
public :
struct Tree_Node{ Queue( ) ;
Tree_Node* left ; bool Is_Empty(){
Type data ; return front == NULL;
Tree_Node* right ; }
}; void Put ( QType Data ) ;
typedef Tree_Node* QType; QType Get( ) ;
~Queue( ) ;
struct Node{ };
QType data ;
Node *next ; Queue :: Queue( ){
}; front = rear = NULL ;
}

09_Binary_Search_Tree.cpp
1 22 2
Example 1: Implementing of Binary Search Tree
void Queue :: Put ( QType Data ){ QType Queue :: Get( ){
Node *newNode ; if ( front == NULL ){
newNode = new Node ; cout << "Queue is empty" ; exit(-1);
if ( newNode == NULL ) }
cout << "\nQueue is full" ; Node *current; QType Data ;
newNode -> data = Data ; Data = front -> data ;
newNode -> next = NULL ; current = front ;
if ( front == NULL ){ front = front -> next ;
rear = front = newNode ; delete current ; return Data ;
return ; }
} Queue :: ~Queue( ){
rear -> next = newNode ; if ( front == NULL )
rear = rear -> next ; return ;
} Node *current ;
while ( front != NULL ){
current = front ;
front = front -> next ;
delete current ;
}
}
3 23 4
Example 1: Implementing of Binary Search Tree
class Binary_Tree{ void Post_Order ( );
private: void Level_Order( );
Tree_Node* root; bool Search (Type _data);
void Insert (Tree_Node* , Type void Find_Min ( );
_data ); void Find_Max ( );
Tree_Node* Remove (Tree_Node* , };
Type _data ); Binary_Tree::Binary_Tree(){
void Pre_Order (Tree_Node* ); root = NULL;
void In_Order (Tree_Node* ); }
void Post_Order (Tree_Node* ); void Binary_Tree :: Insert(Type _data){
void Level_Order (Tree_Node* Insert(root, _data);
node); }
bool Search (Type key , void Binary_Tree :: Insert(Tree_Node *node,
Tree_Node* ); Type _data)
Tree_Node* Find_Min(Tree_Node*); {
Tree_Node* Find_Max(Tree_Node*); // if tree is empty
public: if (root == NULL){
Binary_Tree (); root = new Tree_Node;
void Insert (Type _data); root->data = _data;
void Remove (Type _data); root->left = NULL; root->right =
5 24 6
void Pre_Order ( ); NULL;
Example 1: Implementing of Binary Search Tree
else if(_data >= node->data) { node->left->left = NULL;
// if right subtree is present node->left->right = NULL;
if(node->right != NULL) }
Insert(node->right, _data); }
// create new node }
else{ void Binary_Tree :: Remove(Type _data){
node->right = new Tree_Node; Remove(root, _data);
node->right->data = _data; }
node->right->left = NULL;
node->right->right = NULL; Tree_Node* Binary_Tree :: Remove(Tree_Node
} *node,
} Type x){
else{ if( node == NULL )
// if left subtree is present return NULL; // Item not found; do
if(node->left != NULL) nothing
Insert(node->left, _data); if( x < node->data )
// create new node node->left = Remove(node->left,x );
else{ else if( x > node->data )
node->left = new Tree_Node; node->right = Remove(node->right, x );
node->left->data = _data;
7 25 8
Example 1: Implementing of Binary Search Tree
else { delete oldNode;
// if Node has no child return node;
if(node->left==NULL && }
node->right==NULL){ else{ // if node has two children
delete node; // Replace the data of this node
return NULL;
} // with the smallest data of
// if Node has one right child // the right subtree
else if ( node->left == NULL && node->data =
node->right != NULL){ Find_Min( node->right )->data;
Tree_Node *oldNode = node; // recursively delete smallest node
node = node ->right; node->right = Remove( node->right,
delete oldNode; node->data);
return node; }
}
// if Node has one left child }
else if ( node->right == NULL && return node;
node->left != NULL){ }
Tree_Node *oldNode = node; void Binary_Tree :: Pre_Order ( ){
node = node ->left; Pre_Order(root);
9 26 10
}
Example 1: Implementing of Binary Search Tree
bool Binary_Tree :: Search(Type _data) void Binary_Tree :: Pre_Order(Tree_Node
{ *node){
return Search(_data,root); if(node != NULL){
} cout << node->data << " ";
bool Binary_Tree :: Search(Type key , Pre_Order(node->left);
Tree_Node* Pre_Order(node->right);
node){ }
bool found = false; }
// node is not present void Binary_Tree :: In_Order ( ){
if(node == NULL) In_Order(root);
return false; }
// if node with same data is found
if( key == node->data) void Binary_Tree :: In_Order(Tree_Node *node)
return true; {
else if( key > node->data ) if(node != NULL){
found = Search( key, node->right In_Order(node->left);
); cout << node->data << " ";
else In_Order(node->right);
found = Search( key, node- }
>left); }
11 27 12
Example 1: Implementing of Binary Search Tree
void Binary_Tree :: Post_Order ( ){ if(node->left != NULL )
Post_Order(root); Q.Put( node->left);
} if(node->right != NULL )
void Binary_Tree :: Post_Order( Q.Put( node->right);
Tree_Node }
*node){ cout << endl;
if(node != NULL){ }
Post_Order(node->left); void Binary_Tree :: Level_Order(){
Post_Order(node->right); Level_Order(root);
cout << node->data << " "; }
} void Binary_Tree :: Find_Min ( ){
} Tree_Node* node = Find_Min(root);
void Binary_Tree :: Level_Order( if(node != NULL)
Tree_Node* cout <<"Minimum Number in the tree is:
node){ "
Queue Q; << node->data << endl;
if( node == NULL ) return; }
Q.Put(node); Tree_Node * Binary_Tree :: Find_Min(
while( !Q.Is_Empty() ){ Tree_Node *n ){
node = Q.Get(); if(n == NULL)
13 28 14
cout << node->data << " "; return NULL;
Example 1: Implementing of Binary Search Tree
if(n->left == NULL) int main(){
return n; Binary_Tree tree;
return Find_Min(n->left); int s;
} int Numbers [] =
void Binary_Tree :: Find_Max ( ){ {14,15,4,9,7,18,3,5,16,20,17};
Tree_Node* n = Find_Max(root); // int Numbers [] =
if( n != NULL) {10,3,11,6,1,8,5,9,7};
cout <<"Maximum Number in the tree
is: " int size = sizeof(Numbers) /
<< n->data << endl; sizeof(int);
} for (int i = 0 ; i<size ; i++){
tree.Insert(Numbers[i]);
Tree_Node * Binary_Tree :: Find_Max( }
Tree_Node cout <<" -:Pre_Order Traversal:-" <<
*node){ endl;
if(node == NULL) tree.Pre_Order();
return NULL;
if(node->right == NULL) cout <<"\n\n -:In_Order
return node; Traversal:-"<<endl;
return Find_Max(node->right); tree.In_Order();
15 29 16
}
Example 1: Implementing of Binary Search Tree
cout <<"\n\n -:Level_Order Traversal:-\ cout << endl;
n"; system("PAUSE"); return 0;
tree.Level_Order(); }
cout << endl;

tree.Find_Max();
tree.Find_Min();

cout <<"Enter a Number to Search: ";


cin >> s;

if (tree.Search(s))
cout << "found " << s << endl;
else
cout << s << " is not present " <<
endl;

cout <<"Enter a Number to Remove: ";


cin >> s;
tree.Remove(s);
cout <<" -:Pre_Order Traversal:-\n"; 17 18
30
Applications of Binary Search Tree
 A binary search tree can be used in sorting algorithm implementation. The
process involves inserting all the elements which are to be sorted and then
performing inorder traversal.
 It is used to implement searching Algorithm.
 It can be used to implement dictionary.
 It can be used to Implement routing table in router.
 It is used to implement multilevel indexing in DATABASE.
 It is used in implementation of Huffman Coding Algorithm for data
compression.
 Binary trees are a good way to express arithmetic expressions. The leaves are
operands and the other nodes are operators.
Applications of Binary Search Tree
 Binary trees can also be used for classification
purposes. A decision tree is a supervised machine
learning algorithm. The binary tree data structure
is used here to emulate the decision-making
process.
 A decision tree usually begins with a root node.
The internal nodes are conditions or dataset
features. Branches are decision rules while the
leave nodes are the outcomes of the decision.
 For example, suppose we want to classify apples.
The decision tree for this problem will be as
follows:

You might also like