0% found this document useful (0 votes)
51 views28 pages

C Program of Trees

The document discusses creating and traversing binary trees in C. It defines a node struct with left, right, and data pointers. A createNode function allocates and initializes new nodes. Main constructs sample trees and calls functions to perform preorder, inorder, and postorder traversals, as well as check if a tree is a valid binary search tree and search for a node.

Uploaded by

Harsh Ojha
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)
51 views28 pages

C Program of Trees

The document discusses creating and traversing binary trees in C. It defines a node struct with left, right, and data pointers. A createNode function allocates and initializes new nodes. Main constructs sample trees and calls functions to perform preorder, inorder, and postorder traversals, as well as check if a tree is a valid binary search tree and search for a node.

Uploaded by

Harsh Ojha
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/ 28

@LINKED REPRESENTATION

#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

int main(){

/*

// Constructing the root node

struct node *p;

p = (struct node *) malloc(sizeof(struct node));

p->data = 2;

p->left = NULL;

p->right = NULL;

// Constructing the second node

struct node *p1;

p1 = (struct node *) malloc(sizeof(struct node));

p->data = 1;

p1->left = NULL;
p1->right = NULL;

// Constructing the third node

struct node *p2;

p2 = (struct node *) malloc(sizeof(struct node));

p->data = 4;

p2->left = NULL;

p2->right = NULL;

*/

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(2);

struct node *p1 = createNode(1);

struct node *p2 = createNode(4);

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

return 0;

# Pre order traversal program

#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data


n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(4);

struct node *p1 = createNode(1);

struct node *p2 = createNode(6);

struct node *p3 = createNode(5);

struct node *p4 = createNode(2);

// Finally The tree looks like this:

// 4

// /\

// 1 6

// / \

// 5 2

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

preOrder(p);

return 0;
}

# POST ORDER TRAVERSAL


#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

}
}

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(4);

struct node *p1 = createNode(1);

struct node *p2 = createNode(6);

struct node *p3 = createNode(5);

struct node *p4 = createNode(2);

// Finally The tree looks like this:

// 4

// /\

// 1 6

// / \

// 5 2

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

preOrder(p);

printf("\n");

postOrder(p);

return 0;

@ IN-ORDER TRAVERSAL
#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;


struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

}
int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(4);

struct node *p1 = createNode(1);

struct node *p2 = createNode(6);

struct node *p3 = createNode(5);

struct node *p4 = createNode(2);

// Finally The tree looks like this:

// 4

// /\

// 1 6

// / \

// 5 2

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

// preOrder(p);

// printf("\n");

// postOrder(p);

// printf("\n");

inOrder(p);

return 0;

@ CHECK IT IS BINARTY TREE OR NOT

#include<stdio.h>

#include<malloc.h>

struct node{

int data;
struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

}
}

int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL);

if(!isBST(root->left)){

return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);

struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// /\

// 3 6

// / \

// 1 4
// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

// preOrder(p);

// printf("\n");

// postOrder(p);

// printf("\n");

inOrder(p);

printf("\n");

// printf("%d", isBST(p));

if(isBST(p)){

printf("This is a bst" );

else{

printf("This is not a bst");

return 0;

@SEARCHING IN BINARY TREE


#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer


n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL){

if(!isBST(root->left)){
return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

struct node * search(struct node* root, int key){

if(root==NULL){

return NULL;

if(key==root->data){

return root;

else if(key<root->data){

return search(root->left, key);

else{

return search(root->right, key);

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);


struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// /\

// 3 6

// / \

// 1 4

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

struct node* n = search(p, 10);

if(n!=NULL){

printf("Found: %d", n->data);

else{

printf("Element not found");

return 0;

@ITERATIVE SEARCH IN BIARY TREE


#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;


struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

}
int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL){

if(!isBST(root->left)){

return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

struct node * searchIter(struct node* root, int key){

while(root!=NULL){

if(key == root->data){

return root;

else if(key<root->data){

root = root->left;

else{

root = root->right;

return NULL;

int main(){
// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);

struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// /\

// 3 6

// / \

// 1 4

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

struct node* n = searchIter(p, 6);

if(n!=NULL){

printf("Found: %d", n->data);

else{

printf("Element not found");

return 0;

@INSERTION IN BINARY TREE


#include<stdio.h>

#include<malloc.h>
struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);


inOrder(root->right);

int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL){

if(!isBST(root->left)){

return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

struct node * searchIter(struct node* root, int key){

while(root!=NULL){

if(key == root->data){

return root;

else if(key<root->data){

root = root->left;

else{

root = root->right;

}
return NULL;

void insert(struct node *root, int key){

struct node *prev = NULL;

while(root!=NULL){

prev = root;

if(key==root->data){

printf("Cannot insert %d, already in BST", key);

return;

else if(key<root->data){

root = root->left;

else{

root = root->right;

struct node* new = createNode(key);

if(key<prev->data){

prev->left = new;

else{

prev->right = new;

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);


struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// /\

// 3 6

// / \

// 1 4

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

insert(p, 16);

printf("%d", p->right->right->data);

return 0;

@DELETION CODE
#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer


n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL){

if(!isBST(root->left)){
return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

struct node * searchIter(struct node* root, int key){

while(root!=NULL){

if(key == root->data){

return root;

else if(key<root->data){

root = root->left;

else{

root = root->right;

return NULL;

void insert(struct node *root, int key){

struct node *prev = NULL;

while(root!=NULL){

prev = root;

if(key==root->data){
printf("Cannot insert %d, already in BST", key);

return;

else if(key<root->data){

root = root->left;

else{

root = root->right;

struct node* new = createNode(key);

if(key<prev->data){

prev->left = new;

else{

prev->right = new;

struct node *inOrderPredecessor(struct node* root){

root = root->left;

while (root->right!=NULL)

root = root->right;

return root;

struct node *deleteNode(struct node *root, int value){

struct node* iPre;

if (root == NULL){

return NULL;

}
if (root->left==NULL&&root->right==NULL){

free(root);

return NULL;

//searching for the node to be deleted

if (value < root->data){

root-> left = deleteNode(root->left,value);

else if (value > root->data){

root-> right = deleteNode(root->right,value);

//deletion strategy when the node is found

else{

iPre = inOrderPredecessor(root);

root->data = iPre->data;

root->left = deleteNode(root->left, iPre->data);

return root;

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);

struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// /\

// 3 6

// / \
// 1 4

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

inOrder(p);

printf("\n");

deleteNode(p, 3);

inOrder(p);

return 0;

@AVL TREE PROGRAM


#include <stdio.h>

#include <stdlib.h>

struct Node

int key;

struct Node *left;

struct Node *right;

int height;

};

int getHeight(struct Node *n){

if(n==NULL)

return 0;

return n->height;

struct Node *createNode(int key){

struct Node* node = (struct Node *) malloc(sizeof(struct Node));

node->key = key;
node->left = NULL;

node->right = NULL;

node->height = 1;

return node;

int max (int a, int b){

return (a>b)?a:b;

int getBalanceFactor(struct Node * n){

if(n==NULL){

return 0;

return getHeight(n->left) - getHeight(n->right);

struct Node* rightRotate(struct Node* y){

struct Node* x = y->left;

struct Node* T2 = x->right;

x->right = y;

y->left = T2;

x->height = max(getHeight(x->right), getHeight(x->left)) + 1;

y->height = max(getHeight(y->right), getHeight(y->left)) + 1;

return x;

struct Node* leftRotate(struct Node* x){

struct Node* y = x->right;

struct Node* T2 = y->left;

y->left = x;

x->right = T2;

x->height = max(getHeight(x->right), getHeight(x->left)) + 1;

y->height = max(getHeight(y->right), getHeight(y->left)) + 1;


return y;

struct Node *insert(struct Node* node, int key){

if (node == NULL)

return createNode(key);

if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

node->height = 1 + max(getHeight(node->left), getHeight(node->right));

int bf = getBalanceFactor(node);

// Left Left Case

if(bf>1 && key < node->left->key){

return rightRotate(node);

// Right Right Case

if(bf<-1 && key > node->right->key){

return leftRotate(node);

// Left Right Case

if(bf>1 && key > node->left->key){

node->left = leftRotate(node->left);

return rightRotate(node);

// Right Left Case

if(bf<-1 && key < node->right->key){

node->right = rightRotate(node->right);

return leftRotate(node);

return node;

}
void preOrder(struct Node *root)

if(root != NULL)

printf("%d ", root->key);

preOrder(root->left);

preOrder(root->right);

int main(){

struct Node * root = NULL;

root = insert(root, 1);

root = insert(root, 2);

root = insert(root, 4);

root = insert(root, 5);

root = insert(root, 6);

root = insert(root, 3);

preOrder(root);

return 0;

You might also like