• No results found

Description: Here START is a pointer variable which contains the address of first node. PTR is a pointer variable which contains address of node to be deleted. PREV is a pointer variable which points to previous node. ITEM is the value to be deleted.

1. If (START == NULL) Then [Check whether list is empty]

2. Print: Linked-List is empty.

3. Else

4. PTR = START, PREV = START 5. Repeat While (PTR->LINK != NULL) 6. PREV = PTR [Assign PTR to PREV]

7. PTR = PTR->LINK [Move PTR to next node]

[End of While Loop]

8. ITEM = PTR->INFO [Assign INFO of last node to ITEM]

9. If (START->LINK == NULL) Then [If only one node is left]

10. START = NULL [Assign NULL to START]

11. Else

9. PREV->LINK = NULL [Assign NULL to link field of second last node]

[End of Step 9 If]

10. Delete PTR

11. Print: ITEM deleted [End of Step 1 If]

12. Exit

P a g e | 40 Department of Computer Science & Engineering GEC

Exercise 9:

d) Adding two large integers which are represented in linked list fashion.

ALGORITHM:

ADD ( ):

Description: Here A is a two – dimensional array with M rows and N columns and B is a two –dimensional array with X rows and Y columns. This algorithm adds these two arrays.

1. If (M ? X) or (N ? Y) Then 2. Print: Addition is not possible.

3. Exit [End of If]

4. Repeat For I = 1 to M 5. Repeat For J = 1 to N

6. Set C[I][J] = A[I][J] + B[I][J]

[End of Step 5 For Loop]

[End of Step 6 For Loop]

7. Exit

Explanation: First, we have to check whether the rows of array A are equal to the rows of array B or the columns of array A are equal to the columns of array B. if they are not equal, then addition is not possible and the algorithm exits. But if they are equal, then first for loop iterates to the total number of rows i.e. M

and the second for loop iterates to the total number of columns i.e. N. In step 6, the element A[I][J] is added to the element B[I][J] and is stored in C[I][J] by the statement:

C[I][J] = A[I][J] + B[I][J

P a g e | 41 Department of Computer Science & Engineering GEC e) Write a C program to reverse elements of a Single linked list.

ALGORITHM:

Algorithm to Reverse a Linked List Reverse ( ):

Description: Here START is a pointer variable which contains the address of first node. PTR will point to the current node and PREV will point to the previous node.

REV will maintain the reverse list.

1. Set PTR = START, PREV = NULL 2. Repeat While (PTR != NULL) 3. REV = PREV

4. PREV = PTR 5. PTR = PTR->LINK 6. PREV->LINK = REV [End of While Loop]

7. START = PREV 8. Exit

P a g e | 42 Department of Computer Science & Engineering GEC f) Write a C program to representation the given Sparse matrix using arrays.

ALGORITHM:

Description: Here A is a two – dimensional array with M rows and N columns and B is a two – dimensional array with X rows and Y columns. This algorithm adds these two arrays.

1. If (M ? X) or (N ? Y) Then 2. Print: Addition is not possible.

3. Exit [End of If]

4. Repeat For I = 1 to M 5. Repeat For J = 1 to N

6. Set C[I][J] = A[I][J] + B[I][J]

[End of Step 5 For Loop]

[End of Step 6 For Loop]

7. Exit

Explanation: First, we have to check whether the rows of array A are equal to the rows of array B or the

columns of array A are equal to the columns of array B. if they are not equal, then addition is not possible

and the algorithm exits. But if they are equal, then first for loop iterates to the total number of rows i.e. M

and the second for loop iterates to the total number of columns i.e. N. In step 6, the element A[I][J] is

added to the element B[I][J] and is stored in C[I][J] by the statement:

C[I][J] = A[I][J] + B[I][J

P a g e | 43 Department of Computer Science & Engineering GEC g) Write a C program to representation the given Sparse matrix using linked list ALGORITHM:

Description: Here A is a two – dimensional array with M rows and N columns and B is a two – dimensional array with X rows and Y columns. This algorithm multiplies these two arrays.

1. If (M ? Y) or (N ? X) Then

2. Print: Multiplication is not possible.

3. Else

Explanation: First we check whether the rows of A are equal to columns of B or the columns of A are

equal to rows of B. If they are not equal, then multiplication is not possible. But, if they are equal, the first

for loop iterates to total number of columns of A i.e. N and the second for loop iterates to the total number

of rows of B i.e. X. In step 6, all the elements of C are set to zero. Then the third for loop iterates to total

number of columns of B i.e. Y. In step 8, the element A[I][K] is multiplied with B[K][J] and added to

C[I][J] and the result is assigned to C[I][J] by the statement:

C[I][J] = C[I][J] + A[I][K] * B[K][J

P a g e | 44 Department of Computer Science & Engineering GEC

Exercise10:

a) Write a C program to Create a Binary Tree of integers ALGORITHM:

Binary tree is an important type of structure which occurs very often. It is

characterized by the fact that any node can have at most two branches, i.e.,there is no node with degree greater than two. For binary trees we distinguish between the subtree on the left and on the right, whereas for trees the order of the subtree was irrelevant. Also a binary tree may have zero nodes. Thus a binary tree is really a different object than a tree.

Definition: A binary tree is a finite set of nodes which is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree.

We can define the data structure binary tree as follows:

structure BTREE

declare CREATE( ) --> btree

ISMTBT(btree,item,btree) --> boolean MAKEBT(btree,item,btree) --> btree LCHILD(btree) --> btree

DATA(btree) --> item RCHILD(btree) --> btree for all p,r in btree, d in item let ISMTBT(CREATE)::=true

ISMTBT(MAKEBT(p,d,r))::=false

LCHILD(MAKEBT(p,d,r))::=p; LCHILD(CREATE)::=error DATA(MAKEBT(p,d,r))::d; DATA(CREATE)::=error RCHILD(MAKEBT(p,d,r))::=r; RCHILD(CREATE)::=error end

end BTREE

P a g e | 45 Department of Computer Science & Engineering GEC b) Write a recursive C program, for Traversing a binary tree in preorder, inorder and postorder.

ALGORITHM:

PREORDER

The first type of traversal is pre-order whose code looks like the following:

sub P(TreeNode)

Output(TreeNode.value)

If LeftPointer(TreeNode) != NULL Then P(TreeNode.LeftNode)

If RightPointer(TreeNode) != NULL Then P(TreeNode.RightNode)

end sub

This can be summed up as

Visit the root node (generally output this) Traverse to left subtree

Traverse to right subtree

And outputs the following: F, B, A, D, C, E, G, I, H

IN-ORDER

The second(middle) type of traversal is in-order whose code looks like the following:

sub P(TreeNode)

If LeftPointer(TreeNode) != NULL Then P(TreeNode.LeftNode)

Output(TreeNode.value)

If RightPointer(TreeNode) != NULL Then P(TreeNode.RightNode)

end sub

P a g e | 46 Department of Computer Science & Engineering GEC This can be summed up as

Traverse to left subtree

Visit root node (generally output this) Traverse to right subtree

And outputs the following: A, B, C, D, E, F, G, H, I POST-ORDER

The last type of traversal is post-order whose code looks like the following:

sub P(TreeNode)

If LeftPointer(TreeNode) != NULL Then P(TreeNode.LeftNode)

If RightPointer(TreeNode) != NULL Then P(TreeNode.RightNode)

Output(TreeNode.value) end sub

This can be summed up as

Traverse to left subtree Traverse to right subtree

Visit root node (generally output this)

And outputs the following: A, C, E, D, B, H, I, G, F

P a g e | 47 Department of Computer Science & Engineering GEC

Exercise 11:

a) Write a C program to Create a BST Algorithm CreateBST (A)

Create a binary search tree (BST) from an array A with N elements.

root the root of a new binary search tree named T;

Perform an inorder (second-visit) traversal of the BST with root named root.

if root is empty then return

else

InOrder (left child of root);

output the value in root;

InOrder (right child of root);

end if

Algorithm TreeSort (A)

Sort the elements in array A using a binary search tree (BST).

CreateBST (A); {Creates a new BST T containing the elements of A}

InOrder (root of T); {Sorts the elements in T using an inorder traversal}

b) Write a C program to insert a note into a BST.

Binary Search Tree Algorithms Algorithm Insert BST (v, root)

Iteratively insert a value v into a binary search tree (BST) with root named root.

P a g e | 48 Department of Computer Science & Engineering GEC if root is empty then

root v;

else

node root;

loop {an infinite loop; we will explicitly exit the loop after v is inserted}

if v value stored in node then if the left child of node exists then node left child of node;

else

insert v as the left child of node;

exit the loop;

end if else

if the right child of node exists then node right child of node;

else

insert v as the right child of node;

exit the loop;

Recursively insert a value v into a binary search tree (BST) with root named root.

if root is empty then root v;

else

if v value stored in root then if the left child of root exists then InsertBST (v, left child of root);

else

insert v as the left child of root;

P a g e | 49 Department of Computer Science & Engineering GEC end if

else

if the right child of root exists then InsertBST (v, right child of root);

else

insert v as the right child of root;

end if end if end if

c) Write a C program to delete a note from a BST.

Related documents