




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





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