Question

In: Computer Science

C++ Build a binary tree using a binary tree class member function from the following array...

C++

Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

#include <bits/stdc++.h>
using namespace std;

class node
{
public:
        int data;
        node *left;
        node *right;
};

/* Prototypes for utility functions */
int search(int arr[], int strt, int end, int value);
node *newNode(int data);

//Construct the tree using PreOrder and Inorder
node *buildTree(int in[], int pre[], int inStrt, int inEnd)
{
        static int preIndex = 0;
        if (inStrt > inEnd)
                return NULL;
        node *tNode = newNode(pre[preIndex++]);
        /* If this node has no children then return */
        if (inStrt == inEnd)
                return tNode;
        /* Else find the index of this node in Inorder traversal */
        int inIndex = search(in, inStrt, inEnd, tNode->data);
        /* Using index in Inorder traversal, construct left and  
    right subtress */
        tNode->left = buildTree(in, pre, inStrt, inIndex - 1);
        tNode->right = buildTree(in, pre, inIndex + 1, inEnd);
        return tNode;
}

/* Function to find index of value in arr[start...end]  
The function assumes that value is present in in[] */
int search(int arr[], int strt, int end, int value)
{
        int i;
        for (i = strt; i <= end; i++)
        {
                if (arr[i] == value)
                        return i;
        }
        return 0;
}

/* Helper function that allocates a new node with the  
given data and NULL left and right pointers. */
node *newNode(int data)
{
        node *Node = new node();
        Node->data = data;
        Node->left = NULL;
        Node->right = NULL;
        return (Node);
}
/* This funtcion is here just to test buildTree() */
void printPostorder(node *node)
{
        if (node == NULL)
                return;
        printPostorder(node->left);
        printPostorder(node->right);
        cout << node->data << " ";
}

/* Driver code */
int main()
{
        int inorder[] = {9, 3, 15, 20, 7};
        int preorder[] = {3, 9, 20, 15, 7};
        int len = sizeof(inorder) / sizeof(inorder[0]);
        node *root = buildTree(inorder, preorder, 0, len - 1);
        cout << "PostOrder traversal of the constructed tree is \n";
        printPostorder(root);
}

Related Solutions

Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode* find(int i) { // implement } If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right;...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of...
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element.""" def __init__(self, container, node): def element(self): def __eq__(self, other): # ------------------------------- utility methods ------------------------------- def _validate(self, p): """Return associated node, if position is valid.""" def _make_position(self, node): """Return Position...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
(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...
Build a binary search tree with the following words. Insert them in an order so that...
Build a binary search tree with the following words. Insert them in an order so that the tree has as small a depth as possible. (Consider the insertion order of the words) Print the tree after,also, any, back, because, come, day, even, first, give, how, its, look, most, new, now, only, other, our, over, than, then, these, think, two, us, use, want, way, well, work. C++
Create a new project to build a Binary search tree, and do the following: Create a...
Create a new project to build a Binary search tree, and do the following: Create a TreeNode class, Add the methods "Insert" to insert new nodes to the tree. Add the method "inorder". Make the method to return a list of the node elements in inorder. Implement the equals method in the BST. Two BST are equal if they contain the same elements. In the main insert the elements of the tree, call the max method and print the max...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT