C++ Program To Implement Binary Search Tree 6
C++ Program To Implement Binary Search Tree 6
C++ Program To Implement Binary Search Tree 6
Iterative program to count leaf nodes in a Binary Tree (level order traversal)(uses queue)
7. Check if each internal node of a BST has exactly one child(pre-order traversal)
11. C Program to Construct a Binary Search Tree and perform deletion and inorder traversal
13. Write a program to Delete a Tree.(del leaf node, node with one child,node with 2 children)
1.
a binary tree*/
// If tree is empty
if (!node)
return 0;
queue<Node *> q;
q.push(node);
while (!q.empty())
q.pop();
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
count++;
return count;
2.
{
if(node==NULL)
return 0;
return 1;
else
return getLeafCount(node->left)+getLeafCount(node->right);
3.
if (node==NULL)
return 0;
else
return(size(node->left) + 1 + size(node->right));
4.
#include<iostream>
struct node
int data;
char name[50];
node* right;
node* left;
};
if(root!=NULL)
traversal(root->left);
cout<<endl<<root->data<<endl<<root->name;
traversal(root->right);
temp->data=info;
cin>>temp->name;
temp->right=temp->left=NULL;
return temp;
if (temp==NULL)
return create(info);
if(info<temp->data)
temp->left=insert(temp->left,info);
temp->right=insert(temp->right,info);
return temp;
int main()
node* root=NULL;
int opt;
while(1)
cout<<"1.insert"<<endl<<"2.traverse"<<endl<<"3.exit";
cin>>opt;
switch(opt)
case 1:
int inf;
cin>>inf;
root=insert(root,inf);
break;
case 2:
traversal(root);
break;
case 3:
exit(0);
return 0;
5.
# include <iostream>
# include <cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
/*
* Class Declaration
*/
class BST
{
public:
void find(int, node **, node **);
void insert(int);
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};
/*
* Main Contains Menu
*/
int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
* Find Element in the Tree
*/
void BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
/*
* Inserting Element into the Tree
*/
void BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
return;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return;
}
}
/*
* Delete Element from the tree
*/
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
free(location);
}
/*
* Case A
*/
void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
/*
* Case B
*/
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
/*
* Case C
*/
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
/*
* Pre Order Traversal
*/
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
/*
* In Order Traversal
*/
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
/*
* Postorder Traversal
*/
void BST::postorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
/*
* Display Tree Structure
*/
void BST::display(node *ptr, int level)
{
int i;
if (ptr != NULL)
{
{
display(ptr->right, level+1);
cout<<endl;
if (ptr == root)
cout<<"Root->: ";
else
{
for (i = 0;i < level;i++)
cout<<" ";
}
cout<<ptr->info;
display(ptr->left, level+1);
}
}
6.
// A utility function to do inorder traversal of BST
if (root != NULL)
{
inorder(root->left);
inorder(root->right);
return node;
7.
if (nextDiff*lastDiff < 0)
return false;;
return true;
int main()
printf("Yes");
else
printf("No");
return 0;
8.
if (node == NULL)
return newNode(key);
return node;
root->key > N)
return -1;
value*/
return root->key;
// left subtree
if (root->key >= N)
// right subtree
else
9.
parameters). */
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(data));
else
else
return node;
to be searched. */
current = current->left;
return(current->data);
10.
// Removes all nodes having value outside the given range and returns the root
// of modified tree
// Base Case
if (root == NULL)
return NULL;
// Now fix the root. There are 2 possible cases for toot
// 1.a) Root's key is smaller than min value (root is not in range)
delete root;
return rChild;
}
// 1.b) Root's key is greater than max value (root is not in range)
delete root;
return lChild;
// 2. Root is in range
return root;
// A utility function to create a new BST node with key as given num
temp->key = num;
return temp;
if (root == NULL)
return newNode(key);
else
return root;
if (root)
inorderTraversal( root->left );
inorderTraversal( root->right );
11.
a linked list*/
push(&result, t1->data);
t1 = t1->next;
if (!isPresent(result, t2->data))
push(&result, t2->data);
t2 = t2->next;
return result;
}
/* Function to get intersection of two linked lists
if (isPresent(head2, t1->data))
t1 = t1->next;
return result;
{
/* allocate node */
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
node = node->next;
while (t != NULL)
if (t->data == data)
return 1;
t = t->next;
return 0;
12.
deleteTree(node->left);
deleteTree(node->right);
13.
temp->key = item;
return temp;
if (root != NULL)
inorder(root->left);
inorder(root->right);
else
return node;
/* Given a non-empty binary search tree, return the node with minimum
key value found in that tree. Note that the entire tree does not
need to be searched. */
current = current->left;
return current;
/* Given a binary search tree and a key, this function deletes the key
// base case
// to be deleted
else
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
}
14.
if (root == NULL)
return newNode(data);
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
free(root);
return NULL;
// subtrees.
root->left = leafDelete(root->left);
root->right = leafDelete(root->right);
return root;
15.
// Base case
if (root == NULL)
return;
{
printSingles(root->left);
printSingles(root->right);
printSingles(root->right);
printSingles(root->left);
16.
if (level[i] == 'L')
ptr->left = newNode(key);
else
ptr->right = newNode(key);
}