Question

In: Computer Science

the following lists the nodes in a binary tree in two different orders: Preorder : A...

the following lists the nodes in a binary tree in two different orders:

Preorder : A B C D E F G H I J K L M

Inorder : C E D F B A H J I K G M L

Draw the binary tree

Solutions

Expert Solution

Answer:

In order to draw a binary tree from Preorder and Inorder traversals. We have to know some basics.

  • First element in Preorder will be the root of the tree.
  • Now search that element in Inorder, say you find it at position i, once you find it. make note of elements which are left to i ( this will construct the left sub tree ) and elements which are right to i ( this will construct the right subtree )
  • See the step above and recursively construct left subtree and recursively construct right subtree until you get a binary tree.

Given Preorder and Inorder traversals

  • Preorder = A B C D E F G H I J K L M
  • Inorder = C E D F B A H J I K G M L

First element in preorder is A. So A is root node. Search that element in Inorder.

From A -> Left we have ( C E D F B )

From A -> Right we have ( H J I K G M L)

                                               

Call recursively until you get the binary tree.

                                                         

The Fina binary tree will be

Thank you. Please ask me if you hav any doubt and upvote my answer if you like it.


Related Solutions

Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ā€˜Cā€™ to generate Fibonacci series using recursion            [8]
a tree is binary, if every node has at most two children nodes. prove that the...
a tree is binary, if every node has at most two children nodes. prove that the maximum of nodes in a binary tree of height h is 2^(h+1)-1
A binary tree model with 7 decision nodes will have how many terminal nodes?
A binary tree model with 7 decision nodes will have how many terminal nodes?
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
Create a kD tree with: x-nodes and y-nodes -- and maintain the following two properties: The...
Create a kD tree with: x-nodes and y-nodes -- and maintain the following two properties: The children of an x-node are y-nodes The children of a y-node are x-nodes Each type of node uses a different comparison to order points. This causes different levels of the tree to compare points differently, using the following rules: For every x-node: All descendants in the left subtree have a smaller x-coordinate than the point stored at the node. Visually, all descendant points are...
10) A binary tree with N nodes is at least how deep? How deep is it...
10) A binary tree with N nodes is at least how deep? How deep is it at most? 12) A Binary Search Tree is a binary tree with what additional property? 13) Beginning with an empty binary search tree, insert the following values in the order given. Draw the tree at each step of the process. 1 10 5 20 22 7 14) Now delete the value 10 from the tree in question 13. Show each of two possible configurations...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
java. Consider a binary tree with integer values in its nodes, implement a method that returns...
java. Consider a binary tree with integer values in its nodes, implement a method that returns the sum of the values contained in all of the nodes of the binary tree with root n.Hint: use recursion.
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder,...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder, level order), done nonrecursively 2)Ability to provide the result of traversing a tree in all traversals (inorder, preorder, postorder, level order)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT