UNIT-3 Data Structures Questions (Odd)

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

1(a). If the depth of binary tree is k, then the maximum no of nodes in that tree is 2 K - 1, prove?

Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
Types of binary trees:
1. Full Binary Tree. It is a special kind of a binary tree that has either zero children or two children
2. Complete Binary Tree
3. Perfect Binary Tree
4. Balanced Binary Tree
5. Degenerate Binary Tree

In a full binary tree with 2 children for each node except leaf nodes,
you have 1 root, 2 sons of that root, 4 grandsons, 8 grand-grandsons
and so on.

So, the total number of nodes is the sum of the geometric series:

1. 1+2+4+8+⋯+2k=(2(k+1)−1)/(2−1)=2(k+1)−1
where k is the depth (i.e., for k=0 we have 1 node).
2. In some standard books the binary tree with depth k=0 is not considered as a binary tree so the
formula is given as
2(k) - 1, k>=1

1(b). Recommend the result of inserting 3,1,4,6,9,2,5,7 into an initially empty binary search tree?
Binary Search Tree is a node-based binary tree data structure which has the following
Properties:
1. The left sub tree of a node contains only nodes with keys lesser than the node’s key.
2. The right sub tree of a node contains only nodes with keys greater than the node’s key.
3. The left and right sub tree each must also be a binary search tree.

In a binary search tree, we all know that the insertion is done in such an order that the in-order
arrangement of nodes always gives ascending order so the recommended order will be 1, 2,3,4,5,6,7,9
3(a). Write a routine for preorder, in order and post order traversal of a binary tree?
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.

Unlike linear data structures (Array, Linked List, Queues, Stacks, etc.) which have only one logical way to
traverse them, trees can be traversed in different ways. The following are the generally used methods
for traversing trees:

In order Traversal
Algorithm In order (tree):

 Traverse the left sub tree, i.e., call In order(left->sub tree)


 Visit the root.
 Traverse the right sub tree, i.e., call In order(right->sub tree)

Uses of In order Traversal:


In the case of binary search trees (BST), in order traversal gives nodes in non-decreasing order. To get
nodes of BST in non-increasing order, a variation of in order traversal where in order traversal is
reversed can be used.
Preorder Traversal:
Algorithm Preorder (tree)

 Visit the root.


 Traverse the left sub tree, i.e., call Preorder(left->sub tree)
 Traverse the right sub tree, i.e., call Preorder(right->sub tree)

Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix
expressions on an expression tree.
Post order Traversal:
Algorithm Post order (tree)

 Traverse the left sub tree, i.e., call Post order(left->sub tree)
 Traverse the right sub tree, i.e., call Post order(right->sub tree)
 Visit the root

Uses of Post order:


Post order traversal is used to delete the tree. Post order traversal is also useful to get the postfix
expression of an expression tree.

3(b). As workflow for compositing digital images for visual effects what data structures used? Identify
it and explain it with possible operations.
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
Applications of binary trees:
 Store hierarchical data, like folder structure, organization structure, XML/HTML data.
 Binary Search Tree is a tree that allows fast search, insert, delete on a sorted data. It also allows
finding closest item
 Heap is a tree data structure which is implemented using arrays and used to implement priority
queues.
 B-Tree and B+ Tree : They are used to implement indexing in databases.
 Syntax Tree: Scanning, parsing , generation of code and evaluation of arithmetic expressions in
Compiler design.
 K-D Tree: A space partitioning tree used to organize points in K dimensional space.
 Trie : Used to implement dictionaries with prefix lookup.
 Suffix Tree : For quick pattern searching in a fixed text.
 Spanning Trees and shortest path trees are used in routers and bridges respectively in computer
networks
 As a workflow for compositing digital images for visual effects.
 Decision trees.
 Organization chart of a large organization.
 In XML parser.
 Machine learning algorithm.
 For indexing in database.
 IN server like DNS (Domain Name Server)
 In Computer Graphics.
 To evaluate an expression.
 In chess game to store defense moves of player.
 In java virtual machine.
Explanation about how composition of images is done:
 Computer vision is by its nature very computationally expensive, if for no other reason
than the amount of data to be processed.
 One of the solutions is using parallel computers = brute force
 Many computer vision problems are difficult to divide among processors, or decompose
in any way.
 Hierarchical data structures make it possible to use algorithms which decide a strategy
for processing on the basis of relatively small quantities of data.
 They work at the finest resolution only with those parts of the image for which it is
necessary, using knowledge instead of brute force to ease and speed up the processing.
 Two typical structures - pyramids and quadtrees.
 So we can clearly say quad binary trees are used for this image composition
5(a). Sketch a binary tree and mention various methods in which it can be represented.
Let T be a Binary Tree. There are two ways of representing T in the memory as follow
1. Sequential Representation of Binary Tree.
2. Link Representation of Binary Tree.
Linked Representation of Binary Tree

Consider a Binary Tree T. T will be maintained in memory by means of a linked list representation which
uses three parallel arrays; INFO, LEFT, and RIGHT pointer variable ROOT as follows. In Binary Tree each
node N of T will correspond to a location k such that

 LEFT [k] contains the location of the left child of node N.


 INFO [k] contains the data at the node N.
 RIGHT [k] contains the location of right child of node N.

In this representation of binary tree root will contain the location of the root R of T. If any one of the sub
trees is empty, then the corresponding pointer will contain the null value if the tree itself is empty, the
ROOT will contain the null value.

Example Consider the binary tree T in the figure. A schematic diagram of the linked list representation
of T appears in the following figure. Observe that each node is pictured with its three fields, and that the
empty sub tree is pictured by using x for null entries.
2) Sequential representation of Binary Tree
Let us consider that we have a tree T. let our tree T is a binary tree that is complete binary tree. Then
there is an efficient way of representing T in the memory called the sequential representation or array
representation of T. This representation uses only a linear array TREE as follows:
The root N of T is stored in TREE [1].
If a node occupies TREE [k] then its left child is stored in TREE [2 * k] and its right child is stored into TREE
[2 * k + 1]. For Example, Consider the following Tree:

5(b). One height balanced tree with its operations and example?
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
Types of binary trees:
1. Full Binary Tree. It is a special kind of a binary tree that has either zero children or two children
2. Complete Binary Tree
3. Perfect Binary Tree
4. Balanced Binary Tree
5. Degenerate Binary Tree

One Height Balanced Binary Tree


A height-balanced binary tree is defined as a binary tree in which the height of the left and the right sub
tree of any node differ by not more than 1. AVL tree, red-black tree are examples of height-balanced
trees.
Following are the conditions for a height-balanced binary tree:
 The difference between the heights of the left and the right sub tree for any node is not more
than one.
 The left sub tree is balanced.
 The right sub tree is balanced.

Note: An empty tree is also height-balanced.


To check if the binary tree is height-balanced or not, you have to check the height balance of each node.
For this, you need to calculate the heights of the two sub trees for each node making this impractical.
Instead, we store the height balance information of every sub tree in the root node of that sub tree.
Thus, each node not only maintains its data and children’s information but also a height balance value.
The height balance of a node is calculated as follows:

 height balance of node = height of right sub tree – height of left sub tree
 The above formula means that:
 If the right sub tree is taller, the height balance of the node will be positive.
 If the left sub tree is taller, the balance of the node will be negative.

Applications of Height-Balanced Binary Tree:


1. Balanced trees are mostly used for in-memory sorts of sets and dictionaries.
2. Balanced trees are also used extensively in database applications in which insertions and
deletions are fewer but there are frequent lookups for data required.
3. It is used in applications that require improved searching apart from database applications.
4. It has applications in storyline games as well.
5. It is used mainly in corporate sectors where they have to keep the information about the
employees working there and their change in shifts.
Advantages of Height-Balanced Binary Tree:
1. It will improve the worst-case lookup time at the expense of making a typical case roughly one
lookup less.
2. As a general rule, a height-balanced tree would work better when the request frequencies
across the data set are more evenly spread,
3. It gives better search time complexity.
Disadvantages of Height-Balanced Binary Tree:
1. Longer running times for the insert and remove operations.
2. Must keep balancing info in each node.
3. To find nodes to balance must go back up in the tree.
4. How to check if a given tree is height-balanced:
You can check if a tree is height-balanced using recursion based on the idea that every sub tree of the
tree will also be height-balanced. To check if a tree is height-balanced perform the following operations:

 Use recursion and visit the left sub tree and right sub tree of each node:
 Check the height of the left sub tree and right sub tree.
 If the absolute difference between their heights is at most 1 then that node is height-balanced.
Otherwise, that node and the whole tree are not balanced.

7(a). Types of heap trees and pseudo code of mishap.


A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
Generally, Heap Trees can be of two types:

 Max-Heap Tree: In a Max-Heap the key present at the root node must be greatest among the
keys present at all of its children. The same property must be recursively true for all sub-trees in
that Binary Tree.
 Min-Heap Tree: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of its children. The same property must be recursively true for all sub-trees in
that Binary Tree.
Operations of Heap Data Structure:

 Heapify: a process of creating a heap from an array.


 Insertion: process to insert an element in existing heap time complexity O (log N).
 Deletion: deleting the top element of the heap or the highest priority element, and then
organizing the heap and returning the element with time complexity O (log N).
 Peek: to check or find the most prior element in the heap, (max or min element for max and min
heap).

min heap pseudo code

7(b). reconstruction of tree from its binary tree traversals


Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
Traversals of binary tree:
1. In order
2. Pre order
3. Post order
Let us consider below traversals: the

 In order sequence: D B E A F C
 Preorder sequence: A B D E C F

Construct Tree from In order& Preorder


In a Preorder sequence, the leftmost element is the root of the tree. So, we know ‘A’ is the root for given
sequences. By searching ‘A’ in the In-order sequence, we can find out all elements on the left side of ‘A’
is in the left sub tree and elements on right in the right sub tree. So, we know the below structure now.
A

/ \

/ \

DBE FC

We recursively follow the above steps and get the following tree.

/ \

/ \

B C

/\ /

/ \ /

D EF
Algorithm: build Tree ()

 Pick an element from Preorder. Increment a Preorder Index Variable (preindex in below code) to
pick the next element in the next recursive call.
 Create a new tree node with the data as the picked element.
 Find the picked element’s index in In order. Let the index be in Index.
 Call builds Tree for elements before in Index and make the built tree as a left sub tree of node.
 Call builds Tree for elements after in Index and make the built tree as a right sub tree of node.
 return node.

Example code:
class Node {
char data;
Node left, right;
Node (char item)
{
data = item;
left = right = null;
}
}
class BinaryTree {
Node root;
static int reindex = 0;
Node buildTree (char in [], char pre [], int insert, int in End)
{
if (insert>in End)
return null;
Node tNode = new Node(pre[preIndex++]);
if (inStrt == inEnd)
return tNode;
int inIndex = search(in, inStrt, inEnd, tNode.data);
tNode.left = buildTree(in, pre, inStrt, inIndex - 1);
tNode.right = buildTree(in, pre, inIndex + 1, inEnd);
return tNode;
}
int search(char arr[], int strt, int end, char value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
return i;
}
return i;
}
void printInorder(Node node)
{
if (node == null)
return;
printInorder(node.left);
System.out.print(node.data + " ");
printInorder(node.right);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
char in[] = new char[] { 'D', 'B', 'E', 'A', 'F', 'C' };
char pre[] = new char[] { 'A', 'B', 'D', 'E', 'C', 'F' };
int len = in.length;
Node root = tree.buildTree(in, pre, 0, len - 1);
System.out.println("Inorder traversal of constructed tree is : ");
tree.printInorder(root);
}
}
9(a). What is binary search tree? Create a binary search tree for following data
20,45,30,16,5,78,26,23,14.explain deleting node 23 in resultant bst.
Binary Search Tree is a node-based binary tree data structure which has the following properties:
1. The left sub tree of a node contains only nodes with keys lesser than the node’s key.
2. The right sub tree of a node contains only nodes with keys greater than the node’s key.
3. The left and right sub tree each must also be a binary search tree.

By considering above information, we can say a binary search tree follows some order to arrange the
elements. In a Binary search tree, the value of left node must be smaller than the parent node, and the
value of right node must be greater than the parent node. This rule is applied recursively to the left and
right sub trees of the root.

Binary search tree for given data:

To delete the node 23 initially the pointer goes through 20 -> 45 -> 30 ->26 -> 23 and 23 gets deleted.
The final binary search tree looks like

9(b). Explain and implement recursive algorithms for binary tree traversals with certain examples.
Binary Tree is defined as a tree data structure where each node has at most 2 children. Since each
element in a binary tree can have only 2 children, we typically name them the left and right child.
Traversals of binary tree:
1. In order
2. Pre order
3. Post order

Binary Tree’s Recursive Traversal Algorithm


Since the tree traversal rule is recursive, recursive traversal of a binary tree is very popular and
convenient. Thus, according to the child-first traversal of a binary tree rules, there are three recursive
traversal orders:
1) Preorder: access root node, traverse the left sub tree, and traverse the right sub tree
2) In order: traverse the left sub tree, access root node, traverse the right sub tree
3) Post order: traverse the left sub tree, traverse the right sub tree, access root node
It can be summed up as some rules. First, preorder traversal of the first root node is the root node, while
post order traversal of the last root node is the root node.
Second, the last root node of preorder traversal is the rightmost child node of the right sub tree, the last
node of in order traversal is the most right node of the root node right sub tree. Third, leftmost root
node in order traversal first node to the root of the left sub tree, post order traversal is the first node as
a left sub tree the left child node.
From the above rules, we can draw the following inferences. The whole tree sort can be derived through
the preorder traversal and the post order traversal. In order traversal and post order traversal can
determine a binary tree. Preorder traversal and post order traversal cannot determine a binary tree by
themselves.
Examples:

You might also like