Data Structure notes., Exercises of Data Structures and Algorithms

An introduction to binary trees and tree traversal. It covers basic terminology, representation, binary trees, tree representation using array and linked list, basic operations on binary trees, traversal of binary trees, algorithms on tree traversal with and without recursion, binary search trees, expression trees, and their implementation using pointers. The document also includes short answer questions, paragraph questions, and a 10-mark essay. It is useful for students studying computer science or related fields.

Typology: Exercises

2019/2020

Available from 10/04/2022

Tinutony
Tinutony 🇮🇳

8 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
QUESTIONS AND ANSWERS
MODULE 4
Trees-Basic Terminology, representation, Binary Trees, Tree representation using Array and Linked List, Basic
operation on Binary Tree: insertion, deletion and processing, traversal of binary trees: inorder, preorder and post order,
algorithm on tree traversal with and without recursion, Binary search tree, operation on binary search tree, Operation on
Binary Search Tree, Expression Tree, Implementation using pointers, applications
Short Answer Questions
1. What are the Steps in preorder traversal?
Ans:
root
left subtree in preorder
Right sub tree in preorder
2. What is level number? How it is represented in a tree?
Ans: Depth/height of a tree is the maximum level of a tree.
Level I has 2i nodes/leaves
Eg: Level 0 has 1 node
Level 3 has 23 = 8 nodes
3. What is the specialty of inorder traversal of binary search tree?
Ans:An inorder traversal can return the elements in increasing order since a binary search tree has
elements that are less than the node to the left and those that are greater than the node to the right.
4. Total number of nodes in the binary tree which has of level 4 is: 8
5. What is meant by degree of a node? With example?
pf3
pf4
pf5
pf8

Partial preview of the text

Download Data Structure notes. and more Exercises Data Structures and Algorithms in PDF only on Docsity!

QUESTIONS AND ANSWERS MODULE 4 Trees-Basic Terminology, representation, Binary Trees, Tree representation using Array and Linked List, Basic operation on Binary Tree: insertion, deletion and processing, traversal of binary trees: inorder, preorder and post order, algorithm on tree traversal with and without recursion, Binary search tree, operation on binary search tree, Operation on Binary Search Tree, Expression Tree, Implementation using pointers, applications Short Answer Questions

1. What are the Steps in preorder traversal? Ans: root left subtree in preorder Right sub tree in preorder 2. What is level number? How it is represented in a tree? Ans: Depth/height of a tree is the maximum level of a tree. Level I has 2i^ nodes/leaves Eg: Level 0 has 1 node Level 3 has 2^3 = 8 nodes 3. What is the specialty of inorder traversal of binary search tree? Ans:An inorder traversal can return the elements in increasing order since a binary search tree has elements that are less than the node to the left and those that are greater than the node to the right. 4. Total number of nodes in the binary tree which has of level 4 is : **8

  1. What is meant by degree of a node? With example?**

Degree of a node is the total number of children of that node. Degree of a tree is the highest degree of a node among all the nodes in the tree.

6. Define forest. Also give example of it. Forest is a collection of disjoint trees. In other words, we can also say that forest is a collection of an acyclic graph which is not connected. Here is a pictorial representation of a forest. 7.How do you represent the binary tree in computer memory? Ans: Memory Representation-Array A small and almost complete binary tree can be easily stored in a linear array. Small tree is preferably stored in linear array because searching process in a linear array is expensive. Complete means that if most of the nodes have two child nodes.To store binary tree in a linear array, you need to consider the positional indexes of the nodes. This indexing must be considered starting with 1 from the root node going from left to right as you go down from one level to other. **Paragraph Questions

  1. Write a program to traverse binary search tree non recursively in preorder?** Ans: *void iterativePreorder(node root) {
  1. Trie : Used to implement dictionaries with prefix lookup.
  2. Suffix Tree : For quick pattern searching in a fixed text.
  3. Spanning Trees and shortest path trees are used in routers and bridges respectively in computer networks
  4. As a workflow for compositing digital images for visual effects. 3. Explain different traversal methods in binary tree Tree traversal is one of the most common operations performed on tree data structures. It is a way in which each node in the tree is visited exactly once in a systematic manner. There are three standard ways of traversing a binary tree. They are:
  5. Pre Order Traversal (Node-left-right)
  6. In order Traversal (Left-node-right)
  7. Post Order Traversal (Left-right-node) PRE ORDERS TRAVERSAL RECURSIVELY To traverse a non-empty binary tree in pre order following steps one to be processed
  8. Visit the root node
  9. Traverse the left sub tree in preorder
  10. Traverse the right sub tree in preorder IN ORDER TRAVERSAL RECURSIVELY The in order traversal of a non-empty binary tree is defined as follows :
  11. Traverse the left sub tree in order
  12. Visit the root node
  13. Traverse the right sub tree in order POST ORDER TRAVERSAL RECURSIVELY The post order traversal of a non-empty binary tree can be defined as :
  14. Traverse the left sub tree in post order
  15. Traverse the right sub tree in post order
  16. Visit the root node

4. What is expression tree? Represent the following expression using a tree. Comment on the result that you get when this tree is traversed in preorder, postorder and inorder a + (b * c) + d * (e + f)  Inorder expression: (a+(bc))+(d(e + f)) LEFT ROOT RIGHT**  Postorder Expression: a b c * + d e f + * + LEFT RIGHT ROOT  Preorder Expression: **+ + a * b c * d + e f ROOT LEFT RIGHT 10 mark essay

  1. How to represent a tree using array? Write a program to delete a number from binary tree?**  Because an array's length is fixed at compile time, if we use an array to implement a tree we have to set a limit on the number of nodes we will permit in the tree. Our strategy is to fix the maximum height of the tree (H), and make the array big enough to hold any binary tree of this height (or less). We'll need an array of size (2**H)-1. Here is the biggest binary tree of depth 3: root of the tree (A): array position 1  root's left child (B): array position 2

struct node* node = (struct node) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } / This function traverses tree in post order to to delete each and every node of the tree / void deleteTree(struct node node) { if (node == NULL) return; /* first delete both subtrees / deleteTree(node->left); deleteTree(node->right); / then delete the node / printf("\n Deleting node: %d", node->data); free(node); } / Driver program to test deleteTree function*/ int main() { struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3);

root->left->left = newNode(4); root->left->right = newNode(5); deleteTree(root); root = NULL; printf("\n Tree deleted "); return 0; }