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

Efficient Binary Trees

The document describes code for implementing efficient binary search trees. It includes code for a program that creates a binary search tree and performs operations like insertion, traversal, finding minimum/maximum elements, deletion, and calculating properties. It also includes code snippets for implementing simple right-threaded binary trees and showing insertion in an AVL tree.

Uploaded by

Vishal Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Efficient Binary Trees

The document describes code for implementing efficient binary search trees. It includes code for a program that creates a binary search tree and performs operations like insertion, traversal, finding minimum/maximum elements, deletion, and calculating properties. It also includes code snippets for implementing simple right-threaded binary trees and showing insertion in an AVL tree.

Uploaded by

Vishal Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

TITLE - Data Structures Using C, 2/e

AUTHOR - Reema Thareja

CHAPTER 10 - EFFICIENT BINARY TREES

***********************************************************************************
***********************

1. Write a program to create a binary search tree and perform all the operations
discussed
in the preceding sections.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree;
void create_tree(struct node *);
struct node *insertElement(struct node *, int);
void preorderTraversal(struct node *);
void inorderTraversal(struct node *);
void postorderTraversal(struct node *);
struct node *findSmallestElement(struct node *);
struct node *findLargestElement(struct node *);
struct node *deleteElement(struct node *, int);
struct node *mirrorImage(struct node *);
int totalNodes(struct node *);
int totalExternalNodes(struct node *);
int totalInternalNodes(struct node *);
int Height(struct node *);
struct node *deleteTree(struct node *);
int main()
{
int option, val;
struct node *ptr;
create_tree(tree);
clrscr();
do
{
printf("\n ******MAIN MENU******* \n");
printf("\n 1. Insert Element");
printf("\n 2. Preorder Traversal");
printf("\n 3. Inorder Traversal");
printf("\n 4. Postorder Traversal");
printf("\n 5. Find the smallest element");
printf("\n 6. Find the largest element");
printf("\n 7. Delete an element");
printf("\n 8. Count the total number of nodes");
printf("\n 9. Count the total number of external nodes");
printf("\n 10. Count the total number of internal nodes");
printf("\n 11. Determine the height of the tree");
printf("\n 12. Find the mirror image of the tree");
printf("\n 13. Delete the tree");
printf("\n 14. Exit");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the value of the new node : ");
scanf("%d", &val);
tree = insertElement(tree, val);
break;
case 2:
printf("\n The elements of the tree are : \n");
preorderTraversal(tree);
break;
case 3:
printf("\n The elements of the tree are : \n");
inorderTraversal(tree);
break;
case 4:
printf("\n The elements of the tree are : \n");
postorderTraversal(tree);
break;
case 5:
ptr = findSmallestElement(tree);
printf("\n Smallest element is :%d",ptr�>data);
break;
case 6:
ptr = findLargestElement(tree);
printf("\n Largest element is : %d", ptr�>data);
break;
case 7:
printf("\n Enter the element to be deleted : ");
scanf("%d", &val);
tree = deleteElement(tree, val);
break;
case 8:
printf("\n Total no. of nodes = %d", totalNodes(tree));
break;
case 9:
printf("\n Total no. of external nodes = %d",
totalExternalNodes(tree));
break;
case 10:
printf("\n Total no. of internal nodes = %d",
totalInternalNodes(tree));
break;
case 11:
printf("\n The height of the tree = %d",Height(tree));
break;
case 12:
tree = mirrorImage(tree);
break;
case 13:
tree = deleteTree(tree);
break;
}
}while(option!=14);
getch();
return 0;
}
void create_tree(struct node *tree)
{
tree = NULL;
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr�>data = val;
ptr�>left = NULL;
ptr�>right = NULL;
if(tree==NULL)
{
tree=ptr;
tree�>left=NULL;
tree�>right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr�>data)
nodeptr=nodeptr�>left;
else
nodeptr = nodeptr�>right;
}
if(val<parentptr�>data)
parentptr�>left = ptr;
else
parentptr�>right = ptr;
}
return tree;
}
void preorderTraversal(struct node *tree)
{
if(tree != NULL)
{
printf("%d\t", tree�>data);
preorderTraversal(tree�>left);
preorderTraversal(tree�>right);
}
}
void inorderTraversal(struct node *tree)
{
if(tree != NULL)
{
inorderTraversal(tree->left);
printf("%d\t", tree->data);
inorderTraversal(tree->right);
}
}
void postorderTraversal(struct node *tree)
{
if(tree != NULL)
{
postorderTraversal(tree->left);
postorderTraversal(tree->right);
printf("%d\t", tree->data);
}
}
struct node *findSmallestElement(struct node *tree)
{
if( (tree == NULL) || (tree->left == NULL))
return tree;
else
return findSmallestElement(tree ->left);
}
struct node *findLargestElement(struct node *tree)
{
if( (tree == NULL) || (tree->right == NULL))
return tree;
else
return findLargestElement(tree->right);
}
struct node *deleteElement(struct node *tree, int val)
{
struct node *cur, *parent, *suc, *psuc, *ptr;
if(tree�>left==NULL)
{
printf("\n The tree is empty ");
return(tree);
}
parent = tree;
cur = tree�>left;
while(cur!=NULL && val!= cur�>data)
{
parent = cur;
cur = (val<cur�>data)? cur�>left:cur�>right;
}
if(cur == NULL)
{
printf("\n The value to be deleted is not present in the tree");
return(tree);
}
if(cur�>left == NULL)
ptr = cur�>right;
else if(cur�>right == NULL)
ptr = cur�>left;
else
{
// Find the in�order successor and its parent
psuc = cur;
cur = cur�>left;
while(suc�>left!=NULL)
{
psuc = suc;
suc = suc�>left;
}
if(cur==psuc)
{
// Situation 1
suc�>left = cur�>right;
}
else
{
// Situation 2
suc�>left = cur�>left;
psuc�>left = suc�>right;
suc�>right = cur�>right;
}
ptr = suc;
}
// Attach ptr to the parent node
if(parent�>left == cur)
parent�>left=ptr;
else
parent�>right=ptr;
free(cur);
return tree;
}
int totalNodes(struct node *tree)
{
if(tree==NULL)
return 0;
else
return(totalNodes(tree�>left) + totalNodes(tree�>right) + 1);
}
int totalExternalNodes(struct node *tree)
{
if(tree==NULL)
return 0;
else if((tree�>left==NULL) && (tree�>right==NULL))
return 1;
else
return (totalExternalNodes(tree�>left) +
totalExternalNodes(tree�>right));
}
int totalInternalNodes(struct node *tree)
{
if( (tree==NULL) || ((tree�>left==NULL) && (tree�>right==NULL)))
return 0;
else
return (totalInternalNodes(tree�>left)
+ totalInternalNodes(tree�>right) + 1);
}
int Height(struct node *tree)
{
int leftheight, rightheight;
if(tree==NULL)
return 0;
else
{
leftheight = Height(tree�>left);
rightheight = Height(tree�>right);
if(leftheight > rightheight)
return (leftheight + 1);
else
return (rightheight + 1);
}
}
struct node *mirrorImage(struct node *tree)
{
struct node *ptr;
if(tree!=NULL)
{
mirrorImage(tree�>left);
mirrorImage(tree�>right);
ptr=tree�>left;
ptr�>left = ptr�>right;
tree�>right = ptr;
}
}
struct node *deleteTree(struct node *tree)
{
if(tree!=NULL)
{
deleteTree(tree�>left);
deleteTree(tree�>right);
free(tree);
}
}

***********************************************************************************
*************************

2. Write a program to implement simple right in-threaded binary trees.


#include <stdio.h>
#include <conio.h>
struct tree
{
int val;
struct tree *right;
struct tree *left;
int thread;
};
struct tree *root = NULL;
struct tree* insert_node(struct tree *root, struct tree *ptr, struct tree *rt)
{
if(root == NULL)
{
root = ptr;
if(rt != NULL)
{
root�>right = rt;
root�>thread = 1;
}
}
else if(ptr�>val < root�>val)
root�>left = insert_node(root�>left, ptr, root);
else
if(root�>thread == 1)
{
root�>right = insert_node(NULL, ptr, rt);
root�>thread=0;
}
else
root�>right = insert_node(root�>right, ptr, rt);
return root;
}
struct tree* create_threaded_tree()
{
struct tree *ptr;
int num;
printf("\n Enter the elements, press �1 to terminate ");
scanf("%d", &num);
while(num != �1)
{
ptr = (struct tree*)malloc(sizeof(struct tree));
ptr�>val = num;
ptr�>left = ptr�>right = NULL;
ptr�>thread = 0;
root = insert_node(root, ptr, NULL);
printf(" \n Enter the next element ");
fflush(stdin);
scanf("%d", &num);
}
return root;
}
void inorder(struct tree *root)
{
struct tree *ptr = root, *prev;
do
{
while(ptr != NULL)
{
prev = ptr;
ptr = ptr�>left;
}
if(prev != NULL)
{
printf(" %d", prev�>val);
ptr = prev�>right;
while(prev != NULL && prev�>thread)
{
printf(" %d", ptr�>val);
prev = ptr;
ptr = ptr�>right;
}
}
}while(ptr != NULL);
}
void main()
{
// struct tree *root=NULL;
clrscr();
create_threaded_tree();
printf(" \n The in�order traversal of the tree can be given as : ");
inorder(root);
getch();
}

***********************************************************************************
***********************

3. Write a program that shows insertion operation in an AVL tree.


#include <stdio.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int val;
int balance;
struct node *left_child;
struct node *right_child;
};
struct node* search(struct node *ptr, int data)
{
if(ptr!=NULL)
if(data < ptr -> val)
ptr = search(ptr -> left_child,data);
else if( data > ptr -> val)
ptr = search(ptr -> right_child, data);
return(ptr);
}
struct node *insert (int data, struct node *ptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;
if(ptr==NULL)
{
ptr = (struct node *) malloc(sizeof(struct node));
ptr -> val = data;
ptr -> left_child = NULL;
ptr -> right_child = NULL;
ptr -> balance = 0;
*ht_inc = TRUE;
return (ptr);
}
if(data < ptr -> val)
{
ptr -> left_child = insert(data, ptr -> left_child, ht_inc);
if(*ht_inc==TRUE)
{
switch(ptr -> balance)
{
case -1: /* Right heavy */
ptr -> balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
ptr -> balance = 1;
break;
case 1: /* Left heavy */
aptr = ptr -> left_child;
if(aptr -> balance == 1)
{
printf(�Left to Left Rotation\n�);
ptr -> left_child= aptr -> right_child;
aptr -> right_child = ptr;
ptr -> balance = 0;
aptr -> balance=0;
ptr = aptr;
}
else
{
printf(�Left to right rotation\n�);
bptr = aptr -> right_child;
aptr -> right_child = bptr -> left_child;
bptr -> left_child = aptr;
ptr -> left_child = bptr -> right_child;
bptr -> right_child = ptr;
if(bptr -> balance == 1 )
ptr -> balance = -1;
else
ptr -> balance = 0;
if(bptr -> balance == -1)
aptr -> balance = 1;
else
aptr -> balance = 0;
bptr -> balance=0;
ptr = bptr;
}
*ht_inc = FALSE;
}
}
}
if(data > ptr -> val)
{
ptr -> right_child = insert(info, ptr -> right_child, ht_inc);
if(*ht_inc==TRUE)
{
switch(ptr -> balance)
{
case 1: /* Left heavy */
ptr -> balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
ptr -> balance = -1;
break;
case -1: /* Right heavy */
aptr = ptr -> right_child;
if(aptr -> balance == -1)
{
printf(�Right to Right Rotation\n�);
ptr -> right_child= aptr -> left_child;
aptr -> left_child = ptr;
ptr -> balance = 0;
aptr -> balance=0;
ptr = aptr;
}
else
{
printf(�Right to Left Rotation\n�);
bptr = aptr -> left_child;
aptr -> left_child = bptr -> right_child;
bptr -> right_child = aptr;
ptr -> right_child = bptr -> left_child;
bptr -> left_child = pptr;
if(bptr -> balance == -1)
ptr -> balance = 1;
else
ptr -> balance = 0;
if(bptr -> balance == 1)
aptr -> balance = -1;
else
aptr -> balance = 0;
bptr -> balance=0;
ptr = bptr;
}/*End of else*/
*ht_inc = FALSE;
}
}
}
return(ptr);
}
void display(struct node *ptr, int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr -> right_child, level+1);
printf(�\n�);
for (i = 0; i < level; i++)
printf(� �);
printf(�%d�, ptr -> val);
display(ptr -> left_child, level+1);
}
}
void inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr -> left_child);
printf(�%d �,ptr -> val);
inorder(ptr -> right_child);
}
}
main()
{
bool ht_inc;
int data ;
int option;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
{
printf(�1.Insert\n�);
printf(�2.Display\n�);
printf(�3.Quit\n�);
printf(�Enter your option : �);
scanf(�%d�,&option);
switch(choice)
{
case 1:
printf(�Enter the value to be inserted : �);
scanf(�%d�, &data);
if( search(root,data) == NULL )
root = insert(data, root, &ht_inc);
else
printf(�Duplicate value ignored\n�);
break;
case 2:
if(root==NULL)
{
printf(�Tree is empty\n�);
continue;
}
printf(�Tree is :\n�);
display(root, 1);
printf(�\n\n�);
printf(�Inorder Traversal is: �);
inorder(root);
printf(�\n�);
break;
case 3:
exit(1);
default:
printf(�Wrong option\n�);
}
}
}

***********************************************************************************
**************************

You might also like