You are on page 1of 13

BACHELOR OF COMPUTER

ST
APPLICATIONS (BCA 1 YEAR)
data structure in c
Subject code:
Roll no: 191950040010
Submitted by: SHIVAM PANDEY
Submitted to: SAIDA MAM
 Write short note on the following:
1. Tree
A tree is a nonlinear data structure, compared to arrays,
linked lists, stacks and queues which are linear data
structures. A tree can be empty with no nodes or
a tree is a structure consisting of one node called the root
and zero or one or more sub trees.
• Tree is one of the most powerful and advanced data structures.
• it is a non-linear data structure compared to arrays, linked lists,
stack and queue.
•it represents the nodes connected by edges.

2. Stricly binary tree


In a binary tree, every node can have a maximum of two
children. But in strictly binary tree, every node should have
exactly two children or none. That means every internal
node must have exactly two children. A strictly Binary Tree
can be defined as follows...
A binary tree in which every node has either two or zero
number of children is called Strictly Binary Tree
Strictly binary tree is also called as Full Binary
Tree or Proper Binary Tree or 2-Tree
Strictly binary tree data structure is used to represent
mathematical expressions.

3. Extended binary tree


An extended binary tree is a transformation of
any binary tree into a complete binary tree. This
transformation consists of replacing every null sub tree of
the original tree with “special nodes.” The nodes from the
original tree are then internal nodes. While the “special
nodes” are external nodes.
Long answer type
Question: what is BST? Define its operation with the
help of example.
Answer: Binary search trees (BST), sometimes
called ordered or sorted binary trees, are a particular
type of container: a data structure that stores "items" (such
as numbers, names etc.) in memory. They allow fast
lookup, addition and removal of items, and can be used to
implement either dynamic sets of items or lookup
tables that allow finding an item by its key.

Implementation of Binary Search Tree


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};

void inorder(struct node *root)


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

int main()
{
int n , i;
struct node *p , *q , *root;
printf("Enter the number of nodes to be
insert: ");
scanf("%d",&n);

printf("\nPlease enter the numbers to be


insert: ");

for(i=0;i<i++)
{
p = (struct node*)malloc(sizeof(struct
node));
scanf("%d",&p->data);
p->left = NULL;
p->right = NULL;
if(i == 0)
{
root = p; // root always point to
the root node
}
else
{
q = root; // q is used to
traverse the tree
while(1)
{
if(p->data > q->data)
{
if(q->right == NULL)
{
q->right = p;
break;
}
else
q = q->right;
}
else
{
if(q->left == NULL)
{
q->left = p;
break
}
else
q = q->left;
}
}

}
printf("\n Binary Search Tree nodes in Inorder
Traversal: ");
inorder(root);
printf("\n");

return 0;
}

 Basic Operations
Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-
order manner.
• In-order Traversal − Traverses a tree in an in-order
manner.
• Post-order Traversal − Traverses a tree in a post-
order manner.

Search Operation

Whenever an element is to be searched, start searching


from the root node. Then if the data is less than the key
value, search for the element in the left sub tree.
Otherwise, search for the element in the right sub tree.
Follow the same algorithm for each node.
Algorithm

struct node* search(int data)


{
struct node *current = root;
printf("Visiting elements: ");

while(current->data != data)
{

if(current != NULL)
{
printf("%d ",current->data);

//go to left tree


if(current->data > data)
{
current = current->leftChild;
} //else go to right tree
else
{
current = current->rightChild;
}

//not found
if(current == NULL)
{
return NULL;
}
}
}

return current;
}

Insert Operation

Whenever an element is to be inserted, first


locate its proper location. Start searching
from the root node, then if the data is less
than the key value, search for the empty
location in the left sub tree and insert the
data. Otherwise, search for the empty location
in the right sub tree and insert the data.

Algorithm
void insert(int data)
{
struct node *tempNode = (struct node*)
malloc(sizeof(struct node));
struct node *current;
struct node *parent;

tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

//if tree is empty


if(root == NULL)
{
root = tempNode;
}
else
{
current = root;
parent = NULL;

while(1)
{
parent = current;

//go to left of the tree


if(data < parent->data)
{
current = current->leftChild;

//insert to the left

if(current == NULL)
{
parent->leftChild = tempNode;
return;
}
}
//go to right of the tree
else
{
current = current->rightChild;

//insert to the right


if(current == NULL)
{
parent->rightChild = tempNode;
return;
}
}
}
}
}

You might also like