You are on page 1of 10

qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjklzxcvb DATA STRUCTURES AND nmqwertyuiopasdfghjklzxcvbnmqwer ALGORITHMS ASSIGNMENT tyuiopasdfghjklzxcvbnmqwertyuiopas dfghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiopasdfghj klzxcvbnmqwertyuiopasdfghjklzxcvbn

mqwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzxcvbnmrty uiopasdfghjklzxcvbnmqwertyuiopasdf ghjklzxcvbnmqwertyuiopasdfghjklzxc


Two mark questions and Answers- Tree Structures Djikstra s Algorithm Prim s Algorithm Pushpa Savithri.R 9/12/2011

Unit II- Tree Structures

Two marks

UNIT II-TREE STRUCTURES TWO MARKS

1) What is non-linear data structure? The data structure in which the data is arranged in a linear or in straight order is said to be linear data structure. 2) Define the term tree. Give one example. Tree is a collection of two sets: vertices and edges. There is one special node in trees which is called root. For example,

a b c

3) Define binary tree. Binary tree is a tree in which no node can have more than two children. Maximum number of nodes at level i of a binary tree is 2^i+1 example:
b D e f A

4)

Write the declaration for the binary tree

Struct Treenode { int element; struct TreeNode *left; struct TreeNode *right; 2

Unit II- Tree Structures

Two marks

};

5)

Compare general tree and binary tree: GENERAL TREE BINARY TREE It has not more than two children. Example:
15

y y

It has any number of children Example:


15

18

20

10

20

25

6) What are the two ways of implementing the binary tree? y y Linear representation Linked representation

7) Compare full binary tree and complete binary tree *Full binary tree of height h has 2^n+1-1 node. * A complete binary tree of height h has between 2^n and 2^n-1 nodes. *Each node should have two children. *In the bottom level element should be filled from left to right. *A full binary tree can be a complete binary tree. Example:
A

*All complete binarytree is not a full binary tree.


A

8) How you will represent a binary tree using linear representation? The elements are represented using arrays. For any element in position i , the left child is in position 2i, the right child is in position (2i+1),

Unit II- Tree Structures

Two marks

and the parent is in position (i/2). Example:


A

A
D E F G

B 2

C 3

D 4

E 5

F 6

G 7

9) How will you represent a binary tree using linked representation? The elements are represented using pointers.Each node in linked representation has three fields namely, *pointer to the left subtree *data field *pointer to the right subtree.
A

10) Define expression tree y y Expression tree is a binary tree in which the leaf node are operands and the interior nodes are operators. Like binary tree,expression tree can also be traversed by inorder, preorder and postorder traversal.

11) What are rules for constructing the expression tree? y y Read one symbol at a time from the postfix expression. Check whether the symbol an operator or operand. (a)If the symbol is an operand , create a one-node tree and push a pointer on to the stack.

Unit II- Tree Structures

Two marks

(b) If the symbol is an operand pop two pointers from the stack namely T1 and T2 and from a new tree with root as the operators and T2 as a left child and T1 as a right child . A pointer to this new tree is then pushed onto the stack.

12) Define binary search tree. Binary search tree is a binary tree in which for every node X in the tree , the values of all the keys in its left subtree are smaller than the key value in X , and the values of all the keys in its right subtree are larger than the key value in X.
19

10

22

20

13) Compare binary tree and binary search tree. BINARY TREE *A tree is said to be a binary tree if it has atmost two children . BINARY SEARCH TREE *A binary search tree is a binary tree in which the key values in the left node is less than the root and the key values in the right node is greater the root. 4
4

10

10

12

12

14) write the declaration for binary search tree. Struct treenode; Typedef struct treenode *searchtree; Searchtree insert(int x, searchtree T); 5

Unit II- Tree Structures

Two marks

Searchtree delete(int x, searchtree T); Int find(int x, searchtree T); Int findmin(seachtree T); Int findmax(searchtree T); Searchtree makeempty(searchtree T); Struct treenode { Int element; Searchtree left; Searchtree right; };

15) Create a routine to make an empty tree. Searchtree makeempty(searchtree T) { If(T!=NULL) { Makeempty(T->left); Makeempty(T->right); Free(T); } Return NULL; }

16) create a routine for find operation. Int find(int x, searchtree T) { If(T=NULL) 6

Unit II- Tree Structures

Two marks

Return NULL; If(x<T->element) Return find(x,T->left); Else If(x<T->element) Return find(x,T->right); Else Return T; }

17) Write a routine for find min. //RECURSIVE: Int findmin(searchtree T) { If(T=NULL) Return NULL; Else if(T->left==NULL) Return T; Else Return findmin(T->left); } //NON RECURSIVE: Int findmin(searchtree T) { If(T!=NULL) While(T->left!=NULL) T=T->left; Return T; 7

Unit II- Tree Structures

Two marks

18) Write a routine for findmax. //RECURSIVE: Int findmin(searchtree T) { If(T=NULL) Return NULL; Else if(T->right==NULL) Return T; Else Return findmax(T->right); } //NON RECURSIVE: Int findmin(searchtree T) { If(T!=NULL) While(T->right!=NULL) T=T->right; Return T; }

19) What are the various traversals of binary tree?  Inorder traversal  Preorder traversal  Postorder traversal 20) Define AVL tree 8

Unit II- Tree Structures

Two marks

* AVL Tree is named after its inventors Adelson-Velskill and LANDIS *An AVL tree is an binary search tree except that for every node in the tree , the height of the left and right subtrees can differ by atmost 1. *The height of the empty tree is defined to be -1.

21) When the tree causes imbalance? Case 1: An insertion into the left subtree of the left child of node a. Case 2: An insertion into the right subtree of the left child of node a. Case 3: An insertion into the left subtree of the right child of node a. Case 4: An insertion into the right subtree of the right child of node a.

22) How will you overcome the imbalances in an AVL Tree? y y Single rotation Double rotation

23) What is priority queue? A priority queue is a special kind of queue data structure which will have precedence over jobs. Basic operations performed by priority queue are: y y Insert operarion DeleteMin operation

24) What is need for priority queue?  In multiuser environment, the operating system scheduler must decide which of several processes to run.  The scheduler will take the first job on the queue and allow it to run until either it finishes or its time limit its up.  Generally a process is allowed to run only for a fixed period of time.  It can be overcome by using priority queue in which short jobs are assigned a higher precedence. 25) What are the implementation methods of priority queue? y y Linked list binary search tree 9

Unit II- Tree Structures

Two marks

binary heap.

26) Define binary heap. The efficient way of implementing the priority queue is the binary heap. Heap have two properties namely     Structure property Heap order property Structure property where the tree must be complete binary tree Heap order property where the root node must the minimum node.

27) What are other heap operations? y y y y Decrease key Increase key Delete Build heap

28) What is build heap? The build Heap operations takes as input N keys and places them into an empty heap by maintaining structure property and heap order property.

10