Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Binary Trees and Linked Lists: Problems and Solutions, Quizzes of Data Structures and Algorithms

Solutions to various problems related to binary trees and linked lists, including calculating the number of null branches in a binary tree, finding the number of different trees possible with a given number of nodes, identifying which tree structures are efficient in terms of space and time complexities, converting expressions to prefix and postfix notations, writing a function to find cycles in a linked list, determining the size of a tree based on its depth, and implementing two stacks using one array.

Typology: Quizzes

2021/2022

Uploaded on 07/04/2022

johnny-test-1
johnny-test-1 🇮🇳

3 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1) A binary tree with 20 nodes has how many null branches?
ANS:21
A binary tree with n nodes has exactly n+ 1 null node.
2) How many different trees are possible with 10 nodes ?
ANS: 1014
If there are n nodes, there exist 2n-n different trees.
3) 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed a
full binary tree?
ANS: 15.
In general:
There are 2n-1 nodes in a full binary tree.
By the method of elimination:
Full binary trees contain odd number of nodes. So there cannot be
full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete
binary tree but not a full binary tree. So the correct answer is 15.
4) Of the following tree structure, which is, efficient considering space and time
complexities?
(a) Incomplete Binary Tree
(b) Complete Binary Tree
(c) Full Binary Tree
ANS :(b) Complete Binary Tree.
By the method of elimination:
Full binary tree loses its nature when operations of insertions and deletions are done.
For incomplete binary trees, extra storage is required and overhead of NULL node checking
takes place. So complete binary tree is the better one since the property of complete binary
tree is maintained even after operations like additions and deletions are done on it.
5) Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and
Postfix notations.
ANS:
Prefix Notation:
^ - * +ABC - DE + FG
Postfix Notation:
AB + C * DE - - FG + ^
6) Write a function to find cycles in a linked list.
ANS:
bool find_cycle(Node* head)
{
if(head == NULL // empty list
|| head->next == NULL // list with 1 node
|| head->next>next == NULL) // 2 nodes w/o cycle
{
return false;
}
pf3

Partial preview of the text

Download Binary Trees and Linked Lists: Problems and Solutions and more Quizzes Data Structures and Algorithms in PDF only on Docsity!

  1. A binary tree with 20 nodes has how many null branches? ANS: A binary tree with n nodes has exactly n+ 1 null node.
  2. How many different trees are possible with 10 nodes? ANS: 1014 If there are n nodes, there exist 2 n-n different trees.
  3. 8, 15, 13, 14 nodes were there in 4 different trees. Which of them could have formed a full binary tree? ANS: 15. In general: There are 2 n-1 nodes in a full binary tree. By the method of elimination: Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer is 15.
  4. Of the following tree structure, which is, efficient considering space and time complexities? (a) Incomplete Binary Tree (b) Complete Binary Tree (c) Full Binary Tree ANS :(b) Complete Binary Tree. By the method of elimination: Full binary tree loses its nature when operations of insertions and deletions are done. For incomplete binary trees, extra storage is required and overhead of NULL node checking takes place. So complete binary tree is the better one since the property of complete binary tree is maintained even after operations like additions and deletions are done on it.
  5. Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent Prefix and Postfix notations. ANS: Prefix Notation: ^ - * +ABC - DE + FG Postfix Notation: AB + C * DE - - FG + ^
  6. Write a function to find cycles in a linked list. ANS: bool find_cycle(Node* head) { if(head == NULL // empty list || head->next == NULL // list with 1 node || head->next>next == NULL) // 2 nodes w/o cycle { return false; }

Node* ptr1 = head; Node* ptr2 = head->next; while (ptr1 != NULL && ptr2 != NULL && ptr2->next != NULL) { I f(ptr1 == ptr2) { print("\nClycle present in thr LinkList\n"); return true; } ptr1 = prt1->next; ptr2 = ptr2->next->next; } return false; }

  1. If the depth of a tree is 3 levels, then what is the size of the tree? ANS: 7
  2. List out few of the applications that make use of Multilinked Structures? ¬ Sparse matrix, ¬ Index generation.
  3. What is the output of the following #include<stdio.h> #include<conio.h> Void main() { Union var { Int a; Char ch[2]; }v; v.a=512; printf(“%d %d %d”,v.a,v.ch[0],v.ch[1]); } ANS: 512 2 0
  4. How do you implement 2 stacks using only one array. Your stack routines should not indicate an overflow unless every slot in the array is used? ANS: Given an Array, start the first stack S1 from left end and other stack S2 from the right end. While S1 gets grows towards right, S2 grows towards left.
  5. Consider a empty stack of integers.Let the numbers 1,2,3,4,5,6 be pushed on to this stack only in the order they appeared from left to right.Let S indicates a push and X indicate a pop operation.Can they be permuted in to the order 325641(output) (if a permutation is possible give the order string of operations. (Hint: SSSSSSXXXXXX outputs 654321) ANS: SSSXXSSXSXXX outputs 325641. 154623 cannot be output as 2 is pushed much before 3 so can appear only after 3 is output.