DS & Alg
DS & Alg
False. A binary tree contains at most two child nodes per parent node, but not necessarily
every node.
b) Define: left skewed binary. tree.A left-skewed binary tree is a binary tree in which each
node has at most one child, and all nodes are oriented towards the left side of the tree.
d) Name datastructure used to implement depth first search (DFS) of a graph. Depth
First Search (DFS) of a graph is often implemented using a stack data structure.
e) What is complete binary tree? A complete binary tree is a binary tree in which every
level, except possibly the last, is completely filled, and all nodes are as far left as possible.
f) Define: balance factor. Balance factor in the context of AVL trees is the difference
between the heights of the left and right subtrees of a node.
g)Write properties of a good hash function. Properties of a good hash function include:
determinism (same input yields same output), efficiency (quick computation), uniformity
(outputs are distributed evenly), and minimal collisions (few instances where different inputs
produce the same output).
h) Write any two applications of graph. Two applications of graphs are: network routing
algorithms (e.g., finding the shortest path in a transportation network) and social network
analysis (e.g., finding connections between individuals).
a) What is splay tree? A splay tree is a self-adjusting binary search tree where recently
accessed elements are moved closer to the root to improve access time. It performs
rotations and restructuring operations called “splays” to maintain balance and keep frequently
accessed items near the root.
d) Show the steps of creating a binary search tree for the following data
15,30,20,5,10,2,7. Steps for creating a binary search tree from the given data:Start with the
root node as 15.
15
/ \
5 30
/ \ /
2 10 20
a)Construct red black tree for the following. 50, 40, 30, 20, 10, 35, 5. Here’s the
construction of a Red-Black Tree for the given data:
Inserting 50:
50 (B)
Inserting 40:
50 (B)
/
40 (R)
Inserting 30:
40 (B)
/ \
30 (R) 50 (R)
Inserting 20:
40 (B)
/ \
30 (B) 50 (B)
20 (R)
Inserting 10:
30 (B)
/ \
20 (B) 40 (B)
/ \
10 (R) 50 (R)
Inserting 35:
30 (B)
/ \
20 (B) 40 (B)
/ / \
Inserting 5:
30 (B)
/ \
20 (B) 40 (B)
/ \ \
5(R) 25 50 (R)
b)Write a recursive function in ‘c’ to display and count leaf nodes of a binary search
tree.Here’s a recursive function in C to display and count leaf nodes of a binary search
tree:
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
};
newNode->data = data;
return newNode;
If (root == NULL)
Return 0;
Int main() {
Root->left = createNode(40);
Root->left->left = createNode(30);
Root->left->left->left = createNode(20);
Root->left->left->left->left = createNode(10);
Root->left->left->right = createNode(35);
Root->left->left->left->right = createNode(25);
Return 0;
a)Write a ‘c’ function to insert a node in binary search tree.here’s a simple C function to
insert a node into a binary search tree:
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
If (root == NULL) {
Return createNode(value);
Return root;
If (root != NULL) {
inorderTraversal(root->left);
printf(“%d “, root->data);
inorderTraversal(root->right);
}
}
Int main() {
Insert(root, 30);
Insert(root, 20);
Insert(root, 40);
Insert(root, 70);
Insert(root, 60);
Insert(root, 80);
inorderTraversal(root);
printf(“\n”);
return 0;
b)Construt AVL tree for the following data :XYZ, PQR, LMN, QBC, ABC, STR. UVW,
BMC. To construct an AVL tree, you would typically follow these steps:
After each insertion, check if the tree needs to be rebalanced to maintain the AVL property
(the heights of the two child subtrees of any node differ by at most one).
XYZ
/ \
PQR UVW
/ \ \
ABC LMN STR
\ \
BMC QBC
This tree maintains the AVL property, ensuring that the heights of the subtrees for each
node differ by at most one.
c)Store following values in Hash table:13, 45, 24, 113, 161, 207, 211.
Use division method of hashing with table size 11. Number of slots is I in each bucket.
Apply linear probing to resolve over flow. Show hash table contents.
Let’s create a hash table using the division method with a table size of 11 and resolve
overflow using linear probing.
Calculate the hash value for each number using the division method (hash value = number
% table size).
If the calculated index is already occupied, apply linear probing to find the next available
slot.
Let’s do it:
Table size: 11
13 % 11 = 2
45 % 11 = 1
113 % 11 = 5
161 % 11 = 7
207 % 11 = 4
211 % 11 = 6
Store values in the hash table:
Slot 1: 45
Slot 3: 24
Slot 4: 207
Slot 5: 113
Slot 6: 211
Slot 7: 161
Index: 0 1 2 3 4 5 6 7 8 9 10
This represents the hash table after storing all the values and resolving collisions using
linear probing.
B-trees and B+ trees are both self-balancing tree data structures commonly used in
databases and file systems for efficient storage and retrieval. Here’s a brief differentiation:
Node Structure:
B-tree nodes can store both keys and pointers to child nodes.
B+ tree nodes only store keys; pointers to actual data are stored in the leaf nodes.
Leaf Nodes:
B+ tree leaf nodes only contain keys, and they are linked together to facilitate sequential
access.
Search Operations:
In a B-tree, searches may require traversing multiple levels of the tree.In a B+ tree,
searches always start from the root and go directly to the leaf nodes, improving search
efficiency.