Question

In: Computer Science

Draw a (single) tree T, such that • Each internal node of T stores a single...

Draw a (single) tree T, such that

• Each internal node of T stores a single character;

• A preorder traversal of T yields: E K D M J G I A C F H B L;

• A postorder traversal of T yields: D J I G A M K F L B H C E.

Part2

Let T be an ordered tree with more than one node. Is it possible that the preorder traversal of T visits the nodes in the same order as the post order traversal of T ? If so, give an example. If not, then explain why this cannot occur.

Solutions

Expert Solution

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

// Data structure to store a Binary Tree node
struct Node
{
   int data;
   Node *left, *right;
};

Node* newNode(int key)
{
   Node* node = new Node;
   node->data = key;
   node->left = node->right = nullptr;

   return node;
}

void inorder(Node* root)
{
   if (root == nullptr)
       return;

   inorder(root->left);
   cout << root->data << ' ';
   inorder(root->right);
}

Node* buildTree(auto &preorder, int &pIndex,
               int start, int end, auto &map)
{

   Node* root = newNode(preorder[pIndex]);

   pIndex++;

   if (pIndex == preorder.size())
       return root;

   int index = map[preorder[pIndex]];

   if (start <= index && index + 1 <= end - 1)
   {
     
       root->left = buildTree(preorder, pIndex, start, index, map);

         
       root->right = buildTree(preorder, pIndex, index + 1, end - 1, map);
   }

   return root;
}


Node* buildTree(auto const &preorder, auto const &postorder)
{

   unordered_map<int,int> map;
   for (int i = 0; i < postorder.size(); i++)
       map[postorder[i]] = i;

   int pIndex = 0;

   int start = 0;
   int end = preorder.size() - 1;

     
   return buildTree(preorder, pIndex, start, end, map);
}

int main()
{
   vector<int> preorder = { 1, 2, 4, 5, 3, 6, 8, 9, 7 };
   vector<int> postorder = { 4, 5, 2, 8, 9, 6, 7, 3, 1 };

   Node* root = buildTree(preorder, postorder);

   cout << "Inorder Traversal: ";
   inorder(root);

   return 0;
}


Related Solutions

Draw a (single) binary tree T, such that  Each internal node of T stores a...
Draw a (single) binary tree T, such that  Each internal node of T stores a single character  A preorder traversal of T yields ALGORITHMS  An inorder traversal of T yields GOLATIHRMS
1. Draw a binary search tree as a single root node holding a string as the...
1. Draw a binary search tree as a single root node holding a string as the data element. Each string inserted into a node in the tree will be for a character in a game. Then, draw a new tree each time you insert a new node into the tree holding a string Insert 4 nodes total, including the root. This means the new nodes will need to be inserted at the correct child to maintain the BST property.
Draw the binomial tree listing only the option prices at each node. Assume the following data...
Draw the binomial tree listing only the option prices at each node. Assume the following data on a 6-month call option, using 3-month intervals as the time period. K = $40, S = $37.90, r = 5.0%, σ = 0.35
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
A binary tree is a rooted tree in which each node has at most two children....
A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree the number of nodes with two children is exactly one less than the number of leaves.
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
(+5) Level of a node in a binary tree is distance from root to that node....
(+5) Level of a node in a binary tree is distance from root to that node. For example, level of root is 0 and levels of left and right children of the root are 1. Level      Max number of nodes 1 2 4 8 16 32 64 ..      … n       ?? The maximum number of nodes on level n of a binary tree is : A          2^(n-1)                         B          2^n C          2^(n+1)                       D            2^[(n+1)//2] In the above answers, the operator...
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
in C++, given a binary search tree of ints, in which each node contains a size...
in C++, given a binary search tree of ints, in which each node contains a size parameter (also an int), explain, in English, not code, how you would find the median element of the tree in theta(log N) time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT