Documente Academic
Documente Profesional
Documente Cultură
INTRODUCTION:
A data structure in Computer Science is a way of storing and organizing data in a computer‟s
memory or even disk storage so that it can be used efficiently. It is an organization of
mathematical and logical concepts of data. A well-designed data structure allows a variety of
critical operations to be performed, using as few resources, both execution time and memory
space, as possible. Data structures are implemented by a programming language by the data
types and the references and operations provide by that particular language.
DATA AND INFORMATION:
The term data comes from its singular form datum, which means a fact. The data is a fact about
people, places or some entities. In computers, data is simply the value assigned to a variable.
The term variable refers to the name of a memory location that can contain only one data at any
point of time. For example, consider the following statements:
Vijay is 16 years old.
Vijay is in the 12th standard.
Vijay got 80% marks in Mathematics.
Let „name‟, „age‟, „class‟, „marks‟ and „subject‟ be some defined variables. Now, let us assign a
value to each of these variables from the above statements.
Name = Vijay
Class = 12
Age = 16
Marks = 80
Subject = Mathematics
In the above example the values assigned to the five different variables are called data. We will
now see what is information with respect to computers. Information is defined as a set of
processed data that convey the relationship between data considered. Processing means to do
some operations or computations on the data of different variables to relate them so that these
data, when related, convey some meaning. Thus, information is as group of related data
conveying some meaning. In the examples above, when the data of the variables „name‟ and
„age‟ are related, we get the following information:
Vijay is 16 years old.
In the same example, when the data of the variables „name‟ and
„class‟ are related we get another information as
Vijay is in the 12th standard.
Further, when we relate the data of the variables, „name‟, „marks‟, and „subject‟, we get more
information that Vijay got 80% marks in Mathematics. The following figure shows how
information can be drawn from data.
Fig 1.1 : Relation between data and information
Linked List
Tree
Graph
Stack, Queue etc.
All these data structures allow us to perform different operations on data. We select these data
structures based on which type of operation is required. We will look into these data structures in
more details in our later lessons.
Basic Operations
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be performed
on the data structure.
Traversing
Searching
Insertion
Deletion
Sorting
What is Algorithm?
An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain
predefined task. Algorithm is not the complete code or program, it is just the core logic(solution)
of a problem, which can be expressed either as an informal high level description
as pseudocode or using a flowchart.
An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following properties
:
1. Time Complexity
2. Space Complexity
Space Complexity
Its the amount of memory space required by the algorithm, during the course of its execution.
Space complexity must be taken seriously for multi-user systems and in situations where limited
memory is available.
An algorithm generally requires space for following components :
Instruction Space : Its the space required to store the executable version of the program.
This space is fixed, but varies depending upon the number of lines of code in the program.
Data Space : Its the space required to store all the constants and variables value.
Environment Space : Its the space required to store the environment information needed to
resume the suspended function.
Time Complexity
Time Complexity is a way to represent the amount of time needed by the program to run to
completion.
Queue
Queue is work on the principal of First-In-First-Out (FIFO), it means first entered item
remove first. Queue have two end front and rear, from front you can insert element and from
rear you can delete element.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it and then completing
erasing it from memory. Here we shall try to understand basic operations associated with
queues −
enqueue() − add (store) an item to the queue.
dequeue() − remove (access) an item from the queue.
Few more functions are required to make above mentioned queue operation efficient. These are
−
peek() − get the element at front of the queue without removing it.
isfull() − checks if queue is full.
isempty() − checks if queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing
(or storing) data in queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −
peek()
Like stacks, this function helps to see the data at the front of the queue. Algorithm of peek()
function −
begin procedure peek
return queue[front]
end procedure
Implementation of peek() function in C programming language −
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to implement queue, we just check for the rear pointer
to reach at MAXSIZE to determine that queue is full. In case we maintain queue in a circular
linked-list, the algorithm will differ. Algorithm of isfull() function −
begin procedure isfull
if rear equals to MAXSIZE
return true
else
return false
endif
end procedure
Implementation of isfull() function in C programming language −
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
begin procedure isempty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif
end procedure
If value of front is less than MIN or 0, it tells that queue is not yet initialized, hence empty.
Here's the C programming code −
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Enqueue Operation
As queue maintains two data pointers, front and rear, its operations are comparatively more
difficult to implement than stack.
The following steps should be taken to enqueue (insert) data into a queue −
Step 1 − Check if queue is full.
Step 2 − If queue is full, produce overflow error and exit.
Step 3 − If queue is not full, increment rear pointer to point next empty space.
Step 4 − Add data element to the queue location, where rear is pointing.
Step 5 − return success.
Sometimes, we also check that if queue is initialized or not to handle any unforeseen situations.
Dequeue Operation
Accessing data from queue is a process of two tasks − access the data wherefront is pointing
and remove the data after access. The following steps are taken to perform dequeue operation
−
Step 1 − Check if queue is empty.
Step 2 − If queue is empty, produce underflow error and exit.
Step 3 − If queue is not empty, access data where front is pointing.
Step 3 − Increment front pointer to point next available data element.
Step 5 − return success.
Stack
Stack is linear data structure. In stack addition of new data item and deletion of already
existing data item is done from only one end, known as top. Working of stack on the basis
of Last-in-First-out (LIFO) principal, it means last entered item remove first.
peek()
Algorithm of peek() function −
begin procedure peek
return stack[top]
end procedure
Implementation of peek() function in C programming language −
int peek() {
return stack[top];
}
isfull()
Algorithm of isfull() function −
begin procedure isfull
if top equals to MAXSIZE
return true
else
return false
endif
end procedure
Implementation of isfull() function in C programming language −
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
begin procedure isempty
if top less than 1
return true
else
return false
endif
end procedure
Implementation of isempty() function in C programming language is slightly different. We
initialize top at -1, as index in array starts from 0. So we check if top is below zero or -1 to
determine if stack is empty. Here's the code −
bool isempty() {
if(top == -1)
return true;
else
return false;
}
PUSH Operation
The process of putting a new data element onto stack is known as PUSHOperation. Push
operation involves series of steps −
Step 1 − Check if stack is full.
Step 2 − If stack is full, produce error and exit.
Step 3 − If stack is not full, increment top to point next empty space.
Step 4 − Add data element to the stack location, where top is pointing.
Step 5 − return success.
if linked-list is used to implement stack, then in step 3, we need to allocate space dynamically.
Pop Operation
Accessing the content while removing it from stack, is known as pop operation. In array
implementation of pop() operation, data element is not actually removed, instead top is
decremented to a lower position in stack to point to next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space.
A POP operation may involve the following steps −
Step 1 − Check if stack is empty.
Step 2 − If stack is empty, produce error and exit.
Step 3 − If stack is not empty, access the data element at which top is pointing.
Step 4 − Decrease the value of top by 1.
Step 5 − return success.
As per above shown illustration, following are the important points to be considered.
LinkedList contains an link element called first.
Each Link carries a data field(s) and a Link Field called next.
Each Link is linked with its next link using its next link.
Last Link carries a Link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list.
Insertion − add an element at the beginning of the list.
Deletion − delete an element at the beginning of the list.
Display − displaying complete list.
Search − search an element using given key.
Delete − delete an element using given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We Shall learn this with
diagrams here. First, create a node using the same structure and find the location where it has
to be inserted.
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode).
Then point B.next to C
Now the next of the node at left should point to the new node.
Similar steps should be taken if the node being inserted at the beginning of the list. While
putting it at the end, then the second last node of list should point to new node and the new
node will point to NULL.
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation. First,
locate the target node to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target node
−
This will remove the link that was pointing to target node. Now we shall remove to what target
node is pointing.
TargetNode.next −> NULL;
We need to use the deleted node we can keep that in memory otherwise we can simply
deallocate memory and wipe off the target node completely.
Reverse Operation
This operation is a thorough one. We need to make the last node be pointed by the head node
and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now we shall make it to
point to its previous node −
We have to make sure that last node is not the lost node, so we'll have some temp node, which
looks like the head node pointing to the last node. Now we shall make our all our left side nodes
to point to their previous nodes one by one.
Except the node (first node) pointed by the head node, should point to their predecessor and
making them their new successor. The first node will point to NULL.
We'll make the head node to point the new first node by using temp node.
As per above shown illustration, following are the important points to be considered.
Insertion Operation
Following code demonstrate insertion operation at beginning in a doubly linked list.
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty()) {
//make it the last link
last = link;
}else {
//update first prev link
head->prev = link;
}
//point it to old first link
link->next = head;
//point first to new first link
head = link;
}
Deletion Operation
Following code demonstrate deletion operation at beginning in a doubly linked list.
//delete first item
struct node* deleteFirst() {
//save reference to first link
struct node *tempLink = head;
//if only one link
if(head->next == NULL) {
last = NULL;
}else {
head->next->prev = NULL;
}
head = head->next;
//return the deleted link
return tempLink;
}
As per above shown illustrations, following are the important points to be considered.
Last Link's next points to first link of the list in both cases of singly as well as doubly
linked list.
First Link's prev points to the last of the list in case of doubly linked list.
Basic Operations
Following are the important operations supported by a circular list.
insert − insert an element in the start of the list.
delete − insert an element from the start of the list.
display − display the list.
Insertion Operation
Following code demonstrate insertion operation at in a circular linked list based on single linked
list.
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data= data;
if (isEmpty()) {
head = link;
head->next = head;
}else {
//point it to old first node
link->next = head;
Deletion Operation
Following code demonstrate deletion operation at in a circular linked list based on single linked
list.
//delete first item
struct node * deleteFirst() {
//save reference to first link
struct node *tempLink = head;
if(head->next == head){
head = NULL;
return tempLink;
}
void creat()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\nDo you want to creat another : ");
ch=getche();
}while(ch!='n');
}
//------------------------------------------------------------------
void display()
{
struct node *new_node;
printf("\nThe Linked List : n");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->next;
}
printf("NULL");
}
//----------------------------------------------------
void main()
{
clrscr();
creat();
display();
}
//----------------------------------------------------
Introduction to Sorting
Sorting is nothing but storage of data in sorted order, it can be in ascending or descending order.
The term Sorting comes into picture with the term Searching. There are so many things in our
real life that we need to search, like a particular record in database, roll numbers in merit list, a
particular telephone number, any particular page in a book etc.
Sorting arranges data in a sequence which makes searching easier. Every record which is going
to be sorted will contain one key. Based on the key the record will be sorted. For example,
suppose we have a record of students, every such record will have the following data:
Roll No.
Name
Age
Class
Here Student roll no. can be taken as key for sorting the records in ascending or descending
order. Now suppose we have to search a Student with roll no. 15, we don't need to search the
complete record we will simply search between the Students with roll no. 10 to 20.
Sorting Efficiency
There are many techniques for sorting. Implementation of particular sorting technique depends
upon situation. Sorting techniques mainly depends on two parameters. First parameter is the
execution time of program, which means time taken for execution of program. Second is the
space, which means space taken by the program.
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Heap Sort
If next element is smaller than previous element then interchange their positio
Example:
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare
33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
To be precise, we are now showing that how array should look like after each iteration. After
second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that array is completely sorted.
for(i=0;i<no;i++)
{
scanf("%d",&a[i]);
}
printf("\n\n\nData before sorting : ");
for(j=0;j<no;j++)
{
delay(200);
printf(" %d",a[j]);
}
for(i=0;i<no;i++)
{
for(j=0;j<no-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n\n\nData after sorting : ");
for(j=0;j<no;j++)
{
delay(200);
printf(" %d",a[j]);
}
getch();}
Selection Sort Program in C
Selection sort is based of maximum and minimum value. First check minimum value in array
list and place it at first position (position 0) of array, next find second smallest element in
array list and place this value at second position (position 1) and so on. Same process is
repeated until sort all element of an array.
Algorithm
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the
list, appears in the first position of sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in linear
manner.
We find that 14 is the second lowest value in the list and it should appear at the second place.
We swap these values.
After two iterations, two least values are positioned at the the beginning in the sorted manner.
The same process is applied on the rest of the items in the array. We shall see an pictorial
depiction of entire sorting process −
The shell sort, sometimes called the “diminishing increment sort,” improves on the insertion sort
by breaking the original list into a number of smaller sublists, each of which is sorted using an
insertion sort. The unique way that these sublists are chosen is the key to the shell sort. Instead
of breaking the list into sublists of contiguous items, the shell sort uses an increment i,
sometimes called the gap, to create a sublist by choosing all items that are i items apart.
This can be seen in Figure 6. This list has nine items. If we use an increment of three, there are
three sublists, each of which can be sorted by an insertion sort. After completing these sorts, we
get the list shown in Figure 7. Although this list is not completely sorted, something very
interesting has happened. By sorting the sublists, we have moved the items closer to where they
actually belong.
Figure 8 shows a final insertion sort using an increment of one; in other words, a standard
insertion sort. Note that by performing the earlier sublist sorts, we have now reduced the total
number of shifting operations necessary to put the list in its final order. For this case, we need
only four more shifts to complete the process.
Figure 8: ShellSort: A Final Insertion Sort with Increment of 1
#include<conio.h>
int main()
int arr[30];
int i,j,k,tmp,num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
scanf("%d",&arr[k]);
if(arr[k+i]>=arr[k])
break;
else
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
printf("%d\t",arr[k]);
getch();
return 0;
Figure 14: Completing the Partition Process to Find the Split Point for 54
Example:
#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
int main()
{
int arr[30];
int i,size;
printf("Enter total no. of the elements : ");
scanf("%d",&size);
printf("Enter total %d elements : \n",size);
for(i=0; i<size; i++)
scanf("%d",&arr[i]);
qsort(arr,0,size-1);
printf("Quick sorted elements are as : \n");
for(i=0; i<size; i++)
printf("%d\t",arr[i]);
getch();
return 0;
}
void qsort(int arr[20], int fst, int last)
{
int i,j,pivot,tmp;
if(fst<last)
{
pivot=fst;
i=fst;
j=last;
while(i<j)
{
while(arr[i]<=arr[pivot] && i<last)
i++;
while(arr[j]>arr[pivot])
j--;
if(i<j)
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
tmp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=tmp;
qsort(arr,fst,j-1);
qsort(arr,j+1,last);
}
}
The Merge Sort
We now turn our attention to using a divide and conquer strategy as a way to improve the
performance of sorting algorithms. The first algorithm we will study is the merge sort. Merge
sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one
item, it is sorted by definition (the base case). If the list has more than one item, we split the list
and recursively invoke a merge sort on both halves. Once the two halves are sorted, the
fundamental operation, called a merge, is performed. Merging is the process of taking two
smaller sorted lists and combining them together into a single, sorted, new list. Figure 10 shows
our familiar example list as it is being split by mergeSort. Figure 11shows the simple lists, now
sorted, as they are merged back together.
#include<stdio.h>
#include<conio.h>
void merge(int [],int ,int ,int );
void part(int [],int ,int );
int main()
{
int arr[30];
int i,size;
printf("\n\t------- Merge sorting method -------\n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
part(arr,0,size-1);
printf("\n\t------- Merge sorted elements -------\n\n");
for(i=0; i<size; i++)
printf("%d ",arr[i]);
getch();
return 0;
}
void part(int arr[],int min,int max)
{
int mid;
if(min<max)
{
mid=(min+max)/2;
part(arr,min,mid);
part(arr,mid+1,max);
merge(arr,min,mid,max);
}
}
void merge(int arr[],int min,int mid,int max)
{
int tmp[30];
int i,j,k,m=0;
j=min;
m=mid+1;
for(i=min; j<=mid && m<=max ; i++)
{
if(arr[j]<=arr[m])
{
tmp[i]=arr[j];
j++;
}
else
{
tmp[i]=arr[m];
m++;
}
}
if(j>mid)
{
for(k=m; k<=max; k++)
{
tmp[i]=arr[k];
i++;
}
}
else
{
for(k=j; k<=mid; k++)
{
tmp[i]=arr[k];
i++;
}
}
for(k=min; k<=max; k++)
arr[k]=tmp[k];
}
SEARCHING TECHNIQUES
LINEAR OR SEQUENTIAL SEARCH
Introduction
There are many applications requiring a search for a particular element. Searching refers to
finding out whether a particular element is present in the list. The method that we use for this
depends on how the elements of the list are organized. If the list is an unordered list, then we
use linear or sequential search, whereas if the list is an ordered list, then we use binary search.
The search proceeds by sequentially comparing the key with elements in the list, and continues
until either we find a match or the end of the list is encountered. If we find a match, the search
terminates successfully by returning the index of the element in the list which has matched. If
the end of the list is encountered without a match, the search terminates unsuccessfully.
Example:
#include <stdio.h>
#include<conio.h>
#define MAX 10
void lsearch(int list[],int n,int element)
int i, flag = 0;
clrscr();
for(i=0;i<n;i++)
element,i);
flag =1;
break;
if( flag == 0)
int i;
for(i=0;i<n;i++)
scanf("%d",&list[i]);
int i;
for(i=0;i<n;i++)
printf("%d\t",list[i]);
void main()
scanf("%d",&n);
readlist(list,n);
printlist(list,n);
scanf("%d",&element);
lsearch(list,n,element);
}
BINARY SEARCH
Introduction
The prerequisite for using binary search is that the list must be a sorted one. We compare the
element to be searched with the element placed approximately in the middle of the list.
If a match is found, the search terminates successfully. Otherwise, we continue the search for the
key in a similar manner either in the upper half or the lower half. If the elements of the list are
arranged in ascending order, and the key is less than the element in the middle of the list, the
search is continued in the lower half. If the elements of the list are arranged in descending order,
and the key is greater than the element in the middle of the list, the search is continued in the
upper half of the list. The procedure for the binary search is given in the following program.
#include <stdio.h>
#define MAX 10
void bsearch(int list[],int n,int element)
{
int l,u,m, flag = 0;
l = 0;
u = n-1;
while(l <= u)
{
m = (l+u)/2;
if( list[m] == element)
{
printf(" The element whose value is %d is present atposition %d in list\n",element,m);
flag =1;
break;
}
else
if(list[m] < element)
l = m+1;
else
u = m-1;
}
if( flag == 0)
printf("The element whose value is %d is not present in the list\n",
element);
}
void readlist(int list[],int n)
{
int i;
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}
Terms
Following are important terms with respect to tree.
Path − Path refers to sequence of nodes along the edges of a tree.
Root − Node at the top of the tree is called root. There is only one root per tree and one
path from root node to any node.
Parent − Any node except root node has one edge upward to a node called parent.
Child − Node below a given node connected by its edge downward is called its child node.
Leaf − Node which does not have any child node is called leaf node.
Subtree − Subtree represents descendents of a node.
Visiting − Visiting refers to checking value of a node when control is on the node.
Traversing − Traversing means passing through nodes in a specific order.
Levels − Level of a node represents the generation of a node. If root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2 and so on.
keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
We're going to implement tree using node object and connecting them through references.
Node
A tree node should look like the below structure. It has data part and references to its left and
right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted. First
locate its proper location. Start search from root node then if data is less than key value, search
empty location in left subtree and insert the data. Otherwise search empty location in right
subtree and insert the data.
Algorithm
If root is NULL
then create root node
return
endwhile
insert data
end If
Implementation
The implementation of insert function should look like this −
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
Search Operation
Whenever an element is to be search. Start search from root node then if data is less than key
value, search element in left subtree otherwise search element in right subtree. Follow the same
algorithm for each node.
Algorithm
If data found
return node
endwhile
end if
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//not found
if(current == NULL) {
return NULL;
}
return current;
}
}
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot random access a node in tree. There are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally we traverse a tree to search or locate given item or key in the tree or to print all the
values it contains.
Inorder Traversal
In this traversal method, the left left-subtree is visited first, then root and then the right sub-tree.
We should always remember that every node may represent a subtree itself.
If a binary tree is traversed inorder, the output will produce sorted key values in ascending order.
We start from A, and following in-order traversal, we move to its left subtree B.B is also traversed
in-ordered. And the process goes on until all the nodes are visited. The output of in-order
traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Preorder Traversal
In this traversal method, the root node is visited first, then left subtree and finally right sub-tree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-ordered. And the process goes on until all the nodes are visited.
The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm
Postorder Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse left
subtree, then right subtree and finally root.
We start from A, and following pre-order traversal, we first visit left subtree B.B is also traversed
post-ordered. And the process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Representation
BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has
key and associated value. While searching, the desired key is compared to the keys in BST and if
found, the associated value is retrieved.
An example of BST −
We observe that the root node key (27) has all less-valued keys on the left sub-tree and higher
valued keys on the right sub-tree.
Basic Operations
Following are basic primary operations of a tree which are following.
Search − search an element in a tree.
Insert − insert an element in a tree.
Preorder Traversal − traverse a tree in a preorder manner.
Inorder Traversal − traverse a tree in an inorder manner.
Postorder Traversal − traverse a tree in a postorder manner.
Node
Define a node having some data, references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Search Operation
Whenever an element is to be search. Start search from root node then if data is less than key
value, search element in left subtree otherwise search element in right subtree. Follow the same
algorithm for each node.
while(current->data != data){
if(current != NULL) {
printf("%d ",current->data);
//not found
if(current == NULL){
return NULL;
}
}
}
return current;
}
Insert Operation
Whenever an element is to be inserted. First locate its proper location. Start search from root
node then if data is less than key value, search empty location in left subtree and insert the data.
Otherwise search empty location in right subtree and insert the data.
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1){
parent = current;
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;
}
}
}
}
}
Data Structure - Graph Data Structure
A graph is a pictorial representation of a set of objects where some pairs of objects are connected
by links. The interconnected objects are represented by points termed as vertices, and the links
that connect the vertices are callededges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges,
connecting the pairs of vertices. Take a look at the following graph −