0% found this document useful (0 votes)
18 views

Data Structure Questions

This document discusses data structures like linked lists and binary trees. It provides examples of using arrays and pointers to simulate linked lists. It also discusses traversing and finding elements in linked lists and binary trees in one pass.

Uploaded by

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

Data Structure Questions

This document discusses data structures like linked lists and binary trees. It provides examples of using arrays and pointers to simulate linked lists. It also discusses traversing and finding elements in linked lists and binary trees in one pass.

Uploaded by

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

DATA STRUCTURES

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

(a) If we run traverse() on the


first_list, what is the output
suppose to be?

33 21 7 9 66

(b) If we run traverse() on the


second_list, what is the output
suppose to be?

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;

first->next=NULL; //adress of 34 is set to NULL

while (second->next!=NULL) {

// 34 is linked to 42 and continues


second->next=first;

//first and second pointers move one node to


//the right
first=second; second=second->next;
}

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

// head pointer points the first node of the list

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

void RemoveDuplicates (Node *head) {


Node* first=head;
Node* second=head->next;

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

fun1() prints the given Linked List in


reverse manner.
For Linked List 1->2->3->4,
fun1() prints 4->3->2->1.
Merge two sorted linked lists into one sorted linked list.
Allocate no extra node. At a minimum, how many pointers (or
references) would you need?

1->2->3->4 + 5->6->7->8 ==> 1->2->3->4->5->6->7->8


1->2->3->4 + 1->2->7->9 ==> 1->1->2->2->3->4->7->9
1->2->3->4 + null ==> 1->2->3->4
Write a function to destroy a given linked list from
memory

struct node *destroy (struct node *head) {

if(head == NULL) { printf("List is empty\n"); return; }

struct node *temp2;

while (head!= NULL) {


temp2=head;
head = head->next;
delete temp2; }

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.

Which of the following input sequences will not


produce this binary search 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?

A node can have maximum one parent.


You have a binary search tree (BST) with n elements and
you need to find the 3th largest element in the tree. How
can you find it?

Yes. Do inorder traversal and stop at the (n-2)th node.


If the size of a binary tree is 14, what is the maximum
height of this tree?

13
Which are Perfect (FULL) Binary Trees?

A, B, D
How can we read data in a binary search tree in
ascending order?

A. Post order traversal


B. Inorder traversal
C. Preorder traversal
D. None of above
In array representation of binary tree the right child of
root will be at index value of

3
Why postfix expression is used for mathematical calculations?

Infix Expression Postfix Expression


5+2*3 523*+
5*2+3 52*3+
5*(2+3)-4 523+*4-

All operators have same precedence.

No need for parenthesis. Simplifies code generation.


What is the infix expression defined in this binary tree?

a*b+c-d
What is the postfix expression
defined in this binary tree?

ab*cd-+

void Postorder(node *nptr){


if(nptr){
Postorder(nptr->left);
Postorder(nptr->right);
printf("%d\n", nptr->data);
}}
Write a code which returns the number of nodes of a binary search tree.

int *s=0; //counts the size

int size(BTREE *root, int *s) {


if(root == NULL)
return 0;
else
*s= *s +1;
size(root -> left); size(root -> right);
}
Write a function which returns the elements of a binary
search tree (BST) in assending order.

void Inorder(node *nptr){


if(nptr) {
Inorder(nptr->left);
printf("%d\n", nptr->data);
Inorder(nptr->right);
}
}
Draw Binary Search Tree for The Months of The Year
Input Sequence: JAN, FEB, MAR, APR, MAY, JUNE, JULY, AUG,
SEPT, OCT, NOV, DEC

JAN

FEB MAR

APR JUNE MAY

AUG JULY SEPT

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

APR DEC JUNE NOV SEPT


Disadvantage of using array representation for
binary trees is?
a) difficulty in knowing children nodes of a node
b) difficult in finding the parent of a node
c) have to know the maximum number of nodes possible before
creation of trees
d) difficult to implement
What must be the ideal size of array if the height
of tree is ‘h’? (in case of array representation of
binary tree)
a) 2h+1
b) h-1
c) h
d) 2h
Answer: a
Explanation: Since maximum elements in a tree (complete binary tree) of
height h will be 2h+1-1. So a good array size must be that size.
Advantages of linked list representation of binary
trees over arrays?
a) dynamic size
b) ease of insertion/deletion
c) ease in randomly accessing a node
d) both dynamic size and ease in insertion/deletion
Answer: d
Explanation: It has both dynamic size and ease in insertion and
deletion as advantages.
Write a function which mirrors the binary trees as
shown below
struct Tnode {
int val;
struct Tnode *left,*right; };

/* C Function to Mirror Binary Tree */

void mirrorbst(struct Tnode *Troot){


struct Tnode *tmp;
if(Troot==NULL) return;
mirrorbst(Troot->left);
mirrorbst(Troot->right);
tmp=Troot->left;
Troot->left=Troot->right;
Troot->right=tmp;
}
Write a function which prints ancestor of given node
value in binary tree.
struct Tnode {
Node Value Ancestors int val; struct Tnode *left,*right; };
4 8 3 6 1
13 8 10 14
1 0

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

void levelprinting (struct Tnode **Troot, int level, int inlev) {


if((*Troot)!=NULL) {
levelprinting (&((*Troot)->left),level+1, inlev);
if(level==inlev) printf("%d ",(*Troot)->val);
levelprinting (&((*Troot)->right),level+1, inlev);
}}
Consider the following directed graph

List the nodes in the order they would be visited in a


depth-first search of the graph starting at A. When
choosing a node to explore next, break ties in favor
of the alphabetically least.

A
B
D
E
G
H
J
I
F
C
Consider the following directed graph

List the nodes in the order they would be visited in a


breadth-first search of the graph starting at A.
When choosing a node to explore next, break ties
in favor of the alphabetically least.

A
B
C
D
E
F
G
H
I
J
Consider the following directed graph

How many directed simple cycles does this graph


have?
27
Consider the following graph

Suppose we perform a breadth-first


search of this graph starting at node 4.
Which nodes could be the last node visited
27 by the search? Explain briefly.

The nodes 1 and 2 are the farthest, at


distance 3. So one of these two must be
visited last regardless of how the BFS is
done.
Consider the following graph

Suppose we perform a recursive depth-first


search of this graph starting from 4. From
any given node, we visit its successors in
ascending order of their value. In what
order are the nodes visited?

4, 5, 3, 1, 2, 6, 8, 7, 9

You might also like