Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function.
he main program has been written to input a sequence of integers and build a binary search tree, which you can then output. The input sequence is followed by a negative sentinel. Here's an example program run:
50 25 88 10 40 999 -1 Size: 6 Inorder: 10 25 40 50 88 999
Your job is to produce that last line of output, the inorder traversal of the tree with output to cout.
main.cpp is a read only file
#include <iostream>
#include "bst.h"
using namespace std;
int main()
{
binarysearchtree<int> tree;
int key;
//
// 1. Inputs values from the keyboard and builds a binary search
tree;
// reads input until the sentinel (a negative value) is input.
The
// resulting binary search tree is returned.
//
cin >> key;
while (key >= 0)
{
tree.insert(key);
cin >> key;
}
//
// 2. Output size and contents (in order):
//
cout << "Size: " << tree.size() << endl;
tree.inorder();
// done:
return 0;
}
bsh.h is the file to work on.
/*bsh.h*/
//
// Binary search tree
//
#pragma once
#include <iostream>
using namespace std;
template<typename TKey>
class binarysearchtree
{
private:
struct NODE
{
TKey Key;
NODE* Left;
NODE* Right;
};
NODE* Root; // pointer to root node of tree (nullptr if
empty)
int Size; // # of nodes in the tree (0 if empty)
//
// _inorder does the actual inorder traversal and output
// to console. Each key is output to the console followed
// by " ", including the last key.
//
void _inorder(NODE* cur)
{
//
// TODO:
//
}
public:
//
// default constructor:
//
// Creates an empty tree.
//
binarysearchtree()
{
Root = nullptr;
Size = 0;
}
//
// size:
//
// Returns the # of nodes in the tree, 0 if empty.
//
int size()
{
return Size;
}
//
// inorder:
//
// Performs an inorder traversal of the tree, outputting
// the keys to the console.
//
void inorder()
{
cout << "Inorder: ";
_inorder(Root);
cout << endl;
}
};
void _inorder(Node* cur)
{
if (cur == NULL)
return;
_inorder(cur->left);
cout << cur->data << " ";
_inorder(cur->right);
}
This is a recursice solution for inorder traversal of a tree.
It will go to the leftmost node then print that and then its right node and then backtrack.
Try to understand the recursion behind the code.
First dry run the code with sample input.
Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public...
C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...
Write a method that determines the key of the successor of the root node in a binary search tree. For any input binary search tree, find the key of successor of the root node.Note: Successor is the node with the next highest key, should work for any binary search tree - not just the given example input. #include <iostream> using namespace std; class Node { private: int key; string val; Node* left; Node* right; friend class BinarySearchTree; }; class BinarySearchTree {...
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...
Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
[Python]
Construct Tree Using Inorder and Preorder
Given Preorder and Inorder traversal of a binary tree, create
the binary tree associated with the traversals.You just need to
construct the tree and return the root.
Note: Assume binary tree contains only unique elements.
Input format :
Line 1 : n (Total number of nodes in binary tree)
Line 2 : Pre order traversal
Line 3 : Inorder Traversal
Output Format :
Elements are printed level wise, each level in new line...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
Write a client method that returns a count of the number of
nodes in a binary search tree that contain scores less than or
equal to a passed-in argument (parameter) value. The method header
is:
int countLowerScores (BinarySearchTree tree, int maxValue)
The BinarySearchTree contains these methods in the
picture.
public class BinarySearchTreecT> implements BSTInterfacecT> //reference to the root of this BST public BSTNode<T> root; public Comparator<T> comp: IIused for all comparisons public boolean found: / used by remove public BinarYSearchTree()...
Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree { class Node{ int key; Node left,right; public Node(int item) { key = item; left = right = null; } } Node root; public void BinaryTree(){ root = null; } void...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...