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

Circular Queue Using Linked List

The document discusses binary search trees. It defines a binary search tree as a binary tree where: 1) Each node has a key greater than all keys in its left subtree and less than all keys in its right subtree. 2) Searching a node involves recursively searching the left subtree if the key is less than the node's key, or the right subtree if greater. 3) Insertion first searches to find where the new node belongs, then inserts it there. 4) Deletion of non-leaf nodes requires adjusting pointers if the node has one or two children.

Uploaded by

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

Circular Queue Using Linked List

The document discusses binary search trees. It defines a binary search tree as a binary tree where: 1) Each node has a key greater than all keys in its left subtree and less than all keys in its right subtree. 2) Searching a node involves recursively searching the left subtree if the key is less than the node's key, or the right subtree if greater. 3) Insertion first searches to find where the new node belongs, then inserts it there. 4) Deletion of non-leaf nodes requires adjusting pointers if the node has one or two children.

Uploaded by

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

Circular Queue using Linked List:

 CLL is a SLL where last node points to first node in the list
 It does not contain null pointers like SLL
 We can traverse the list in only one direction
 Its advantage is that when we want to go from last node to first node, it directly points to
first node

Figure: CLL
CIRCULAR QUEUE USING LINKED LIST:
/* C Program to implement queue using circular linked list*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node *link;
}*rear=NULL;

void insert(int item);


int del();
void display();
int isEmpty();
int peek();

int main()
{
int choice,item;
while(1)
{
printf("\n1.Insert\n");
printf("2.Delete\n");
printf("3.Peek\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
DEPT OF INFORMATION TECHNOLOGY, MJCET XX
switch(choice)
{
case 1:
printf("\nEnter the element for insertion : ");
scanf("%d",&item);
insert(item);
break;
case 2:
printf("\nDeleted element is %d\n",del());
break;
case 3:
printf("\nItem at the front of queue is %d\n",peek());
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\nWrong choice\n");
}
}
}

void insert(int item)


{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=item;
if(tmp==NULL)
{
printf("\nMemory not available\n");
return;
}

if( isEmpty() )
{
rear=tmp;
tmp->link=rear;
}
else
{
tmp->link=rear->link;
rear->link=tmp;
rear=tmp;
}

DEPT OF INFORMATION TECHNOLOGY, MJCET XXI


}

del()
{
int item;
struct node *tmp;
if( isEmpty() )
{
printf("\nQueue underflow\n");
exit(1);
}
if(rear->link==rear)
{
tmp=rear;
rear=NULL;
}
else
{
tmp=rear->link;
rear->link=rear->link->link;
}
item=tmp->info;
free(tmp);
return item;
}

int peek()
{
if( isEmpty() )
{
printf("\nQueue underflow\n");
exit(1);
}
return rear->link->info;
}

int isEmpty()
{
if( rear == NULL )
return 1;
else
return 0;
}

void display()
{

DEPT OF INFORMATION TECHNOLOGY, MJCET XXII


struct node *p;
if(isEmpty())
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue is :\n");
p=rear->link;
do
{
printf("%d ",p->info);
p=p->link;
}while(p!=rear->link);
printf("\n");
}

OUTPUT:

VIVA QUESTIONS
1. What is data structure
2. How the memory is allocated dynamically
3. What is linked list
4. What is node
5. What are the types of linked list

DEPT OF INFORMATION TECHNOLOGY, MJCET XXIII


BINARY TREE TRAVERSAL

Binary Trees: A binary tree is a special kind of tree in which each node can have at most two children:
they are distinguished as a left child and a right child. The subtree rooted at the left child of a node is
called its left subtree and the subtree rooted at the right child of a node is called its right subtree.

Level of a node refers to the distance of a node from the root. The level of the root is 1. Children of the
root are at level 2, ‖grandchildren‖ or children of the children of the root are at level 3, etc. The height
of a tree is the largest level of any node in the tree. In this case the term depth is used to indicate the
largest level.
Maximum number of nodes on a level i of a binary tree is 2i-1. Also the maximum number of nodes in a
binary tree of depth k is 2k – 1, k>0.

Figure 12: a Binary Tree

Tree traversal is the process of visiting each node in the tree exactly once. Visiting each node in a graph
should be done in a systematic manner. If search result in a visit to all the vertices, it is called a traversal.
There are basically three traversal techniques for a binary tree that are,

1. Preorder traversal
2. Inorder traversal
3. Postorder traversal

1) Preorder traversal

To traverse a binary tree in preorder, following operations are carried out:

1. Visit the root.


2. Traverse the left sub tree of root.
3. Traverse the right sub tree of root.

DEPT OF INFORMATION TECHNOLOGY, MJCET XXIV


2) Inorder traversal

To traverse a binary tree in inorder traversal, following operations are carried out:

1. Traverse the left most sub tree.


2. Visit the root.
3. Traverse the right most sub tree.

3) Postorder traversal

To traverse a binary tree in postorder traversal, following operations are carried out:

1. Traverse the left sub tree of root.


2. Traverse the right sub tree of root.
3. Visit the root.

BINARY TREE TRAVERSAL:


#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* left;
struct node* right;
};

void inorder(struct node* root){


if(root == NULL) return;
inorder(root->left);
printf("%d ->", root->data);
inorder(root->right);
}

void preorder(struct node* root){


if(root == NULL) return;
printf("%d ->", root->data);
preorder(root->left);
preorder(root->right);
}

void postorder(struct node* root) {


if(root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%d ->", root->data);
}

DEPT OF INFORMATION TECHNOLOGY, MJCET XXV


struct node* createNode(value){
struct node* newNode = malloc(sizeof(struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

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


root->left = createNode(value);
return root->left;
}

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


root->right = createNode(value);
return root->right;
}

int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorder(root);

printf("\nPreorder traversal \n");


preorder(root);

printf("\nPostorder traversal \n");


postorder(root);
}

OUTPUT:

DEPT OF INFORMATION TECHNOLOGY, MJCET XXVI


VIVA QUESTIONS
1. Define binary tree?
2. Which traversal technique should be used to arrange the elements in ascending order?
3. Construct the binary tree if inorder and postorder are given?

DEPT OF INFORMATION TECHNOLOGY, MJCET XXVI


I
Binary Search Tree: A binary search tree is a binary tree which may be empty. If not empty, it
satisfies the following properties:
1. Every element has a key and no two elements have the same key .
2. The keys (if any) in the left subtree are smaller than the key in the root
3. The keys (if any) in the right subtree are greater than the key in the root
4. The left and right subtrees are binary search trees.

Figure 13: Binary Search Tree on Numbers Figure 14: Binary Search Tree on Strings

Searching a Binary Search Tree: Suppose we wish to search for an element with key x. We
being at root. If the root is 0, then the search tree contains no elements and the search terminates
unsuccessfully. Otherwise, we compare x with key in root. If x equals key in root, then search
terminates successfully. If x is less than key in root, then no element in right sub tree can have
key value x, and only left subtree is to be searched. If x is larger than key in root, the no element
in left subtree can have the key x, and only right subtree is to be searched. The subtrees can be
searched recursively.
Insertion into a Binary Search Tree: To insert an element x, we must first verify that its key is
different from those of existing elements. To do this, a search is carried out. If search is
unsuccessful, then element is inserted at point where the search terminated.

DEPT OF INFORMATION TECHNOLOGY, MJCET XXVI


II
Deleting from a Binary Search Tree: Deletion from a leaf element is achieved by simply
removing the leaf node and making its parent’s child field to be null. Other cases are deleting a
node with one subtree and two subtrees.

Figure 15: Deleting a leaf node Figure 16: Deleting a non leaf node.

BINARY SEARCH TREE:


#include <stdio.h>
#include <stdlib.h>

struct node
{
int data;
struct node *right_child;
struct node *left_child;
};

struct node* search(struct node *root, int x)


{
if(root==NULL || root->data==x)
return root;
else if(x>root->data)
return search(root->right_child, x);
else
return search(root->left_child,x);
}

struct node* find_minimum(struct node *root)


{
if(root == NULL)

DEPT OF INFORMATION TECHNOLOGY, MJCET XXIX


return NULL;
else if(root->left_child != NULL)
return find_minimum(root->left_child);
return root;
}

struct node* new_node(int x)


{
struct node *p;
p = malloc(sizeof(struct node));
p->data = x;
p->left_child = NULL;
p->right_child = NULL;

return p;
}

struct node* insert(struct node *root, int x)


{

if(root==NULL)
return new_node(x);
else if(x>root->data)
root->right_child = insert(root->right_child, x);
else
root->left_child = insert(root->left_child,x);
return root;
}

struct node* delete(struct node *root, int x)


{

if(root==NULL)
return NULL;
if (x>root->data)
root->right_child = delete(root->right_child, x);
else if(x<root->data)
root->left_child = delete(root->left_child, x);
else
{

if(root->left_child==NULL && root->right_child==NULL)


{
free(root);
return NULL;
}
DEPT OF INFORMATION TECHNOLOGY, MJCET XXX
else if(root->left_child==NULL || root->right_child==NULL)
{
struct node *temp;
if(root->left_child==NULL)
temp = root->right_child;
else
temp = root->left_child;
free(root);
return temp;
}

else
{
struct node *temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, temp->data);
}
}
return root;
}

void inorder(struct node *root)


{
if(root!=NULL)
{
inorder(root->left_child);
printf(" %d ", root->data);
inorder(root->right_child);
}
}

int main()
{
/*
20
/ \
/ \
5 30
/ \ /\
/ \ / \
1 15 25 40
/ \
/ \
9 45

DEPT OF INFORMATION TECHNOLOGY, MJCET XXXI


/ \ /
/ \ /
7 12 42
*/
struct node *root;
root = new_node(20);
insert(root,5);
insert(root,1);
insert(root,15);
insert(root,9);
insert(root,7);
insert(root,12);
insert(root,30);
insert(root,25);
insert(root,40);
insert(root, 45);
insert(root, 42);

inorder(root);
printf("\n");

root = delete(root, 1);


/*
20
/ \
/ \
5 30
\ /\
\ / \
15 25 40
/ \
/ \
9 45
/ \ /
/ \ /
7 12 42
*/

root = delete(root, 40);


/*
20
/ \
/ \
5 30
\ /\
\ / \
15 25 45

DEPT OF INFORMATION TECHNOLOGY, MJCET XXXI


I
/ /
/ /
9 42
/ \
/ \
7 12
*/

root = delete(root, 45);


/*
20
/ \
/ \
5 30
\ /\
\ / \
15 25 42
/
/
9
/ \
/ \
7 12
*/
root = delete(root, 9);
inorder(root);
/*
20
/ \
/ \
5 30
\ /\
\ / \
15 25 42
/
/
12
/
/
7
*/
printf("\n");

return 0;
}

OUTPUT:

DEPT OF INFORMATION TECHNOLOGY, MJCET XXXI


II
VIVA QUESTIONS
1. Differentiate between BST and complete BST
2. What are the properties of BST
3. How many nodes will be there in given nth level.

DEPT OF INFORMATION TECHNOLOGY, MJCET XXXI


V
DEPT OF INFORMATION TECHNOLOGY, MJCET XXX
V

You might also like