0% found this document useful (0 votes)
29 views7 pages

Implementation of Binary Tree

This document implements a binary search tree in C. It defines a node struct with left, right, and data pointers. Functions are created to insert nodes, print the tree in pre-order, in-order and post-order traversals, search for a node, and delete the entire tree. In main(), a sample tree is created with inserted nodes, each traversal is printed, a node is searched for, and then the tree is deleted.

Uploaded by

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

Implementation of Binary Tree

This document implements a binary search tree in C. It defines a node struct with left, right, and data pointers. Functions are created to insert nodes, print the tree in pre-order, in-order and post-order traversals, search for a node, and delete the entire tree. In main(), a sample tree is created with inserted nodes, each traversal is printed, a node is searched for, and then the tree is deleted.

Uploaded by

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

Implementation of binary tree

PALANATI NEHA
20BCB7006
#include<stdlib.h>
#include<stdio.h>

struct bin_tree { int data;


struct bin_tree * right, * left;
};
typedef struct bin_tree node;

void insert(node ** tree, int val)


{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val; *tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}

void print_preorder(node * tree)


{ if
(tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}

}
void print_inorder(node * tree)
{ if
(tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}

void print_postorder(node * tree)


{ if
(tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}

void deltree(node * tree)


{
if (tree)
{
deltree(tree->left);
deltree(tree->right); free(tree);
}
}

node* search(node ** tree, int val)


{
if(!(*tree))
{
return NULL;
}

if(val < (*tree)->data)


{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}

void main()
{
node *root;
node *tmp;
//int i;

root = NULL; /* Inserting


nodes into tree */
insert(&root, 9); insert(&root,
4); insert(&root, 15);
insert(&root, 6); insert(&root,
12); insert(&root, 17);
insert(&root, 2);
/* Printing nodes of tree */
printf("Pre Order Display\n");
print_preorder(root);

printf("In Order Display\n");


print_inorder(root);

printf("Post Order Display\n");


print_postorder(root);

/* Search node into tree */


tmp = search(&root, 4); if
(tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
/* Deleting all nodes of tree */
deltree(root);
}

You might also like