Professional Documents
Culture Documents
UNIT-3 Data Structures Questions (Odd)
UNIT-3 Data Structures Questions (Odd)
UNIT-3 Data Structures Questions (Odd)
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):
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
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
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
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.
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.
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:
In order sequence: D B E A F C
Preorder sequence: A B D E C F
/ \
/ \
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.
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