0% found this document useful (0 votes)
65 views10 pages

DS & Alg

Uploaded by

jadhavkaran203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views10 pages

DS & Alg

Uploaded by

jadhavkaran203
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

a)”Binary tree contains every node with mininimum two child nodes”. State true/false.

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.

c)What is degree of a graph?The degree of a graph is the number of edges incident to a


vertex.

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).

j) What is synonym of Hashing?A synonym of hashing is “hash function”.

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.

b) Explain Mid-Square function in hashing with suitable example.The Mid-Square hashing


function involves squaring the input value, taking the middle digits of the result, and using
them as the hash value. For example, if we have the input value 546, when squared it
becomes 298116. Then, taking the middle two digits, we get 81, which can be used as the
hash value.
c) What is inverse adjacency list?An inverse adjacency list is a data structure used in
graph theory to represent the inverse relationships of an adjacency list. Instead of listing the
neighbors of each vertex, it lists the vertices that point to a given vertex. It’s particularly
useful for tasks such as finding predecessors of a vertex efficiently.

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.

Insert 30 to the right of 15.

Insert 20 to the left of 30.

Insert 5 to the left of 15.

Insert 10 to the right of 5.

Insert 2 to the left of 5.

Insert 7 to the right of 5.

The resulting binary search tree looks like this:

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)

/ / \

10 (R) 35 (R) 50 (R)

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;

Struct Node* left;

Struct Node* right;

};

Struct Node* createNode(int data) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

Int displayAndCountLeafNodes(struct Node* root) {

If (root == NULL)

Return 0;

If (root->left == NULL && root->right == NULL) {

Printf(“%d “, root->data); // Display leaf node


Return 1; // Count leaf node

Return displayAndCountLeafNodes(root->left) + displayAndCountLeafNodes(root->right);

Int main() {

Struct Node* root = createNode(50);

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);

Printf(“Leaf nodes: “);

Int count = displayAndCountLeafNodes(root);

Printf(“\nNumber of leaf nodes: %d\n”, count);

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;

Struct Node *left;

Struct Node *right;


};

Struct Node* createNode(int value) {

Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

Struct Node* insert(struct Node* root, int value) {

If (root == NULL) {

Return createNode(value);

If (value < root->data) {

Root->left = insert(root->left, value);

} else if (value > root->data) {

Root->right = insert(root->right, value);

Return root;

Void inorderTraversal(struct Node* root) {

If (root != NULL) {

inorderTraversal(root->left);

printf(“%d “, root->data);

inorderTraversal(root->right);

}
}

Int main() {

Struct Node* root = NULL;

Root = insert(root, 50);

Insert(root, 30);

Insert(root, 20);

Insert(root, 40);

Insert(root, 70);

Insert(root, 60);

Insert(root, 80);

Printf(“Inorder traversal of the binary search tree: “);

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:

Insert each element into the AVL tree one by one.

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).

Rebalance the tree if necessary by performing rotations.

Here’s the AVL tree constructed for the given data:

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.

Here’s the step-by-step process:

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.

Store the value in the hash table at the calculated index.

Let’s do it:

Values: 13, 45, 24, 113, 161, 207, 211

Table size: 11

Calculate hash values:

13 % 11 = 2

45 % 11 = 1

24 % 11 = 2 (collision, so linear probing: 2 + 1 = 3)

113 % 11 = 5

161 % 11 = 7

207 % 11 = 4

211 % 11 = 6
Store values in the hash table:

Slot 1: 45

Slot 2: 13 (collision resolved using linear probing)

Slot 3: 24

Slot 4: 207

Slot 5: 113

Slot 6: 211

Slot 7: 161

Here’s the hash table contents:

Index: 0 1 2 3 4 5 6 7 8 9 10

Value: - 45 13 24 207 113 211 161 - - -

This represents the hash table after storing all the values and resolving collisions using
linear probing.

a)Differentiate between B and B+ tree.

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 contain actual data.

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.

You might also like