linked list 3 stack 33 queue 40 - wordpress.com by dhaval s sheth(9033271276) 3 linked list singly...

63
Devloped By Dhaval S Sheth(9033271276) 1 Linked List .................................................................... 3 Singly Linked List .............................................................. 3 Doubly Linked List ............................................................. 6 Explain Doubly Linked list: - ............................................... 6 Circular Singly Linked List ................................................. 10 Order Linked List: ............................................................ 15 Application of Linked list: ................................................. 16 Graphical Representation of Linked List .............................. 19 Graphical Representation of Doubly Linked List ................... 25 Graphical Representation of Circular Linked List .................. 28 Stack ......................................................................... 33 Application of the stack: ................................................... 34 Algorithm for push operation ............................................ 35 Operation: POP I from the stack. ....................................... 36 Operation: PEEP 2nd element from the stack. .................... 37 Queue........................................................................ 40 What is queue? ............................................................... 40 Implementation of Queue: ................................................ 40 Algorithm for simple queue:- ............................................ 42 Insert function: .................................................................... 42 Delete Function .................................................................... 43 Print function ....................................................................... 44 Search Function .................................................................... 44 Update function .................................................................... 44 Circular Queue ................................................................ 45 Advantages of circular queue over simple queue ....................... 45 Disadvantages of Circular queue ............................................. 46 Algorithm for Circular Queue: - ......................................... 46 Insert function: .................................................................... 46 Delete Function: ................................................................... 47 Print Function:...................................................................... 47 Application of Queue:....................................................... 48 Tree .......................................................................... 49 Definition:- ..................................................................... 49 Explain Tree:- ................................................................. 49 Binary Tree:- .................................................................. 50 Representation OR Implementation of Binary Tree............... 51 Operation Of Binary Tree ....................................................... 53 Algorithm for preorder:- ........................................................ 54 Inorder:- ............................................................................. 54 Algorithm For Inorder Traversal of Binary Tree:- ....................... 56

Upload: ngoque

Post on 27-May-2018

257 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 1

Linked List .................................................................... 3 Singly Linked List .............................................................. 3 Doubly Linked List ............................................................. 6 Explain Doubly Linked list: - ............................................... 6 Circular Singly Linked List ................................................. 10 Order Linked List: ............................................................ 15 Application of Linked list: ................................................. 16 Graphical Representation of Linked List .............................. 19 Graphical Representation of Doubly Linked List ................... 25 Graphical Representation of Circular Linked List .................. 28

Stack ......................................................................... 33 Application of the stack: ................................................... 34 Algorithm for push operation ............................................ 35 Operation: POP I from the stack. ....................................... 36 Operation: PEEP 2nd element from the stack. .................... 37

Queue ........................................................................ 40 What is queue? ............................................................... 40 Implementation of Queue: ................................................ 40 Algorithm for simple queue:- ............................................ 42 Insert function: .................................................................... 42 Delete Function .................................................................... 43 Print function ....................................................................... 44 Search Function .................................................................... 44 Update function .................................................................... 44

Circular Queue ................................................................ 45 Advantages of circular queue over simple queue ....................... 45 Disadvantages of Circular queue ............................................. 46

Algorithm for Circular Queue: - ......................................... 46 Insert function: .................................................................... 46 Delete Function: ................................................................... 47 Print Function: ...................................................................... 47

Application of Queue: ....................................................... 48 Tree .......................................................................... 49 Definition:- ..................................................................... 49 Explain Tree:- ................................................................. 49 Binary Tree:- .................................................................. 50 Representation OR Implementation of Binary Tree. .............. 51 Operation Of Binary Tree ....................................................... 53 Algorithm for preorder:- ........................................................ 54 Inorder:- ............................................................................. 54 Algorithm For Inorder Traversal of Binary Tree:- ....................... 56

Page 2: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 2

Postorder Traversal:- ............................................................ 56 Algorithm for Postorder Traversal of Binary Tree. :- .................. 57

Application of Binary Tree:- .............................................. 57 Graphs ....................................................................... 59

Page 3: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 3

Linked List

Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list in which each node contains only one link field pointing the next node in the list. Each node is divided in two parts.

1. Information part. 2. Contains address of next node.

For Example: - Head

NULL Head = Pointer Variable Points to first element (node) of in the list. NULL=It indicates the end of the list. 1. Algorithm for the Creation of the Simple Linked List

Function CREATE(X, FIRST) [Given X, a new element, and FISRT, a pointer to the first element of a Linked linear list whose typical node contains INFO and LINK fields as in above fig, this function inserts X.]

Page 4: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 4

1. [Repeat thru step 5] Repeat while Choice! = ‘n’ 2. [Allocate the New node] NEW NODE 3. [Initialize the fields of new node] INFO (NEW) = X LINK (FIRST) = NEW 4. [Want to insert another node] Read (Choice) 5. [Set the LINK field of Last inserted element] LINK (FIRST) = NULL 6. [Finished] Return Algorithm for the Inserting the element in the Simple Linked List

Function INSERT (TEMPHEAD,KEY) [This Function Insert the element after the node, which have the information field equal to the X. And HEAD is the pointer variable, which points to the first element of the list] 1. [Allocate the Memory for the NEW node] NEW NODE 2. [Set fields of the NEW node] INFO (NEW) = X LINK (NEW) = NULL 3. [Insertion as the first node] LINK (NEW) = TEMPHEAD TEMPHEAD = NEW Return (HEAD) 4. [Save the address of the first element of the list] SAVE = TEMPHEAD 5. [Find the element after which we want to insert the element] Repeat while INFO (LINK (SAVE) ) != NULL 6. [Insert the element] LINK (NEW) = LINK (SAVE) LINK (SAVE) = NEW 7. [Return the address of the first element] Return (TEMPHEAD)

Page 5: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 5

Algorithm for the delete the element from the list Function DEL (TEMPHEAD, KEY) [This Function Delete the Node whose information fields equals to the KEY. And TEMPHEAD is the pointer which points the first element of the list and function returns the address of the first node] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (“Empty List”) 2. [Deletion of the first node] SAVE = TEMPHEAD TEMPHEAD = LINK (TEMPHEAD) Free (SAVE) Return (TEMPHEAD) 3. [Save the address of the first node of the list] SAVE = TEMPHEAD 4. [Find the Node which to be deleted] Repeat while INFO (LINK (SAVE))! = KEY 5. [Delete the node] TEMP = LINK (SAVE) LINK (SAVE) = LINK (LINK (SAVE)) Free (TEMP) 6. [Finished] Return (TEMPHEAD) Algorithm for the print the list

Procedure PRINT (HEAD) [This Procedure print the information field of the list and HEAD is the first element of the list] 1. [Repeat step thru] Repeat while LINK (HEAD) != NULL 2. [Print the Information] Write (INFO (HEAD))

Page 6: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 6

3. [Finished] Return Doubly Linked List Explain Doubly Linked list Ans. The Linked list in which each node has two pointers, one to store address of forward link & second to store address of backward link, is called Doubly Linked list. The Backward link for point out left most node. & The forward link for point out right most node. Reason for use of doubly linked list. OR Disadvantages of Singly linked list. Ans. Suppose we have singly linked list in which we want to insert a node A pointed to by POINT 1 just before A node B pointed to by POINT 2. We can change the link field of A to point out B, but we don’t know the address of the node preceding B. Therefore this required time consuming sequential searching in singly linked list & this is In-efficient. To avoid this problem we used doubly linked list. Representation:

Generally, doubly linked list is represented as shown below. Head NULL Backward Address Forward Address

Page 7: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 7

NULL: - It indicates the end of the list in each direction. Each node having three fields.

1. Pointer to previous node. 2. Information field. 3. Pointer to next node.

Advantage: -

Inserting node in to or Deleting one node from the list is much easier

task because we don’t have to search the list sequentially to locate the preceding node. Algorithm for the Creation of the Doubly linked list Procedure CRETE(TEMPHEAD) [This Procedure Create the Doubly linked list TEMPHEAD is the pointer variable which point to the first element of the list and LPTR and RPTR is the pointer field of the NODE which points the Previous and new Node of the list respectively.] 1. [Repeat thru step] Repeat while choice! = ‘n’ 2. [Allocate the new Node] NEW NODE 3. [Set field of new Node] INFO (NEW) = X LPTR (NEW) = RPTR (RPTR) = NULL 4. [Insert the element] RPTR (TEMPHEAD) = NEW LPTR (RPTR (TEMPHEAD)) = TEMPHEAD TEMPHEAD = RPTR (TEMPHEAD) 5. [Read the Choice] Read (choice) 6. [Finished] Return

Page 8: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 8

Insertion in the middle of a doubly linked list

A Left-most insertion in a doubly linked list

Page 9: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 9

Algorithm for the insert an element in the doubly list

Function INSERT (TEMPHEAD, KEY) [This Function inserts an element after the node which the info filed equals to the KEY and the returns the address of the first node] 1. [Allocate the memory for the new node] NEW NODE INFO (NEW) = X 2. [Insertion as the first node] RPTR (NEW) = TEMPHEAD LPTR (NEW) = NULL RPTR (TEMPHEAD) = NEW TEMPHEAD = NEW Return (TEMPHEAD) 3. [Save address of the first node] SAVE = TEMPHEAD 4. [Find the element after which we want to insert the element] Repeat thru step while RPTR (SAVE)! = NULL 5. [Check for the desire position] If INFO (RPTR (SAVE)) = KEY Then RPTR (NEW) = RPTR (SAVE) LPTR (NEW) = SAVE LPTR (RPTR (SAVE)) = NEW RPTR (SAVE) = NEW 6. [Finished] Return (TEMPHEAD) Algorithm for the deleting an element from the doubly linked list Function DELETE (TEMPHEAD, KEY) [This Function delete an element from the doubly list and returns the address of the first element TEMPHEAD is pointer which points the first element of the list and KEY specify info of the node which is to be deleted] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (“Empty list”) Return

Page 10: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 10

2. [Deletion of the first node] TEMP = TEMPHEAD RPTR (TEMPHEAD) = TEMPHEAD PRV (TEMPHEAD) = NULL Free (TEMP) Return (TEMPHEAD) 3. [Save the address of the first node] SAVE = TEMPHEAD 4. [Search for the desire node] Repeat while thru step 5 RPTR (SAVE)! = NULL 5. [Check for the information field] If INFO (RPTR (SAVE)) = KEY Then TEMP = RPTR (SAVE) RPTR (SAVE) = RPTR (RPTR (SAVE)) LPTR (RPTR (SAVE)) = SAVE Free (TEMP) 6. [Finished] Return (TEMPHEAD) Algorithm for the print the doubly list Procedure PRINT (TEMPHEAD) [This procedure print the element of the node in the LIFO and FIFO format and TEMPHEAD points the first element of the list] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (“Empty list”) Return 2. [First in first out] Repeat while RPTR (TEMPHEAD)! = NULL Write (INOF (TEMPHEAD)) 3. [Last in first out] Repeat while TEMPHEAD! = NULL Write (INFO (TEMPHEAD)) 4. [Finished] Return Circular Singly Linked List

Page 11: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 11

Explain Singly Circular Linked List: Ans. A singly circular linked list is a linked list in which the last node of the list point to the first node in the list. In Circular linked list, we can start at any node in the list & travel the whole list. For this reason we can make our external pointer to the list pointer to any node & still access all the node in the list. Representation of Circular Linked list:

Head

Head Advantage of Circular List over Singly linked list.

Ans. 1. It is concerned with the accessibility of a node. 2. In Circular list every node is accessible from given node. i.e.: -

From this given node all nodes can be reached by many changing through the list.

3. It concerns the deletion operation. In singly linked list to delete desired node, it is necessary to give the address of first node of the list.

4. This necessity result from the fact that in order to delete desired node. The predecessor of this node has to be found.

5. To find the predecessor required that a search could be carried out by changing through node from the first node of the list such requirement doesn’t exist for circular list.

Disadvantage:

Page 12: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 12

Ans. It is possible that without some care in processing, it is possible to get in to an infinite loop. Solution of Disadvantage: Ans. In processing a circular list, it is important that we are able to delete the end of list. This deletion of end is achieved by placing special node, which can be easily identified in the circular list. This special node is often called the list head of the circular list. Representation of circular list with list head is given as in following figure. Head Algorithm for the Creation of the Circular list

Procedure CREATE (TEMPHEAD) [This procedure creates the circular list and TEMPHEAD is the pointer variable which points the first element of the list] 1. [Save the address of the first element] SAVE = TEMPHEAD 2. [Repeat thru step 5] Repeat while Choice! = ‘n’ 3. [Allocate the New node] NEW NODE 4. [Initialize the fields of new node] INFO (NEW) = X LINK (SAVE) = NEW SAVE = NEW 5. [Want to insert another node] Read (Choice) 6. [Set the LINK field of Last inserted element] LINK (SAVE) = TEMPHEAD

Page 13: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 13

7. [Finished] Return Algorithm for the insertion of the node in the circular list Function INSERT (TEMPHEAD, KEY) [This Function inserts an element after the node which have the info field equal to the KEY variable and TEMPHEAD is the pointer which points the first element of the list and SAVE is the temp variable for the store address of the first element] 1. [Allocate the Memory for the NEW node] NEW NODE 2. [Set fields of the NEW node] INFO (NEW) = X LINK (NEW) = NULL 3. [Save address of the first node] FIRST = TEMPHEAD 4. [Insertion as first node and find last element of the list] Repeat while LINK (TEMPHEAD)! = NULL 5. [Insert the node] LINK (TEMPHEAD) = NEW LINK (NEW) = FIRST FIRST = NEW Return (FIRST) 6. [Insert in the list other than the first node] Repeat while INFO (LINK (TEMPHEAD)) = KEY 7. [Set the link for the NEW node] LINK (NEW) = LINK (TEMPHEAD) LINK (TEMPHEAD) = NEW 8. [Finished] Return (FIRST) Algorithm for the Deletion an element from the circular list

Page 14: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 14

Function DELETE (TEMPHEAD, KEY) [This Function deletes an element from the circular list] 1. [Check for the empty list] If TEMPHEAD = NULL Then write (“Empty List”) 2. [List contain Single node] if LINK (TEMPHEAD) = TEMPHEAD Return NULL Free (TEMPHEAD) 3. [Save the address of the first node] FIRST = TEMPHEAD 4. [Deletion of the first node] Repeat while LINK (TEMPHEAD)! =NULL 5. [Delete the node] LINK (TEMPHEAD) = LINK (FIRST) LINK (FIRST) = FIRST Return (FIRST) 6. [Finding desire node] Repeat while INFO (LINK (TEMPHEAD)) = KEY 7. [Deletes the node] TEMP = LINK (TEMPHEAD) LINK (TEMPHEAD) = LINK (LINK (TEMPHEAD)) Free (TEMP) 8. [Finished] Return (FIRST) Algorithm for the printing the element of the circular list

Procedure PRINT (TEMPHEAD) 1. [Check for the empty list] If TEMPHEAD = NULL Then write (“Empty list”) Return 2. [Print the desire node] Repeat while LINK (TEMPHEAD)! = TEMPHEAD Write (INFO (TEMPHEAD)) 3. [Finished]

Page 15: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 15

Return Order Linked List:

Trace of the construction of an ordered linked linear list using Function INSORD Algorithm:

Function INSORD (TEMPHEAD, X) [This function inserts an element in the list which sorted to its info fields and X is the info field of the new node] 1. [Allocate Memory for the new node] NEW = NODE 2. [Copy the information field of the new node]

Page 16: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 16

INOF (NEW) = X 3. [Is the list empty?] If TEMHEAD = NULL Then LINK (NEW) = NULL Return (NEW) 4. [Does the new node precede all other node in the list?] If INFO (NEW) <= INFO (TEMPHEAD) Then LINK (NEW) = TEMPHEAD Return (NEW) 5. [Save the address of the first node] FIRST = TEMPHEAD 6. [Search the predecessor of the new node] Repeat while LINK (TEMPHEAD)! = NULL and INFO (LINK (TEMPHEAD)) <= INFO (NEW) - TEMPHEAD = LINK (TEMPHEAD) 7. [Set link fields of the new node] LINK (TEMPHEAD) = LINK (NEW) LINK (SAVE) = NEW 8. [Return first node pointer] Return (FIRST). Application of Linked list: Ans. There are no of applications of linear linked list, many examples could be given but only a few will be described here, 1. In Line Editor: -

One interesting use of linked list is line editor. We can keep a linked list of line nodes. Each containing line number, a line of Text & a pointer to next line information node.

2. In String Manipulation: - → Variable string length can also be represented as linked list.

Page 17: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 17

A string may be declared as a record that contains a string count & a pointer to the linked list of character.

Circular manipulation is very simple with this string implementation.

→ In one simple representation, each character node contains a string character & a pointer to the next character node. With this representation much more space is used for the pointer and for the character.

→ If space is limited, each node can contain several character, as well as pointer to next node.

This representation saves space at the cost of increasing the complexity of the algorithm that manipulate the character in the string.

3. In Implementation of Sparse matrix: - A sparse matrix is a table which relatively with few non-zeros

elements. 4. In Operating System: - The allocation of memory space may be managed doubly linked

list of various size block of memory. In multi-user system the operating system may keep track of

user jobs waiting to execute through linked queue of control block. 5. Implementing stack & queue: - It is easy to implement stack & queue operation using linked

list rather than array implementation of stack & queue. 6. In Polynomial Manipulation: - A linked list uses as a typical term of polynomial. The common

operation performs on polynomial are addition, subtraction, multiplication, division, integration & differentiation.

7. Linked Dictionary: - An important part of any compiler is the construction &

maintenance of a dictionary containing name & their associated values, such dictionary is also called a symbol table.

8. Another application, which is closely related to polynomial

equation, is performing operation to same arbitrary precision.

Page 18: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 18

What do you mean by dummy header? Ans. A Dummy header node is the list before the first actual data node can often contain useful information about the structure. For Example: - No of Node. A query Algorithm can then determines the status of list by examine the contents of the PREFIRST node. This amount to adding node more node to the list.

Page 19: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 19

Head

Addition & Deletion from list required changing this information-keeping field in the dummy header node of the list. If the list become empty & Dummy header node is not used then the HEAD pointer for the list must be made NULL. But if the dummy header node is present, then the HEAD pointer not need to be changed to NULL, because it always points to this dummy header.

Graphical Representation of Linked List Top

2000 Address of a node 20 Data of a node

3000 Address of next node

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL

Page 20: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 20

Insertion into a linked list

1. Insertion as a first node Before insertion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000

newNode 8000 4000 40 5000 8000 100 5000 50

NULL

Page 21: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 21

After Insertion

Head 1000 8000 10 2000

2000 20 3000 3000 30 4000

newNode 8000 4000 40 5000 8000 100 5000 50

1000 NULL

Page 22: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 22

2. Insertion at a desired position

Before Insertion

Head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL newNode 8000 8000 100

After Insertion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 8000 40 5000 5000 50 NULL newNode 8000 8000 100 4000

Page 23: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 23

Append a node in a linked list Before Append

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL newNode 8000 8000 100 NULL

After Appending

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 8000 newNode 8000 8000 100 NULL

Page 24: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 24

Deletion from a Linked List Deletion of a first node Before Deletion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL

After Deletion

head 1000 2000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL

Deletion of a desired node Before deletion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL

After deletion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL

Page 25: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 25

Graphical Representation of Doubly Linked List

Top

4000 Address of a node 5000 Address of previous node 40 Data of a node

5000 Address of next node head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 NULL

Insertion into a doubly linked list As a first node Before Insertion

head 1000 1000 NULL 2000 10 1000 3000 2000 20 2000 4000

newNode 8000 3000 30 3000 5000 8000 4000 40 4000

100 5000 50 NULL

After Insertion

head 1000 8000 8000 2000 10 1000 3000 2000 20 2000 4000

newNode 8000 3000 30 3000 5000 8000 NULL 4000 40 4000

100 5000 50 1000 NULL

Page 26: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 26

Append a node to doubly linked list Before Append head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 NULL 8000 NewNode 8000 100 NULL

After Append head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 8000 8000 NewNode 5000 8000 100 NULL

Page 27: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 27

Deletion of node Deletion of a first node Before deletion head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 NULL

After Deletion head 1000 1000 NULL 2000

10 NULL 3000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 NULL

Deletion of a desired node Before deletion head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 NULL

After deletion head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 5000 40 3000 5000 50 NULL

Page 28: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 28

Insertion at a desired position Before Insertion head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 3000 5000 4000 40 4000 5000 50 newNode 8000 NULL 8000 100

After Insertion head 1000 1000 NULL 2000

10 1000 3000 2000 20 2000 4000 3000 30 8000 5000 8000 40 4000 5000 50 newNode 8000 NULL 8000 4000 100 3000

Graphical Representation of Circular Linked List

Top

2000 Address of a node 20 Data of a node

3000 Address of next node

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 1000

Insertion into a linked list

Page 29: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 29

3. Insertion as a first node Before insertion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000

newNode 8000 4000 40 5000 8000 100 5000 50

NULL

After Insertion

head 1000 8000 10 2000

2000 20 3000 3000 30 4000

newNode 8000 4000 40 5000 8000 100 5000 50

1000 8000

Page 30: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 30

4. Insertion at a desired position

Before Insertion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 1000 newNode 8000 8000 100

After Insertion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 8000 40 5000 5000 50 1000 newNode 8000 8000 100 4000

Page 31: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 31

Append a node in a linked list Before Append

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 1000 newNode 8000 8000 100 NULL

After Appending

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 8000 newNode 8000 8000 100 1000

Page 32: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 32

Deletion from a Linked List Deletion of a first node Before Deletion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 1000

After Deletion

head 1000 2000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 2000

Deletion of a desired node Before deletion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 1000

After deletion

head 1000 1000 10 2000

2000 20 3000 3000 30 4000 4000 40 5000 5000 50 NULL

Page 33: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 33

Stack What is Stack? Ans. A stack is a data structure in which addition of New element or deleting of existing elements always takes place at the same end. This end is often known as ‘top’ of stack.

This situation can be compared to a stack of plates in a cafeteria where every new plate added to the stack is added at the ‘top’.

Similarly, every new plate taken off the stack is also from the ‘top’ of the stack.

When add an item to a stack we say that we push it on the stack & when we remove an item we say that pop it from the stack. So we

can say there are mainly two types of operations, Push & Pop, stack also sometimes called LIFO (Last In First Out). The real life example of Stack: Ans. As we talk about real life example, we all are familiar with “a railway system for shutting cars”, A railway Shutting system Representation As shown in figure, in this system the last railway car to be

placed on the stack is the First-level. Using respectively the insertion & deletion operations permits the cars to be arranging on the output

railway line in various orders.

What is the major advantage of pointer Implementation stack over array Implementation of stack? Ans.

1. With the array Implementation of stack. It is necessary to preallocate the max. Stack size at the time of implementing the program.

2. In pointer Implementation of stack is not need of the stack size.

3. With array implementation of stack, we must check for stack overflow but with linked list implementation we don’t need this.

Page 34: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 34

4. In array implementation if we want to insert a large number of element then we must define the big array. So allocate the memory as per our requirements.

5. When we delete the item from memory that memory can’t use for any other purpose.

6. In pointer implementation when we delete the item, the memory is free for any other purpose.

Application of the stack: Ans.

There are several applications where stack can be put to use.

1. Recursion 2. Polish Notation

3. Stack Machine

1. Recursion: - Recursion is the name given to the technique of defining

a set on a process in term of itself. OR When a called function in turn calls another function a

process of chaining occurs recursion a special case of this process when a function calls itself.

There are two important conditions that must be satisfied by any recursive procedure.

1) Each time a procedure calls itself (either directly or indirectly) it must be “nearer

in some sense, to a solution”. In the case of the factorial function, each time that the function calls itself, it argument is

decrement by one, so the argument of the function is getting smaller.

2) There must be a decision criterion for stopping the process or computation. In

the case of the factorial function, the value of n must be zero.

2. Polish notation: -

We are already familiar with arithmetic expressions

in infix notation. In this notation a binary operator is placed

between its operands.

Page 35: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 35

For example: -

A + B – C A – ( C – D ) / ( B * D )

A + B * D – E / F

The operations are normally carried out from left to

right. We also have procedure rules for evaluating expressions. A*B+C+D*E would be to multiply B & A, then adding it to C, saving

that result temporarily say in RESULT, Then multiplying D & E, and add it to the RESULT. Therefore we have to followed the sequence as given below,

AB * C + DE * + This notation is called the postfix notation or

reverse-polish notation. We can convert infix notation to postfix notation by using stack data structure.

3. Stack Machine: - One of the main problem with using machines

which have a very limited no of registers is how to handle the store of intermediate results to solve this problem such machines are

known as stack machine. Many of the machines, which are appearing on the

market, include in their architecture hardware stacks or stack

mechanisms. The such machines are the PDP-11 & the Burroughs 5000. Both machines are particularly well suited for the stacking of

local variables & parameters that arise in procedure calls of block nested languages.

What is push operation? Write an algorithm to push an element in to the stack. Ans. When an item is added to a stack, It is pushed on to the stack, given a stack & an item I, performing the operation push (st,I) adds the item I to the

top of stack ‘st’. Push operation is applicable to any stack.

Push I, to the stack ‘st’

tos → I

Stack – st

Algorithm for push operation

Page 36: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 36

Variables Size → Total no of elements tos → Top of the stack. val → Information which you want to insert in stack. Stack[]→ Array of stack. Step 1 [Check that the stack is Full] If tos = size-1 then (print message) Stack is full. Return. Step 2 [else] [Increment tos by 1] tos ← tos+1 Step 3 [Input the element to stack] Stack[tos]←val Step 4 [Stop] What is pop operation? Write an algorithm to pop an element in to the stack. Ans. The pop operation removes the top most item to understand after removed of top most information new value of the pointer top becomes the

previous value of top that is top=top-1 & free position is allocated as free space.

tos → I

B

A Stack (st)

Operation: POP I from the stack.

tos → B

A

Stack (st) Algorithm: Step 1 [Check that stack is empty]

Page 37: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 37

if tos=-1 then (print message) “Stack is Empty” return Step 2 [Else] [Decrement tos by 1] tos←tos-1 return.

Step 3 [Stop] What is peep operation? Write an algorithm to peep an element from the stack. Ans. The peep operation does not removes the item from the stack but by using peep we can view the elements from stack. So after peep operation

value of tos is unchanged.

tos → I

B

A Stack (st)

Operation: PEEPS 2nd element from the stack.

tos → I

B A

Stack (st) Peeped element is : B

Algorithm: This function returns the value of ith element from top of the stack(containing SIZE elements) Step 1 [Check for stack underflow] if tos – I + 1 < 0 then

Page 38: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 38

(print message) “Stack is Empty” return Step 2 [Return the Ith element from top of stack] return St[tos – I + 1];

Graphical Representation of Stack

Array representation of stack with five elements for following operations

1. insert 10 2. insert 20 3. insert 30 4. delete node 5. delete node 6. insert 40 7. insert 50

-

10

push(10)

10 20

push(20)

10 20 30

push(30)

10 20

pop( )

10

pop( )

Page 39: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 39

10 40

push(40)

10 40 50

push(50)

Page 40: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 40

Queue

What is queue? Ans.

� Queue is very useful in computer science. � We define a queue to be a list in which all addition to the list is made at the one end & all deletion from the list is made at other

end. � Queue are also called First In First Out list of FIFO for sort. � We may draw queue in any one of the forms as given below.

rear Data front

Data Data

Data Data

Data Data

front Data rear

� Queue makes two open ends called front & rear. � Similarly to stack operation, that operation define a queue are given below, 1. Create a queue

2. Check whether queue is empty 3. Check whether queue is full

4. Add item at the rear queue 5. Remove item from front of queue

6. Read the front of queue 7. Print the enter queue

There are mainly two types of queue,

1. Priority queue.

2.Circular queue. For example: -

The railway reservation counter is an example of queue where the people collect their tickets on the first in first out basis.

Implementation of Queue:

Page 41: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 41

A queue as pointer “front” & “rear” pointing to the “front” & “rear” elements of the queue, respectively consider a queue “Q”

consisting of (n) elements & element value which we have to insert in to “Q” the value NULL of front pointer implies an empty queue.

Queue is also called FCFS(First Come First Served).

Draw a queue using following data. Ans.

Consider a size 6. Assume that the queue is initially empty. It is required to insert element 1,2 & 3 followed by delete 1 & 2 & insert

4,5 & 6.

1

1 2

1 2 3

2 3

3

3 4

3 4 5

front rear

Front

Front

Front

Front

Front

Front

rear

rear

rear

rear

rear

rear

Page 42: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 42

3 4 5 6

Now, if we try to insert 7, an overflow occurs even through the first two cells are free. To avoid this drawback, we can arrange these

elements in a circular fashion with Queue[0] following Queue[n-1]. It is then called a circular array representation. We may depict a circular

queue as given in figure,

Note:- To see the disadvantage of queue see the advantage of circular queue.

Algorithm for simple queue:- Insert function:

Variables: -

val = Information of user. rear = Variable for last subscript value.

front = Point first element in queue. size = Total no of elements.

queue = Array of queue.

Front rear

Page 43: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 43

Step 1:- [Check that queue is full] if rear = (size – 1) then

[Print message] “Queue is overflow”

return

Step 2:- [else]

read value

Step 3:- [Check that the element is first element] if rear = -1 then rear = rear +1 front = front +1

Step 4:- [Input an element] queue [rear]←val

Step 5:- [Stop]

Delete Function

Step 1. [Check that queue is empty]

If front = -1 then (print message) “Queue is empty” return Step 2. [Check that front & rear both points to same element]

If front = rear then

Front ← -1 Rear ← -1 Return

Step 3. [Else] [Increment front by 1]

Front ← front +1 Step 4. Return

Step 5. [Stop]

Page 44: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 44

Print function

Step 1. [Check that queue is empty] If front = -1 then

(print message) “Queue is empty” return

Step 2. [Print Queue from front to rear] for i←front to rear print queue[i]

Step 3. [Stop] Search Function

Step 1. [Check that queue is empty] If front = -1 then

(print message) “Queue is empty” return

Step 2. [Which value you want to search] Read key value Step 3. [Search whole queue] for i←front to rear

Step 4. [Check that value found] if queue[i] = key then [print message] “key Value Found” return

Step 5. [Stop]

Update function

Step 1. [Check that queue is empty] If front = -1 then (print message) “Queue is empty” return

Step 2. [Which value you want to update] Read key value

Page 45: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 45

Step 3. [Search whole queue] for i←front to rear

Step 4. [Check that value found] if queue[i] = key then read queue[i]

return

Step 5. [Stop]

Circular Queue

Explain Circular Queue:-

Any number of items could be placed on the queue, so long as items

were also being taken off. This implementation of a queue is called circular queue, because it uses its storage array as if it were a circular instead of a

linear list. In essence of queue is full when the stored index is one index less that the retrieve index, otherwise there is room in the queue for another

event.

Circular Queue Perhaps the most common use of a circular queue is in operating

system where a circular queue holds the information read from & written to disk files on the console. Circular queues are also used in Real Time Application’s programs.

Which must continue to process information while buffering I/O request.

Advantages of circular queue over simple queue Ans.

But in circular queue we can insert new item to the location from where previous item to be deleted using crap

cround of the queue.

Page 46: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 46

In circular queue we can insert n numbers of elements

continuously but condition is that we must used deletion. Where as in simple queue continuously insertion is not possible.

If a computer need to provide continues scheduling of batch job, without interruption then by using circular

implementation we can achieve it.

Disadvantages of Circular queue Ans. In circular queue implementation the full queue condition & empty queue condition became same & it is inefficient for program therefore it in

necessary to delete the full queue condition at (Array size 1) location meaning that if we have array of 10 location than we can use only a location

to insert a queue.

Algorithm for Circular Queue: - Insert function:

Variables: -

Front = Points first element of queue. Rear = Variable for last subscript value.

Queue = Array of queue Size = Total no of elements. Step 1. [Check that Queue is full]

If front = 0 & rear = size-1 then [Print message] “Queue is overflow” return

Step 2. [Else] [Check that Queue is full] if rear = front-1 then [Print message]

“Queue Overflow”

return Step 3. [Else]

[Check that rear points to last element of queue]

if rear = size-1 then rear←0

Page 47: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 47

queue[rear]←val return

Step 4. [Else]

[Check that rear doesn’t points to any element]

if rear=-1 then front ← 0 rear ← 0 queue[rear] ← val return

P.T.O.

Step 5. [Else] [Increment rear by 1]

rear←rear+1 Step 6. [assign value in queue]

Queue[rear]←val Step 8. [Stop]

Delete Function:

Step 1. [Check that front & rear both points to same] If front = rear then

Front ← -1 Rear ← -1 Return

Step 2. [Else] [Check front points to last element]

If front = size –1 then Front ← 0

Step 3. [Else] [Increment front by 1]

Front ← front +1

Step 4. Return

Step 5. [Stop]

Print Function:

Page 48: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 48

Step 1. [Check that front points to any before rear

element] If front <= rear then

[Repeat i up to rear] for i←front to rear print queue[i]

Step 2. [else] [Repeat i up to last element] for i←rear to size –1 print queue[i]

Step 3. [Repeat i up to front] for i←0 to front print queue[i]

Step 4. [Stop]

Application of Queue: In a computer network messages from one to another

computer are generally created asynchronously. These messages therefore need to be buffered until the receiving computer is ready for it these communication buffers make extensive use of Queues by storing of Queues

by storing these message in a queue. Also the messages need to be sent to receiving computer in the some order in which they are created. I.e. FIFO.

Page 49: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 49

Tree

Definition:- o A Tree structure means that the data is organized as branches, which

relate the info. It is a non-linear data structure. One very common

gynecological chart that is used to represent tree structure is lineage.

The lineage chart represents ancestors.

Explain Tree:- o A Tree structure means that the data is organized as branches, which

relate the info.

o It is used to represent the relationship among data element in so

many applications.

o Tree is a non-linear data structure.

o Trees are encountered frequently in every life.

o An arrays, lists, stacks, queues are linear data structure.

o Graphs are classified in the non-linear category of data structure. You

may recall from the previous blocks on graph that an important class

of graph is called Trees.

o In Tree structure each node may paint to several other nodes. Thus a

tree is a very flexible & powerful data structure that can be used for a

glide variety of application.

o Although the nodes in a general tree may contain any no of pointer to

the other tree nodes.

o A large no of data structure have at the most two pointers to the other

tree nodes. This type of tree is called Root. Together with two binary

trees called the left sub tree & right sub tree of the root.

o Gaining from the leaves to the root is called climbing the tree &

gaining from the root to the leaves is called descending the tree.

o One of the most fundamental & useful concept is computer science.

o Trees find their application such as compiles construction database

design, operating systems etc.

o For (e.g.) :-

� Suppose we wish to use a data structure to represent a person

& all of his or her descendants. Assume that the person’s name

is Rahul & that he has 3 Children, sanjay, Sameer, Nisha. Also

suppose that sameer has 3 children, Abhay, Ajit & Madhu and

nisha has a child Neha. We can represent rahul & his

descendants guit naturally with the tree structure shown below.

Page 50: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 50

The set of tree is called forest.

Binary Tree:- o A Binary tree is a finite set of element that is either empty or is

partitioned into three disjoint subsets. The first subset contains a

single element called the root of the tree. The other two subsets are

themselves binary trees called the left & right subtree of the original

tree. A left or right subtree can be empty. Each element is called a

NODE of the tree.

o For (e.g):-

� We have a root R & two disjoint binary tree, T1 & T2 (Which are

called the left sub tree & right sub tree respectively). If T1 is

non-empty then the root of the T1 is called the left successor of

R. If T2 is non-empty then the root of T2 is called as right

successor of R.

Page 51: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 51

Here root is 1 & its predecessor is 2 & right successor is 3. Similarly

left successor of 2 is 4& right successor is 5.

Representation OR Implementation of Binary Tree.

� There are two traditional popular techniques that are used to maintain

binary tree in to the memory these are,

♦ Sequential Representation (Linear) ♦ Linked list Representation (Link)

� Sequential Representation:- ♦ A Sequential Representation of a binary tree requires numbering of

nodes starting with nodes on level then level 1 & so on…. The

nodes are numbered from left to right.

♦ The nodes of the binary tree are maintained in a one-dimensional array.

♦ Once the size of array has been determined the following method is used to represent the tree.

� Stare the root in 1st location of array. � If a node is in location n of the array.

♦ Stare its left & location in & its right child location (zn+1) � Advantages:-

♦ The main advantage of this method i.e in its simplicity & the fact that given a child node, its parent node can be determined

immediately, If the child nodes at location N in the array then its

parents node is at location N/Z (integer division).

♦ It can be implemented easily in elder language such as BASIC & FORTRAN.

♦ In tree searching is faster than linked list. � Disadvantages:-

Page 52: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 52

♦ Insertion & Deletion of node cause considerable data movement up & down. The array takes more processing time.

♦ In this method, the memory locations are listed if tree is partially filled.

� Linked Representation:- ♦ In this representation each node requires three fields. ♦ One for the link of the left child, Second field for representing the

information associated with the node & the Third field is used to

represent the link of the right child.

♦ When a node has no child then the corresponding pointer fields are Null.

♦ The given figure shows a linked representation of the binary tree. ♦ The Llink & Rlink fields are pointer to left child & the right child of a

node.

� Advantages:- � The insertion & the deletion in value no data movement except the

rearrangement of pointer . Thus, processing time is reduce.

� Disadvantages:- � Wastage of memory space in NULL pointer. The above given figure has 10

NULL pointers.

� Given a node, it is difficult to determine it’s a parent. � Its algorithm implementation is more difficult in languages such as BASIC,

COBOL. FORTRON. � Solution of Disadvantages:-

Info Pointer to Right Child

Pointer to Left Child

Page 53: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 53

� Threading the tree can offset the first disadvantage. � The second disadvantage can be offset by adding a parent field to a node

Operation Of Binary Tree

o There are several operation that can be performed in binary

trees such as, � Insertion

� Deletion � Searching

� Traversal of the tree etc.

• Traversal:- � One of the most common operation performed on tree

structure is that the Traversal. This is procedure by

which each node in the tree is processed exactly once.

� There are mainly three types of traversing binary tree. • Preorder

• Inorder

• Postorder • Preorder:-

� In this technique first of all we processed the root R of

the binary tree T. Then we traversal the left subtree T1

& R in preorder

� Which means that we traversal root of the subtree T1

first & then its left subtree.

� First of all consider the root node A then consider its left subtree as shown in upper figure.

Page 54: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 54

� Now we process root of subtree T1 & then its left subtree T3 that is a terminal node o. Now, consider the right subtree of T1 that T4, the root of T0 is E & then left subtree of T4 is T7 that is the

terminal of all the nodes of the left subtree T1 is finished & is given as, B,D,E,F.

� Continue the same process for the right subtree of A & its nodes. � After completion of the preorder traversing of binary tree we get list of nodes as following, � A, B, D, E, F, C, G, H, I, J.

� The preorder traversal of a binary tree is defined as follows, � First process the root node. � Second traversal the left subtree in preorder. � Third traversal the right subtree in preorder.

Algorithm for preorder:- � Temproot:- Temporary pointer variable initialized with root.

� Info:- Information part of node. � Left:- Pointer to left most node. � Right:- Pointer to right most node.

� Step-1:-( Repeat step 2,3,4 & check that temproot is not equal to NULL)

• If temproot is not equal to NULL then � Step-2:-(Print information part of node)

• Print info(temproot) � Step-3:-(Call function itself as a left most node)

• Preorder(left(temproot)) � Step-4:-(Call function itself as a right most node)

• Preorder(right(temproot)) � Step-5:-(Stop)

Inorder:- � In inorder traversal method first of all we have to process the left subtree T1 of the root R in Inorder then process the root

R & at the last. We process the right subtree T2 of R.

Page 55: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 55

� Consider the figure we first process the node D then the root of D is B node & then the right subtree of B. now the left subtree of e is f that is the terminal node thus, all the nodes

of left subtree of root is processed & resulting list is as follows: • Left Tree:- T1

T3

T4

T9

• Then we process the root A & then process the right subtree T2.

• Combining all the list of elements of the left subtree T1 & root & T2 element of the right subtree T2, We get the list of all the element in the binary tree T as following

B

D E

J

Page 56: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 56

♦ D, B, J, E, A, F, C, H, G, I. • The inorder to traversal of a binary tree is define as follows,

♦ Traverse the left tree in Inorder. ♦ Process the root node. ♦ Traverse the right tree in Inorder.

Algorithm For Inorder Traversal of Binary Tree:-

� Variable:- � Same as Preorder.

♦ Step-1:- (Repeat step2,3 & 4 & check that temproot is not equal to NULL)

� If temproot is not equal to NULL then ♦ Step-2:-(Call function itself as a left most node)

� Inorder (left(temproot)) ♦ Step-3:-(print Information part of node)

� Info (temproot) ♦ Step-4:-(Call function itself as a right most node)

� Inorder (right(temproot)) ♦ Step-5:-(Stop)

Postorder Traversal:- ♦ In the postorder traversal first of all we process the left subtree T1 of root in postorder. Then, the right subtree

T2 in postorder & at the last the root.

T1 T2

T3

T4 T5

T6 T7 T8

T9

♦ Consider the figure, we first process the terminal node D. Now, we consider right subtree of B that is E, the left subtree of E is F, so process F at the second priority as,

A

B C

D E G H

F I J

Page 57: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 57

there is no right subtree of E & at list root of subtree T1

that is B, the resulting list of elements after traversing the left subtree in postorder is as follows

T1 T3

T4

T7

♦ The final list of elements after traversing binary tree is as follows, � D, F, E, B, G, I, J, H, C, A.

♦ Postorder traversal of binary tree as defined as follows, � Traverse the left subtree in postorder. � Traverse the right subtree in postorder. � Process the root node.

Algorithm for Postorder Traversal of Binary Tree. :-

� Variable:-

♦ Same as preorder. • Step-1:-(Repeat step 2,3,4 & check that temproot is equal to NULL) ♦ If temproot is not equal to NULL then

• Step-2:- (Call function itself as a left most node)

♦ Postorder(left(temproot)) • Step-3:- (Call function itself as a right most node)

♦ Postorder(right(temproot)) • Step-4:-(Output the information part of node)

♦ Info(temproot) • Step-5:-(Stop)

Application of Binary Tree:- � There are three types of Binary tree application

B

D E

F

Page 58: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 58

• Manipulation of Arithmetic operation. • Symbol table constructor. • Syntax analysis.

� MANIPULATION OF ARITHMETIC OPERATION

� We will first discuss the relationship between binary tree & formulas in prefix or suffix notation. Next we will discuss the

mechanical manipulation of expressions that are represented by binary tree.

� We observe that the formulas in reverse polish notation are very useful in the compilations process.

� There is a close relationship between binary tree & formulas in prefix or suffix notation.

� Lets write any where the left & right subtree are as the left & right operands of the tree are the variable & constants in the

expression. � We may want to symbolically add, subtract, multiply, divide, differential, integrate etc. such expressions.

� Symbolic table Construction:-

� One of the criteria that a symbol table routine must meet is that the table searching must be performed efficiently.

� The two required operations that must be performed on symbol table are insertion & look-up each of which involves

searching. � A binary tree structure is chosen for two reasons. � The first reason is if the symbol entries as encountered one uniformly distributed according to lexico graphic order

� Second a binary tree is easily maintained in lexico graphic Order in the sense that only a few paints need to be changed.

� Also the message needs to be sent to receiving computer in the same order in which they are created i.e. FIFO (First In First out) order.

Page 59: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 59

Graphs

1) Graph:- A graph G consists of a non-empty set V called the set of nodes of the graph a set E which is the set of edge

of the graph & a mapping from the set of edges E to a set of pairs of elements of V.

Or

A graph consists of a set of nodes & a set of edges. A pair of nodes specifies each edge in a graph.

2) Node: - A graph G consists of non empty set V called the set

of Nodes of the graph.

3) Structure: -

Structure is a user define data type that allows the

user to perform certain(several) operations on to the different types of DATA TYPES.

4) Forest: -

A set of disjoint tree is called the Forest.

5) Weighted Graph: -

A graph in which weights are assigned to every edge is called a weighted graph.

6) Sling: -

An edge of a graph, which joins a node to itself, is called a Sling.

7) Complete Graph: -

A graph is complete or completely connected if and only if every pair of vertices are connected in at list

one direction.

8) Mixed Graph: -

If some of the edges are directed and some of the

edges are undirected in a graph then the graph is called Mixed graph.

Page 60: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 60

9) Pointer: -

Pointer is a one type of utility which is provided by

the ‘C’ Language that can store the address of the any particular node.

10) Isolated Vertex: -

In a graph, which is not adjacent to any other node is called Isolated vertex.

11) Loop: -

An edge of a graph, which joins a node to itself, is called a Loop. The direction of loop of no significance, hence it can be considered either a directed or

undirected edge.

12) Directed Graph: -

A graph is called a directed graph if each edge is

identified by ordered pay of vertices (vi, vj) Undirected graph the first element of the pair is called the start vertex and the second element is

called the end vertex of the edge. The edge is set to be directed from the start vertex to the end

vertex therefore the pairs (vi, vj) and (vj, vi) represent two different edges in a directed graph.

13) Undirected Graph:-

In Graph G=(V,E) an edge which has no specific

direction is called an Undirected edge. A graph in which every edge is undirected is called

Undirected graph.

14) Acyclic: -

A directed graph is acyclic if it has no cycles.

Otherwise graph is known as cyclic graph.

15) Outdegree of node: -

The outdegree of vi is the number of edges whose

start vertex is vi.

16) Indegree of node: -

Page 61: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 61

The no of edges which have V as their terminal node is

called the Indegree of V.

17) Total-Degree of node: -

The sum of the outdegree & indegree of a node V is called its Total degree.

18) Null graph: -

A graph containing only isolated node is called Null graph.

19) Initiating (Originating) & Terminating (Ending) Nodes:-

Let (V,E) be a graph & let XEE be a directed edge

associated with the ordered pair of nodes (u,v). Then the edge X is said to be initiating or originating in the

node u & terminating or ending in the node V.

20) Multi Graph: -

Any graph, which contains some parallel edges, is called a Multi graph.

21) Simple Graph: -

If there is no more than one edge between a pair of nodes, then such a graph is called a simple

graph.

22) Length: -

The no of edges appearing in the sequence of a path is called the length of the path.

23) Simple Path: -

A path in a digraph in which the edges are distinct is

called a simple path (edge simple).

24) Elementary path A path in which all the nodes through which it

traverses are distinct is called an elementary path.

25) Directed Tree: -

Page 62: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 62

A directed tree is an acyclic digraph which has one node called its root, with indegree 0, while all other

nodes have indegree 1.

26) Cyclic: - A path which originates & ends in the same node is

called a cycle.

27) Terminal node: - In a directed tree, any node, which has outdegree 0, is

called a terminal node or a leaf all other nodes are called branch node.

28) Root: - A cyclic digraph, which has one node, called its root.

29) Ordered Tree: - If in a directed tree an ordering of the nodes at each level is prescribe, then such a tree is called an

ordered tree.

30) Binary Tree: - A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets.

31) Successor & Predecessor: - A node n is adjacent to a node m if there is an edge

from m to n. If n is adjacent to m, n is called a successor of m & m a predecessor of n.

32) DAG: -

A directed acyclic graph is called a DAG from its acronym.

33) M-Ary Tree: - If in a directed tree the outdegree of every node is

less than or equal to m, then the tree is called an m-ary tree.

34) Sub tree: - A tree contains one or more nodes such that one of

the nodes is called the root while all other nodes are partitioned into a finite number of trees called sub tree.

Page 63: Linked List 3 Stack 33 Queue 40 - WordPress.com By Dhaval S Sheth(9033271276) 3 Linked List Singly Linked List Explain Singly Linked list: - Ans. A singly linked list is a linked list

Devloped By Dhaval S Sheth(9033271276) 63

35) Descendent: - Every node that is reachable from a node, say u, is

called a descendent of u

36) Siblings: - Children of the same parent are called the siblings.