Data Structure Questions
Data Structure Questions
QUESTIONS
Answer:C
How can you simulate a Linked List by using arrays
The given figure use 2D array to simulate linked-list (first column
stores data part and second column stores address part).
33 21 7 9 66
10 48 18 11 25
How to find middle element of linked list in one pass?
Head
7 ● 14 ● 5 ● 5 ● 5 NULL
P1 P2
In order to find middle element of linked list in one pass, you need to
maintain two pointer.
The first one increments one node at a time while other pointer
increments two nodes at a time. When second pointer reaches end,
first pointer will point to middle element of linked list.
Head
7 ● 14 ● 5 ● 5 ● 5 NULL
P1 P2
How to find 3rd element from end in a linked list in
one pass?
Head
7 ● 14 ● 5 ● 5 ● 5 NULL
P2 P1
Two pointers are used. First pointer starts moving from the 3rd
element and second pointer starts from the first element.
When first pointer reaches to the end of linked list, second pointer will
be pointing to the 3rd element from last in a linked list.
Head
7 ● 14 ● 5 ● 5 ● 5 NULL
P2 P1
At a minimum, how many pointers (or references)
would you need to reverse a singly linked list with
ONE iteration?
first second Node* first=head;
Node* second=head->next;
while (second->next!=NULL) {
second->next=first;
At a minimum, how many pointers (or references)
would you need to reverse a singly linked list with
ONE iteration? (second solution)
Write a code that finds the last node of a Linked
List. Return a pointer (reference) to the last node
struct Node {
int data;
Node *next;
};
Node* tail = head; // tail pointer points to the first node of the list
while (tail->next != NULL) {// while tail is not at the end of the list
tail = tail->next;
}
Write a RemoveDuplicates() function which takes a list sorted in
increasing order and deletes any sequentialy duplicated nodes
from the list. Ideally, the list should only be traversed once.
1->2->3->4 -> 5->6->7->8 ==> 1->2->3->4->5->6->7->8
1->1->2->2->3->4->7->9 ==> 1->2->3->4->7->9
first second
while (second->next!=NULL) {
if (first->data == second->data) {
first->next= second->next; //link definition
delete second;
second= first->next; }
else { //first and second pointers move one node to the right
first=second;
second=second->next; }
} }
What does the following function do for a given Linked
List?
void fun1(struct Node* head) {
if(head == NULL)
return;
fun1 (head->next);
printf ("%d ", head->data);
}
return head;
}
Consider the binary tree shown below. For each of
the traversals listed, give the order in which the
nodes are visited.
F B A E C D H J L
A B E D C F J H L
A D C E B J L H F
F B H A E J L C D
Suppose we remove the 63 from the binary search
tree, replacing it with something from the left
subtree. What will be the new number at the place of
63?
Answer: 50
The binary search tree shown below was constructed
by inserting a sequence of items into an empty tree.
Answer: E
Suppose we remove the root from the binary search
tree, replacing it with something from the left
subtree. What will be the new root?
Answer: 38
What is the minimum and maximum number of nodes
in a binary tree with depth 3?
Answer:
Maximum=20+21+22+23
Minimum=4
Recall that in a binary tree, a node may have 0, 1,
or 2 children. In the following questions about binary
trees, the height of a tree is the length (number of
edges) of the longest path. A tree consisting of just
one node has height 0
(a) What is the maximum number of nodes in a binary tree 2d+1 − 1
of height d?
(b) What is the minimum number of nodes in a binary tree d+1
of height d?
(c) What is the maximum height of a binary tree n−1
containing n nodes?
(d) What is the minimum height of a binary tree containing log n
n nodes?
You want to build a table of contents for a textbook.
The textbook consists of chapters, chapters consist
of sections, and sections consist of subsections
A) List
B) Binary Seach Tree
C) Tree
D) Binary Tree
E) Balanced Tree
What is the depth of this tree?
What is the level of node 52?
Answer:
Depth=4
Level=5
Which figüre is not a tree? Why?
13
Which are Perfect (FULL) Binary Trees?
A, B, D
How can we read data in a binary search tree in
ascending order?
3
Why postfix expression is used for mathematical calculations?
a*b+c-d
What is the postfix expression
defined in this binary tree?
ab*cd-+
JAN
FEB MAR
DEC OCT
NOV
Draw a Balanced Binary Search Tree for The Months of The Year
Input Sequence: JULY, FEB, MAY, AUG, DEC, MAR, OCT, APR,
JAN, JUNE, SEPT, NOV
JULY
FEB MAY
JAN
AUG MAR OCT
0 1
0
int printances (struct Tnode *Troot1, int nodeval){
if(Troot1==NULL) return 0; 0
0
if(nodeval==Troot1->val) return 1; 1
if(printances (Troot1->left, nodeval) || printances (Troot1->right, nodeval))
{ printf(" %d ",Troot1->val);
return 1;
}
return 0;
}
Write a function which prints specific level values in
BST. Level 0 mean Root value.
Level Values struct Tnode {
0 8 int val;
1 3 10 struct Tnode *left,*right;
2 1 6 14 };
A
B
D
E
G
H
J
I
F
C
Consider the following directed graph
A
B
C
D
E
F
G
H
I
J
Consider the following directed graph
4, 5, 3, 1, 2, 6, 8, 7, 9