Вы находитесь на странице: 1из 40

1

DS

DATA STRUCTURES
Introduction :

In Data Structures, Data is a combination of numeric & non-numeric values . Structure is representation of data in meaningful order . Data structures are used to represent numeric & non numeric values in a meaningful order . ( Or) The way of arranging the data in a specific order in the memory is called DS. Data structures are divided into mainly Two types . 1. Primitive Data structures . 2. Non primitive Data structure
DATA STRUCTURE

PRIMITIVE

DS

Non Primitive DS

Ex: int ,float,char


LINEAR DS
NON LINEAR DS

Ex: Arrays, Stacks ,Queues , Linked list

Ex: Trees , Graphs

Primitive Data Structure : In primitive DS the features are access directly from the system. The primitive Data structures are int , float , char . Non primitive Data Structure : In Non primitive DS the features are access indirectly from the system .The non primitive data structures are Arrays ,stacks, queues , trees, linked lists .. The non Primitive DS are divided into Two types . They are 1. Linear Data Structures
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

2. Non linear Data Structure Linear Data Structure: In this all elements are arranged in a Sequential order. Ex: Arrays ,stacks ,queues, Linked lists . In DS the linear data structures are again divided into two types. They are 1 . Linear Array 2 . Linear Linked List In linear array the relation ship between the elements is maintained by using the sequential memory locations. In linear Linked list the relation ship between the elements maintained by using pointers (or) Links Non Linear Data Structure: In this all elements are arranged in a Non Sequential order. (or) Hierarchical Order . Ex: Trees , Graphs .

Data Structure Operations

DS Provides 7 types of Operations for developing the applications .(or) arrange the data in a specific Order. They are 1. Creation 2. Insertion 3 . Traversing 4. Deletion 5. Searching 6. Sorting 7. Merging 1.Creation: Creation refers to create an array (List) and also to read elements into that array (or) List. 2.Insertion : It means insert a new element at a particular position into the list . 3.Traversing: It means to display each and every element in the list. 4.Deletion : It means delete an element from the list . 5.Searching: It means searching(or) finding a particular element in the List . 6.Sorting : It means arrange the elements in the particular order ,that means either in ascending or descending order. 7.Merging: It means combing the two sorted lists into a single list.

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

Linear Array
Linear Array is a collection of Homogeneous values (similar type of values) and also store in sequential memory location. To develop the applications by using the linear array the following operations are used. Creation : It refers to create an array and also read the elements into that array (or) List. Algorithm: Step 1: Start Step 2: Create an array Step 3: Read elements into an Array Step 4: Stop Traversing : It means display each and every element in the list Algorithm: Step 1: Start Step 2: Create an array Step 3: Read elements into an Array Step 4: Display the elements from the List Step 5: Stop Insertion : It means insert a new element at a particular position into the list. If you want to insert a new element at the end of the list (or) array it is very is easy . But if you want insert an element in the middle of an array it is very difficult because the elements are have to move to next locations. Algorithm: Insertion ( a [ ] ,ele ,pos) Step 1: Start Step 2: set i = 4 Step 3: Repeat the steps 4 , 5 while( i > = pos) Step 4: a[ i+1 ] =a[ i ] Step 5: i= i -1 Step 6: a[ pos ] =ele Step 7: stop

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

4
Ex: pos =2 10 20 (0) (1) 10 (0) 10 (0) 20 (1) 20 (1) ele =25 30 40 (2) (3) 30 (2) 30 (2) 40 (3) 40 (3) 30 (3) 30 (3) 50 (4) 50 50 (4) (5) 40 50 (4) (5) 40 50 (4) (5)

DS

10 20 30 (0) (1) (2) a[ pos] =ele = 25 10 20 25 (0) (1) (2)

40 50 (4) (5)

Deletion: It means delete a element at a particular position from the list. If you want to delete an element at the end of the list (or) array it is very is easy . But if you want to delete an element in the middle of an array it is very difficult because the elements are have to move to previous locations. Algorithm: Deletion ( a [ ] ,pos) Step 1: Start Step 2: set i = pos Step 3: Repeat the steps 4 , 5 while( i < 4) Step 4: a[ i ] =a[ i + 1 ] Step 5: i= i +1 Step 6: stop Searching : It means searching(or) finding a particular element in the list .That means we can find out that element is in the list or not . In searching we have two methods . They are i) Linear search ii) Binary Search
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

Linear search : In this we can search for the given element is in the list (or) not in sequential order from the beginning of the list ending of the list. If the element is found then it display the element position otherwise it display element is not found . Algorithm: Linear search ( a [ ] ,ele) Step 1: Start Step 2: set i = 0, flag = 1 Step 3: Repeat the steps 4 , 5 while( i < 5) Step 4: if (a[ i ]==ele) then print i and flag = 0 Step 5: i= i +1 Step 6: if( flag) then print element is not found Step 7: stop Ex: 10 (0) Ele i=0 i=1 i=2 i=3 20 (1) 30 (2) 40 (3) 50 (4)

= 40 (10==40) (20==40) (30==40) (40==40)

pos = 3

Binary Search: In this first we find out the mid value based on the low and high values .In this low value is beginning of the Array position and high value is ending of the Array position. Now we check the condition , search element is mid element or Not , if it is true then display the element position. Otherwise we check another condition ,the search element is less than the mid element then our search with Lower half (or) if the search element is greater than the mid element then our search with higher half. This process will continue until the search element is found.

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

Algorithm: Binary search ( a [ ] ,ele) Step 1: Start Step 2: set i = 0, low=0,high =4,flag=1 Step 3: Repeat the steps 4 , 5,6 &7 while( i <= 4) Step 4: mid = (low+high) / 2 Step 5: if ( ele == a[ mid ]) then print mid and flag =0 Step 6: if( ele > a [ mid ]) then low = mid + 1 Step 7: if( ele < a [ mid ]) then high = mid - 1 Step 8: if( flag ) then print given element is not found Step 7: stop Ex: 10 20 30 40 50 (0) (1) (2) (3) (4) Search Ele = 40 Low =0 ; high =4 Mid = low+ high / 2 0+ 4 / 2 = 2 Ele = a[mid] (40==30) Ele > a[mid] (40 > 30 ) then low = mid + 1 2 + 1 =3 Mid = low + high / 2 3 +4 / 2 = 7 /2 = 3 If( ele == a[mid]) ( 40 == 40) Position = 3 Sorting : It means arrange the elements in the particular order ,that means either in ascending or descending order. In sorting we have different methods techniques for arranging the elements in particular order . They are Bubble sort Interchanging sort Selection sort Insertion sort Quick sort

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

LINKED LIST
Linked List is a collection of linear data items and also it is a flexible dynamic data structure. We can insert (or) delete the element at run time. Linked list are 3 types . Singly Linked List Doubly Linked List Circular Linked List Difference b/w Array & Linked List: Array Array is collection of Homogeneous elements . We can easily identify a particular element in the list . It is a Static Data Structure. Here size is fixed. Linked List Linked list a collection of linear data items. It is not easy to identify a particular element in the list It is a dynamic Data Structure . Here size is not fixed. It is a collection of linear data items. In this an element is referred as a node. A node contains Two fields ,they are Data item & Link . Data Item contain actual value and the link contains next node address. In this we use a special pointer called Head ,contains first node address .It means in the single linked list first node address will be store in the Head pointer. Last node Link contains the NULL pointer. The representation of Single linked list is as follows Node 1 head (100)
10 101 20

Singly Linked List

Node 2
102

Node 3
30 NULL

100

101

102

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

Structure of a node : struct node { int data ; node * link ; }; We can develop the single linked list by using the following operations . 1. Creation 2. Traversing 3. Insertion 4. Deletion 1.Creation: To create a linked list first we check the condition weather the head is null or not . If the head is null then create a new node and assign that address to the head .Otherwise create a new node and assign that address to the previous node link . Algorithm: Creation Step 1: Start Step 2 : if ( Head == null) p = new node p data = num p link = NULL head = p Step 3 : else t = new node t data = num t link = NULL p link = t p=t Step 4 : Stop 2.Traversing : In this we display each and every element in the list. In single linked list traversing can possible in only one direction that is from head (first) to last element .
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

DS

Algorithm: Traversing Step 1: Start Step 2: set a= head Step 3: Repeat the steps 4 , 5 while( a ! =NULL) Step 4: print a data Step 5: set a= a link Step 6: Stop 3.Inseetion : In the single linked list we can insert the element in three ways. They are i. Insert the element at the beginning of the list ii. Insert the element at the end of the list iii. Insert the element at after specified node. Insert the element at the beginning of the list: If we want to insert the element at the beginning of the list then we create a new node and assign that new node address to the head and also establish link to the next node. Before Insertion Node 1 Node 2 Node 3 head 10 102 20 103 30 NULL (101) 101 102 103 After Insertion Node 1 Node 2 Node 3 Node 4 head 40 101 10 102 20 103 30 NULL (104) 104 101 102 103 Algorithm : Insert beginning(num) Step 1: Start Step 2: t = new node Step 3: t data =num Step 4: t link =head Step 5: head =t Step 6: Stop

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

10

DS

Insert the element at the end of the list: To insert the element at end of the list we can create a new node and assign that node address to previous node link and assign Null pointer to the link. Before Insertion Node 1 Node 2 Node 3 head 10 102 20 103 30 NULL (101) 101 102 103 After Insertion Node 1 Node 2 Node 3 Node 4 head 10 102 20 103 30 104 40 NULL (101) 101 102 103 104 Algorithm : Insert ending(num) Step 1: Start Step 2: t = new node Step 3: t data =num Step 4: t link =NULL Step 5: p link = t Step 6: p =t Step 7: Stop Insert the element at the after specified node: To insert an element after a specified node first we check the weather the node is in the list (or) not. If the specified node is found then that node points to the new node element and the node element points to the next node . Other wise display element is not found Before Insertion Node 1 head (101)
10 102 20

Node 2
103

Node 3
30 NULL

101

102

103

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

11

DS

After Insertion Node 1 Node 2 Node 3 Node 4 head 10 102 20 103 40 103 30 NULL (101) 101 102 104 103 Algorithm : Insert after (ele ,num) Step 1: Start Step 2: set a = head Step 3: repeat the steps 4 & 5 while (a ! =NULL) Step 4: if( ele = =a data) then t= new node t data =num t link = a link a link = t Step 5: else a= a link Step 6: Stop Deletion : If we want to delete the element from the list we can follow two methods. 1. If the deleted element is first element , then assign second node address to the head. 2. If the deleted element is middle element then assign next node address to the previous node . Before deletion b head (101)
10 102 20 103 30 104 40 NULL

deleted node

101 After deletion head (101)


10 102

102

103

104

20

104

40

NULL

101

102

104 Dept.of Computer Science

SSN DEGREE COLLEGE :: ONGOLE

12

DS

Algorithm : deletion(num) Step 1: Start Step 2: set a = head Step 3: if (a data = =num) then head = a link , delete a ; Step 4: else Set b=a , set a= a link Step 5: repeat the steps 6 & 7 while ( a ! =Null) Step 6: : if (a data = =num) then b link = a link delete a ; Step 7: else Set b=a a= a link Step 8 : stop *****

STACK
Stack is a linear data Structure .It is a collection of order data. In this insertion & deletion of an element can be done at the same end (only one end). The basic behavior of stack is LIFO (Last in First Out) In the Stack most recent element is called first element .Stack is a static (or) dynamic data structure .It is very use full to solve the air thematic expressions ,memory management operation. In Stack recently entered element is treated as a Top element. If you want to insert an element in to the stack , called as PUSH operation. If you want to delete an element from the stack , called as POP operation. Visit or display the element in the stack is called Traverse Operation . If we Push more elements than the stack size ,then it is called as Stack is over flow SSN DEGREE COLLEGE :: ONGOLE Dept.of Computer Science

13

DS

If we want to delete the element from empty Stack ,then it is called as Stack is Under flow Stack can be represented in two ways . i. Linear stack ii. Linked stack

Linear Stack
The Stack that is implemented by using Arrays is called Linear stack. The representation of Linear Stack is as follows . POP D C B A STACK PUSH TOP

In the above representation Stack contains Four elements like A,B,C,D . In that D is a top element because it is a recently entered element. The following are the operation on the linear Stack. They are 1. Push 2. Pop 3. Traverse 1.Push: In Stack Push is nothing but Insertion . That means Inserting an element in the Stack is called Push operation . In this first we check the condition either Stack is full or not . If the Stack is full then it display the message as Stack is over flow . If the stack is not full we can insert an element into the stack by increasing the Top position and assigning that number at Top position.

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

14

DS

Algorithm : Push (num) Step 1: Start Step 2: if( top = size -1) then print Stack is Over Flow Step 3: else set top =top + 1 set stack[ top ] = num Step 4: End

2. Pop:

In stack pop is nothing but deletion . That means deletion of an element from the stack is called Pop operation. In this first we check the condition either Stack is empty or not. If it is empty then display the message as stack is empty or Stack is under flow . If it is not empty then delete the element in the Top position and decrease top position. Algorithm : Pop (num) Step 1: Start Step 2: if( top = = -1) then print Stack is empty Step 3: else delete stack [top ] set top =top -1 Step 4: End

3.Traverse :

Display each and every element from the stack is called traverse operation . In this we display the elements from the first element (top) to the last element. Algorithm : Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Traverse (num) Start Set i = top Repeat the steps 4 & 5 until while ( i >=0) print stack[ i ] i =i -1 End Dept.of Computer Science

SSN DEGREE COLLEGE :: ONGOLE

15 Linked Stack :

DS

The Stack that is implemented by using links or pointers is called Linked Stack. The representation of Linked Stack is as follows . POP 204 203 202 201 PUSH

40 203 TOP 30 202 20 201 10 Null Linked STACK In the above , stack contains four Nodes like 1,2,3,4 .On that the fourth node is the top node, because it is recently entered element. The following are the operation on the linear Stack. They are 1. Push 2. Pop 3. Traverse 1.Push: In Stack Push is nothing but Insertion . That means Inserting an element in the Stack is called Push operation . In this first we check the condition either Stack is empty or not . If the Stack is empty then insert a new node and assign the NULL pointer to the link. Other wise insert a new node and assign the next node address to the link . Algorithm : Push (num) Step 1: Start Step 2: if( top = =Null) then t = new node t data =num t link = Null set top =t Step 3: else t = new node
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

16
t data =num t link = top set top = t Step 4 : End

DS

2.Pop :

In stack pop is nothing but deletion . That means deletion of an element from the stack is called Pop operation. In this first we check the condition either Stack is empty or not. If it is empty then display the message as stack is empty or Stack is under flow . If it is not empty then delete the Top node and assign next node address to the top. Algorithm : Pop (num) Step 1: Start Step 2: if( top = = Null) then print Stack is empty Step 3: else Set t = top top =top link delete t Step 4: End 3.Traverse Display each and every element from the stack is called traverse operation . In this we display the elements from the first element (top) to the last element. Algorithm : Traverse (num) Step 1: Start Step 2: set t=top Step 3: repeat the steps 4 & 5 while( t != Null) Step 4 : print t data Step 5: t = t link Step 6: End

*****

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

17

DS

QUEUES
Queue is a linear data Structure .It is a collection of order data. In this elements are inserted at one end and delete at anther end . The basic behavior of Queue is FIFO (First in First Out). We can develop the queue by using arrays or linked list . Queues are used in many places like reservation counters, cinema theaters. In queue elements are inserted at Rare end. In queue elements are deleted from front end. In queue most recently entered element is treated as Last element . (rare element) If we insert more elements than the queue size ,then it is called as Queue is over flow If we want to delete the element from empty Queue ,then it is called as Queue is Under flow Queue can be represented in two ways . 1. Linear Queue 2. Linked Queue

Linear Queue
The Queue that is implemented by using Arrays is called Linear Queue. The representation of Linear Queue is as follows Front end 1 2 3 4 Rare end

Delete Insert Front rare In the above representation Queue contains Four elements like 1,2,3,4 In that 1 is a front element(first element) .And 4 is rare element (last element) The following are the operation on the linear Queue. They are 1. Insertion 2. Deletion 3. Traverse
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

18

DS

Insertion: For inserting the elements into the queue first we have to check the condition either the Queue is full or not. If it is full then display the massage as Queue is Over Flow .Other wise increase the rare position and place the element at rare point. Algorithm : Insertion ( ) Step 1: Start Step 2: if ( rare ==size -1) then Print queue is over flow Step 3: Else Set rare = rare +1 Queue [ rare ] = num Step 4: End Deletion : For deleting the element from the queue first we have to check the condition either the Queue is Empty or Not . If the Queue is empty then display the message as Queue is under flow or empty. Otherwise increase the front position and delete the element. Algorithm : Deletion ( ) Step 1: Start Step 2: if ( front == rare) then Print queue is Under Flow Step 3: Else Set front = front +1 delete Queue [ front ] Step 4: End Traversing: Display each and every element from the queue is called traverse operation . In this we display the elements from front to rare . Algorithm : Traverse ( ) Step 1: Start Step 2: set i = front +1 Step 3: repeat the steps 4 & 5 while ( i <= rare ) Step 4 : print Queue [ i ] Step 5 : i = i+1 Step 6 : End
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

19

DS

Linked Queue
The Queue that is implemented by using linked list is called Linked Queue. It is a dynamic Data Structure. The representation of Linked Queue is as follows Front=101 102 Delete Front End 101 1 2 103 3 104 4 null Insert rare end

In the above representation Linked Queue contains Four elements like 1,2,3,4. In that 1 is a front Node(first node) .And 4 is rare node (last node) The following are the operation on the linked Queue. They are 1. Insertion 2. Deletion 3. Traverse Insertion: For inserting the elements into the queue first we have to check the condition either the Queue is empty or not. If it is empty then crate a new node and assign Null pointer to that node and also assign that node address to the front & rare . Other wise create a new node and assign that address to the previous node link and rare . Algorithm : insertion (num) Step 1: Start Step 2: if( rare = =Null) then t = new node t data =num t link = Null front = rare = t Step 3: else t = new node t data =num t link = Null
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

20

DS

rare link = t set rare = t Step 4 : End Deletion : For deleting the element from the queue first we have to check the condition either the Queue is Empty or Not . If the Queue is empty then display the message as Queue is under flow or empty. Otherwise delete the first node and assign second node address to the front. Algorithm : Deletion ( ) Step 1: Start Step 2: if( front = = Null) then print Queue is empty Step 3: else Set t = front front =front link delete t Step 4: End Traverse : Display each and every element from the queue is called traverse operation . In this we display the elements from front to rare . Algorithm : Traverse ( ) Step 1: Start Step 2: set t=front Step 3: repeat the steps 4 & 5 while( t != Null) Step 4 : print t data Step 5: t = t link Step 6: End

*****

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

21

DS

Circular Queue
Circular queue is a Queue ,in which the data inserted at one end(rare end) and deleted at another end (front end) with circularly. The representation of circular queue is as follows .
50 60 10

4040
30 20

In the circular Queue there are 6 elements like 10 ,20,30,40,50,60. The main difference between queue and circular Queue is first (front) location is always followed the last (rare) location . Circular Queue can be represented in two ways . i. Linear circular queue ( by using arrays ) ii. Linked circular Queue (by using pointer or link ) *****

DOUBLE LINKED LIST


In this we can insert and delete the elements at run time. In this Size is not fixed. In double linked list elements are referred as nodes. Every node contains Three fields. They are i. Left link - (It contains previous node address) ii. Data - (It contains actual value) iii. Right link ( It contains next node address) Structure of node : Struct node { int data ; node * r link ,*l link; }
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

22
Node Ex:
101 10 103

DS
t t t l link =101 data = 10 r link = 103

The representation of Double linked list is as follows head (101)


Null 10 102 101 20 103 102 30 Null

tail (103)

102 103 In the above diagram we use two pointers namely head and tail. Head contains address of the first node ands tail contains last node address. Left link of the first node contains Null pointer and right link of the last node is always Null . The following are the operations of double linked list 1. Creation 2. Traverse 3. Insertion 4. Deletion Creation In this first we check the condition either the Head is null or Not. If it is null then create a node and assign that node address to the Head & Tail .Otherwise create a new node and assign that address to the Tail. Algorithm : creation (num) Step 1: Start Step 2: if( head = =Null) then t = new node t L link =NULL t data = num t r link=NULL head = tail = t Step 3: else t = new node t L link =tail t data = num SSN DEGREE COLLEGE :: ONGOLE Dept.of Computer Science

101

23
t r link=NULL tail = t Step 4: end

DS

Traverse :

In doubly Linked list Traverse can possible in two directions. 1. Forward Traverse 2. Back word Traverse Forward Traverse: In forward traverse we display each and every element from first element to last element. ( head to tail) Algorithm : f traverse ( ) Step 1: Start Step 2: set a =head Step 3: Repeat the steps 4 & 5 while ( a!= null) Step 4: Print a data Step 5: a= a r link Step 6: Stop Back word Traverse: In back word traverse we display each and every element from last element to first element. ( tail to head) Algorithm : b traverse ( ) Step 1: Start Step 2: set a =tail Step 3: Repeat the steps 4 & 5 while ( a!= null) Step 4: Print a data Step 5: a= a L link Step 6: Stop Insertion: In double linked list we can insert an element in to the list in three way like at the begining , ending , after a specified node. Algorithm : insertion at begin (num ) Step 1: Start Step 2: t=new node Step 3: t data = num Step 4: t L link = Null
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

24
Step 5: Step 6: Step 7: Step 6: head (101) head (100)
Null

DS

t rlink= head head L link =t head =t Stop Before Insertion


10 102 101 20 103 102 30 Null

tail (103)

101
Null 5 101

102 103 After Insertion ( at begin)


100 10 102 101 20 103

100

101

102
102 30 Null

tail (103)

Deletion :

103

To delete an element from the list we first search for the deleted element .If it is in the list then delete that node and assign previous node address to the next node left link and next node address to the previous node right link. Algorithm : deletion (ele ) Step 1: Start Step 2: set a =head Step 3: set b = a L link Step 4: set c = b r link Step 5: if ( ele == a data ) then c L link =b b r link =c delete a Step 6: else a=a rlink b= a L link ; c= a r link Step 7 : End Dept.of Computer Science

SSN DEGREE COLLEGE :: ONGOLE

25

DS

TREES
Tree is a non linear data structure. That represents hierarchical relationship between the elements . In this each element referred as node. All the nodes are connect with edges . Every tree must be start with a single node , called as root . The representation of tree is
A

ROOT EDGE PARENT CHILD

LEAF

Leaf or Terminal node : In a tree if a node does not contains child nodes , that is called leaf or terminal node. In the above tree H ,I, J, G are the leaf nodes Non terminal node : In a tree if a node contains at least one child node , that node is called non terminal node Ex: From the above A,B,C,D,E,F,G are non terminal nodes

Edge :

In a tree the link between one node to another node is called Edge . In a tree contains N node there are N -1 edges in that tree. In the above tree there are 10 nodes and 9 edges . Path: Path is a way to connect or to display the list of nodes from first to last node Ex: A H ( A B D H)
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

26
Length :

DS

Length means the number of edges in particular path. Ex: : A H ( A B D H) length = 3 A G ( A C H) length = 2 Degree: It means number of child nodes for a particular node. Ex: Degree (A) =2 Degree(F) =1 Order: It means the highest degree of the tree. In the above example the order of the tree is 2 Height: It means the highest length of the tree . In the above example the height of the tree is 3 Binary tree : It is a tree ,in which each node contains two child nodes except leaf node. Ex:
A

Skewed Binary Tree: A binary tree which has only one side nodes either left or right side is called a Skewed Binary tree. Binary Search tree : It is one type of binary tree. In this left sub tree nodes all are less than the root node and right sub tree node all are greater than the root node.

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

27
Ex: 10,5,3,8,15,20,17,22
10 0 5 15

DS

8 17

20

22

Binary search tree can be c represented in two ways . 1. Sequential representation 2 . Linked representation Sequential representation : We can develop the this by using the arrays. It is also called static data structure ,because array size is fixed . In this we easily find out the address of the child node and parent node. Linked representation : We can develop the this by using the links (or) pointers. It is also called dynamic data structure . In this we can insert or delete any number of elements at runtime , because size is not fixed.

Operations on Binary Search Tree:

In binary search trees we can perform three types of operations ,such as 1 . Creation 2. Traverse 3. Deletion Creation : To create the binary search tree first we have to check the condition that the tree is empty or not . If it is empty then create the root node, otherwise compare the inserted element is less than or greater than the root element . Dept.of Computer Science

SSN DEGREE COLLEGE :: ONGOLE

28

DS

If inserted element is less than the root then insert that left side of the root ,other wise insert right side of the root node. Algorithm : creation (num ) Step 1: Start Step 2: if ( root == Null) t = new node t data = num t lptr = t rptr = NULL root = t Step 3 : else Set p =root a) Repeat the steps b ,c, d, e while ( p ! =NULL) b) Set r =p c) if ( num < p data ) then p=p lptr d) else if ( num > p data ) then p=p rptr e) else print duplication values are not allowed Step 4: t = new node Step 5: t data = num Step 6: t lptr = t rptr = NULL Step 7 : if ( num < r data ) then r lptr = t Step 8: else r rptr = t Step 9 : End

Traversing Techniques :

1. Pre order :

In binary search tree we can traverse 1. Pre order 2. In order 3. Post order

in three ways like

In pre order traverse we first visit Root node , left node and then right node . Method : ROOT , LEFT , RIGHT Dept.of Computer Science

SSN DEGREE COLLEGE :: ONGOLE

29
Algorithm : Pre order ( root) Step 1: Start Step 2: if ( root ) then Print root data Preorder ( root lptr ) Preorder ( root rptr ) Step 3 : End

DS

2. In order :
In Inorder traverse we first visit left node , root node and then right node . Method : LEFT , ROOT , RIGHT Algorithm : In order ( root) Step 1: Start Step 2: if ( root ) then In order ( root lptr ) Print root data In order ( root rptr ) Step 3 : End

3. Post order :
In Post order traverse we first visit left node , right node and then root node . Method : LEFT , RIGHT , ROOT Algorithm : Post order ( root) Step 1: Start Step 2: if ( root ) then Post order ( root lptr ) Post order ( root rptr ) Print root data Step 3 : End

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

30
Deletion:

DS

For deleting an element from the Binary search Tree we can follow the steps . If the node a leaf node then it can be deleted immediately . If the deleted node has left child node then replace the deleted node with left child node and finally delete the left child . If the deleted node has right child node then replace the deleted node with right child node and finally delete the right child . If the deleted node has two child nodes then replace the deleted node with the smallest data of right sub tree and recursively delete that smallest data. Algorithm : deletion ( root , num) Step 1: Start Step 2: if( root == NULL) then print element not found Step 3: else if ( num < root data ) then goto Left sub tree to delete Step 4: else if ( num > root data ) then goto Right sub tree to delete Step 5: else if ( num == root data ) then a) Replace the node with the smallest element in right sub tree. b) Perform Recursive deletion Step 6: else temp = root if (root lptr == NULL) then root= root rptr else root = root lptr Step 7: End

*****

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

31
Graph :

DS

GRAPHS
A graph is a set of nodes . These node are connected by Edges. A graph can be represented by G = ( V,E ) Adjacent Nodes : If two nodes are connected by an edge then those two nodes are called Adjacent nodes . Directed Graph: It is a graph , which is developed by only the directed edges.That means there is some direction. Ex:
A B

Undirected Graph: It is a graph ,which is developed by only the un directed edges .That means there is no particular direction. Ex:
A B

Mixed Graph: If a graph contains both directed and Undirected edges is called mixed graph Ex:
A B

Isolated Graph: If node is not connected by any edge is called an isolated node ,that graph is called Isolated Graph.
SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

32
Ex:
A B E C D

DS

Null Graph: A graph which contains only isolated nodes is called Null Graph . Ex:
A B E C D

Loop: In a graph an edge is starting and ending with same node called loop. Ex:
A B

In degree: In a graph number of edges ends at that node is called Indgree Ex: A B In degree of ( A ) = 1 In degree of (B) = 1 In degree of( C) =1 C D In degree of ( D) =2 Out degree: In a graph number of edges starts from that node is called Out dgree Ex: A B Out degree of ( A ) = 2 Out degree of (B) = 1 Out degree of( C) =1 C D Out degree of ( D) =1 Total degree: SSN DEGREE COLLEGE :: ONGOLE Dept.of Computer Science

33

DS

In a graph some of In degree & Out degree of a node called total degree . Ex: A B Total degree of ( A ) = 3 Total degree of (B) = 2 Total degree of( C) =2 C D Total degree of ( D) =3 Source Node : In a graph , a node whose InDegree is Zero is called Source Node. Ex:
A B

Here A is an Source Node


C D

Sink Node : In a graph , a node whose Out Degree is Zero is called Sink Node. Ex:
A B

Here D is an Sink Node


C D

Waited Graph : It is a graph that contains a number associated with each edge is called waited graph Ex: 1
A B

3
C

2 4
D

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

34

DS

Graph Representation Methods The Graph can be represented in two methods . They are 1. Sequential Representation 2. Linked Representation Sequential Representation : To develop the graph by using Two dimensional Array is called sequential representation . It is represented by a matrix, called as Adjacent matrix. Ex: Directed Graph Adjacent Matrix Ex:
A B

A A
C D

B 1 0 0 1

C 0 1 0 1

D 0 1 0 0

0 0 1 1

B C D

Linked Representation To develop the graph by using links or pointers is called linked representation .In this we first create Two types of lists like node list and adjacent list. Ex: Graph Ex:
A B

Node Adjacent List A B C D B D A A,C Dept.of Computer Science

SSN DEGREE COLLEGE :: ONGOLE

35

DS

Graph Traversal Methods


Traversal means displaying the list of elements from the graph . In this graph can traverse in two methods . 1. DFS ( Depth first search ) 2. BFS (Breadth first search) (Depth first search) In this we can follow Stack operation . That means Last in first out ( LIFO) and recently entered element treated as Top element. Algorithm : Step 1: Start Step 2: Initialized all nodes to ready state . Step 3: Begin with Top Node and push into the Stack. Step 4: Repeat the steps 5 & 6 while stack is empty . Step 5: Pop the top node of the stack and print it. Step 6: Push All the Adjacent nodes of the deleted (pop) elements which are still in the ready state. Step 7: End Example : The process of DFS traversal is as shown below .
V1

1. DFS

V2

V3

V4

V5

V6

V7

V8

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

36
Step 1 : Step 2: Push V1 into the stack

DS
push

Step 3:

Step 4:

Step 5:

V1 Pop V1 , then print V1 and push all the adjacent nodes of V1 into stack pop push V1 v3 v2 Pop V3 , then print V3 and push all the adjacent nodes of V3 into stack pop push v7 V1,V3 v6 v2 Pop V7 , then print V7 and push all the adjacent nodes of V7 into stack pop push v8 V1,V3 ,V7 v6 v2 Pop V8 , then print V8 and push all the adjacent nodes of V8 into stack pop v5 push v4 V1,V3 ,V7,V8 v6 v2

Step 6: Pop V5 , then print V5 and push all the adjacent nodes of V5 into stack pop push v4 V1,V3 ,V7,V8,V5 v6 v2

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

37

DS

Step 7: Pop V4 , then print V4 and push all the adjacent nodes of V4 into stack pop push v6 v2 Step 8: Pop V6 , then print V6 and push all the adjacent nodes of V6 into stack pop push V1,V3 ,V7,V8,V5,V4,V6 v2 V1,V3 ,V7,V8,V5,V4

Step 9: Pop V2 , then print V2 and push all the adjacent nodes of V2 into stack pop push V1,V3 ,V7,V8,V5,V4,V6,V2 Step 10: Stop Result of traversing node are : V1,V3 ,V7,V8,V5,V4,V6,V2 empty

The Result of the Traversing graph is as follows


V1

V2 V6 V4 V5

V3

V7

V8

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

38

DS

BFS

(Breadth first search)

The BFS traversal follows Queue operation .It means First in First Out (FIFO).In this elements are inserted at one end and delete at anther end . Algorithm : Step 1: Start Step 2: Initialized all nodes to ready state . Step 3: Begin with a Node and insert that node into the Queue. Step 4: Repeat the steps 5 & 6 while queue is empty . Step 5: Delete the front node of the queue and print it. Step 6: Insert All the Adjacent nodes of the deleted elements which are still in the ready state. Step 7: End Example : The process of BFS traversal is as shown below .
V1

V2

V3

V4

V5

V6

V7

V8

Step 1: Insert V1 into the Queue . V1 front insert Rare

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

39

DS

Step 2: Delete V1 from the Queue ,then print V1 and insert all the adjacent nodes of V1 into the Queue. V1 front v2 v3 insert Rare

Step 3: Delete V2 from the Queue ,then print V2 and insert all the adjacent nodes of V2 into the Queue. V1,V2 front v3 v4 v5 insert Rare

Step 4: Delete V3 from the Queue ,then print V3 and insert all the adjacent nodes of V3 into the Queue. V1,V2,V3 front v4 v5 v6 v7 insert Rare

Step 5: Delete V4 from the Queue ,then print V4 and insert all the adjacent nodes of V4 into the Queue. V1,V2,V3,V4 front v5 v6 v7 v8 insert Rare

Step 6: Delete V5 from the Queue ,then print V5 and insert all the adjacent nodes of V5 into the Queue. V1,V2,V3,V4,V5 front v6 v7 v8 insert Rare

Step 7: Delete V6 from the Queue ,then print V6 and insert all the adjacent nodes of V6 into the Queue. V1,V2,V3,V4,V5,V6 front
SSN DEGREE COLLEGE :: ONGOLE

v7 v8

insert

Rare Dept.of Computer Science

40

DS

Step 8: Delete V7 from the Queue ,then print V7 and insert all the adjacent nodes of V7 into the Queue. V1,V2,V3,V4,V5,V6,V7 front v8 insert Rare

Step 9: Delete V8 from the Queue ,then print V8 and insert all the adjacent nodes of V8 into the Queue. V1,V2,V3,V4,V5,V6,V7,V8 front Step 10: Stop Result of traversing node are : V1,V2 ,V3,V4,V5,V6,V7,V8 empty insert Rare

The Result of the Traversing graph is as follows


V1

V2

V3

V4

V5

V6

V7

V8

THE END

GOOD LUCK

SSN DEGREE COLLEGE :: ONGOLE

Dept.of Computer Science

Вам также может понравиться