I am having difficulty printing out an Inorder binary tree. When I run my program and enter my input, it prints the Inorder after each character.
For example, if I enter ABCD, it will print:
Inorder: A
Inorder: AB
Inorder: ABC
Inorder: ABCD
However I only want to print out that last line.
This is the code that I have:
#include <iostream>
using namespace std;
template <class T>
class BinaryTree
{
private:
struct TreeNode
{
TreeNode *left;
TreeNode *right;
T data;
};
TreeNode *root;
public:
BinaryTree()
{
root = NULL;
}
void Inorder(TreeNode *n)
{
if(n != NULL)
{
Inorder(n -> left);
cout<< n -> data;
Inorder(n -> right);
}
}
void PrintInorder()
{
Inorder(root);
}
void InsertData(T data)
{
TreeNode *t = new TreeNode;
TreeNode *parent;
t -> data = data;
t -> left = NULL;
t -> right = NULL;
parent = NULL;
//is this a new tree?
if (isEmpty())
root = t;
else
{
TreeNode *curr;
curr = root;
while(curr)
{
parent = curr;
if (t -> data > curr -> data)
curr = curr -> right;
else
curr = curr -> left;
}
if(t -> data < parent -> data)
parent -> left = t;
else
parent -> right =t;
}
}
bool isEmpty()
{
return (root == NULL);
}
};
int main()
{
BinaryTree <char> BT;
char num;
while (cin >> num)
{
BT.InsertData(num);
cout << "Inorder: ";
BT.PrintInorder();
cout << endl;
}
return 0;
}
Don't print anything until you've read all the numbers.
while (cin >> num)
{
BT.InsertData(num);
}
cout << "Inorder: ";
BT.PrintInorder();
cout << endl;
Related
I am trying to create a binary tree using C++ classes and pointers. The tree is being initialized properly. However, when I add nodes to the tree, instead of adding them to the left or right sub-tree, the root node gets overwritten. I tried printing the inorder value to check if the nodes are added correctly or not, but that also prints nothing.
Here is the code for the same.
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <cstdio>
using namespace std;
class Node{
private:
Node *left;
Node *right;
public:
int number;
Node(int number){
this->number = number;
this->left = NULL;
this->right = NULL;
}
void set_left_node(Node *node){
this->left = node;
}
void set_right_node(Node *node){
this->right = node;
}
Node* read_left_node(){
return this->left;
}
Node* read_right_node(){
return this->right;
}
};
class Binary_Tree{
public:
Node *root;
Binary_Tree(){
this->root = NULL;
}
Node* read_root(){
return this->root;
}
Node* insert_node(Node *root, Node node){
if(!root){
root = &node;
cout << "Inserted " << node.number << " " << root << endl;
return root;
}
else{
cout << "Root and node values " << root->number << " " << node.number << endl;
if(root->number < node.number){
root->set_right_node(insert_node(root->read_right_node(), node));
}
else{
root->set_left_node(insert_node(root->read_left_node(), node));
}
}
return root;
}
void inorder(Node *root){
if (root != NULL){
inorder(root->read_left_node());
cout<<root->number<<" ";
inorder(root->read_right_node());
}
}
};
int main(){
Binary_Tree bt = Binary_Tree();
bt.root = bt.insert_node(bt.root, Node(34));
bt.root = bt.insert_node(bt.root, Node(17));
bt.root = bt.insert_node(bt.root, Node(56));
cout << "Inorder" << endl;
bt.inorder(bt.root);
return 0;
}
Output:
description
I don't know how to do this task.... but i just created a tree and enter value..can anyone please help me to do this task...the Stack is also of node type and we have to push value of operators like ab+ so we will push a as node then b as node and when + will come we make a tree and a and b will be its leafs node.
.Code
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *left;
Node *right;
Node()
{
data = 0;
left = NULL;
right = NULL;
}
};
class Tree
{
Node *root;
void insert(int d, Node *node)
{
if (d < node->data)
{
if (node->left == NULL)
{
Node *leaf = new Node();
leaf->data = d;
node->left = leaf;
}
else
{
insert(d, node->left);
}
}
else
{
if (node->right == NULL)
{
Node *leaf = new Node();
leaf->data = d;
node->right = leaf;
}
else
{
insert(d, node->right);
}
}
}
void inOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
inOrderDisplay(subRoot->left);
cout << subRoot->data << " ";
inOrderDisplay(subRoot->right);
}
}
void postOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
postOrderDisplay(subRoot->left);
postOrderDisplay(subRoot->right);
cout << subRoot->data << " ";
}
}
void preOrderDisplay(Node *subRoot)
{
if (subRoot != NULL)
{
cout << subRoot->data << " ";
preOrderDisplay(subRoot->left);
preOrderDisplay(subRoot->right);
}
}
void deleteSubtree(Node *subRoot)
{
if (subRoot != NULL)
{
deleteSubtree(subRoot->left);
deleteSubtree(subRoot->right);
cout << "\ndeleting: " << subRoot->data;
delete subRoot;
subRoot = NULL;
}
}
public:
Tree()
{
root = NULL;
}
~Tree()
{
deleteAll();
}
void insert(int d)
{
if (root == NULL)
{
Node *leaf = new Node();
leaf->data = d;
root = leaf;
}
else
{
insert(d, root);
}
}
void inOrderDisplay()
{
inOrderDisplay(root);
}
void postOrderDisplay()
{
postOrderDisplay(root);
}
void preOrderDisplay()
{
preOrderDisplay(root);
}
void deleteAll()
{
deleteSubtree(root);
}
};
.Main Class:
#include<iostream>
#include"task1.h"
using namespace std;
void main()
{
Tree tree;
tree.insert(10);
tree.insert(6);
tree.insert(14);
tree.insert(5);
tree.insert(8);
tree.insert(11);
tree.insert(18);
cout << endl;
system("pause");
//tree.deleteAll();
}
Based on the code you have here, you only have a void insert(int d, Node *node) function, no void insert(operator o, Node *node) function.
I think this shows that you missed an important point here. Every node in the tree can either be an integer (as you did) or an operator. In both cases, I'd call it a string. Every node that is not a leaf must be an operator, and all leafs must be integers (or strings that represents operators/integer in our case).
Then, iterating over your input, the first three item should result in something like:
+
/ \
a b
The next step would be to build more sub trees (not sure of the definition of the input you have), keep them in your stack and then construct more inner nodes of the tree.
So if the tree I showed above is called Tree(+) (for ease of use), and the initial stack was [a,b,+,c,d,e,*,*], then after one iteration you'll have [Tree(+),c,d,e,*,*] and you continue from there.
I have the following code. It creates a binary tree class. The functions are insert(), pre_order(), post_order(), in_order(). But when I debug it I get zeros values. Also I insert 9 values but only have 7 zeros. Why I did wrong?
#include <iostream>
using namespace std;
//Begin the construction of the BINARY TREE
struct tree_node {
tree_node *left;
tree_node *right;
int data;
};
//Declaring the class
class bst {
tree_node *root; //creating the root of the binary tree
public:
bst() {
root = NULL; //intialize the default construction, set the root to NULL
}
int is_empty() { //check for empty graph
return (root == NULL);
}
//Manipulating the Binary Tree
void insert(int item);
void remove_it(int value); //difficult implementation
//Graph Traversal of Binary Tree
void in_order_trav();
void in_order(tree_node *);
void pre_order_trav();
void pre_order(tree_node *);
void post_order_trav();
void post_order(tree_node *);
};
void bst::insert(int item) {
tree_node *p = new tree_node;
tree_node *parent;
p->left = NULL;
p->right = NULL;
parent = NULL;
if (is_empty()) {
root = p;
}
else {
tree_node *ptr;
ptr = root;
while (ptr != NULL) {
parent = ptr;
if (item > ptr->data)
ptr = ptr->right;
else
ptr = ptr->left;
}
if (item < parent->data)
parent->left = p;
else
parent->right = p;
}
}
/*************In Order Traversal*****************************/
// Begin
void bst::in_order_trav() {
in_order(root);
}
void bst::in_order(tree_node *ptr) {
if (ptr!=NULL) {
in_order(ptr->left);
cout << " " << ptr->data << " ";
in_order(ptr->right);
}
}
// End
/***********************************************************/
/*************Pre Order Traversal*****************************/
// Begin
void bst::pre_order_trav() {
pre_order(root);
}
void bst::pre_order(tree_node *ptr) {
if (ptr!=NULL) {
cout << " " << ptr->data << " ";
pre_order(ptr->left);
pre_order(ptr->right);
}
}
// End
/***********************************************************/
/*************Post Order Traversal*****************************/
// Begin
void bst::post_order_trav() {
post_order(root);
}
void bst::post_order(tree_node *ptr) {
if(ptr!=NULL) {
post_order(ptr->left);
post_order(ptr->right);
cout << " " << ptr->data << " ";
}
}
// End
/***********************************************************/
int main() {
bst bin_tree; //create the Binary Tree
bin_tree.insert(20);
bin_tree.insert(30);
bin_tree.insert(52);
bin_tree.insert(254);
bin_tree.insert(2);
bin_tree.insert(24);
bin_tree.insert(25);
bin_tree.insert(42);
bin_tree.insert(59);
bin_tree.in_order_trav(); //in order traversal
bin_tree.pre_order_trav(); //pre order traversal
bin_tree.post_order_trav(); //post order traversal
}
The node value should be initialized(p->data = item) at function insert() as below
void bst::insert(int item) {
tree_node *p = new tree_node;
tree_node *parent;
p->left = NULL;
p->right = NULL;
p->data = item;
parent = NULL;
... ...
}
Ok the solution is silly! -.-
I forgot to add that line in insert routine!
p->data = item;
This code is part of a RedBlack Tree program supposed to receive "ADSOMERT" and sort its characters into Inorder form "A D E M O R S T". It does the job, but with the ASCII numbers of each character "65 68 69 77 79 82 83 84".
Main function:
int main()
{
Tree char_tree = Tree();
cout << "Eingabe: ADSOMERT" << endl;
char_tree.insert('A');
char_tree.insert('D');
char_tree.insert('S');
char_tree.insert('O');
char_tree.insert('M');
char_tree.insert('E');
char_tree.insert('R');
char_tree.insert('T');
cout << "ADSOMERT in In-Order" << endl;
char_tree.print_inorder();
}
insert and rb_insert fucntions:
void Tree::insert(int x)
{
...
}
void Tree::rb_insert(Node *& node, Node *&parent, int x, bool sw)
{
...
}
inorder and print_inorder functions:
void Tree::print_inorder()
{
inorder(head->right);
cout << endl;
}
void Tree::inorder(Knote* node)
{
if (node != nullptr)
{
inorder(node->left);
cout << node->item << " ";
inorder(node->right);
}
}
class dependencies for Node and Tree:
Node.h
class Node {
public:
Node(int data = 0);
bool red;
int item;
Node *left;
Node *right;
Node *parent;
};
Node.cpp
Node::Node(int data)
{
this->item = data;
parent = nullptr;
left = nullptr;
right = nullptr;
red = true;
}
void Tree::inorder(Knote* node)
{
if (node != nullptr)
{
inorder(node->left);
cout << node->item << " ";
inorder(node->right);
}
}
class Node {
public:
Node(int data = 0);
bool red;
int item;
Node *left;
Node *right;
Node *parent;
};
You are outputting node->item and it is an int. Should be a char if you want to output "A D E M O R S T"
template <class T>
class Node{
public:
Node(int data = 0);
bool red;
T item;
....
};
Something like this
I am trying to build a Binary Tree, but I keep getting an error. When I call my Inorder() function in main() I get the error:
error: no matching function for call to 'BinaryTree :: Inorder()'.
I was hoping someone could help me figure this out.
#include <iostream>
using namespace std;
class BinaryTree{
private:
struct TreeNode{
TreeNode *left;
TreeNode *right;
int data;
};
TreeNode *root;
public:
BinaryTree(){
root = NULL;
}
void Inorder(TreeNode *p){
if(p != NULL){
Inorder(p -> left);
cout<< p -> data;
Inorder(p -> right);
}
}
void InsertData(int data){
TreeNode *t = new TreeNode;
TreeNode *parent;
t -> data = data;
t -> left = NULL;
t -> right = NULL;
parent = NULL;
//is this a new tree?
if (isEmpty())
root = t;
else{
TreeNode *curr;
curr = root;
while(curr){
parent = curr;
if (t -> data > curr -> data)
curr = curr -> right;
else
curr = curr -> left;
}
if(t -> data < parent -> data)
parent -> left = t;
else
parent -> right =t;
}
}
bool isEmpty(){
return root == NULL;
}
};
int main(){
BinaryTree BT;
int num;
while (cin >> num)
BT.InsertData(num);
cout << "Inorder: " << BT.Inorder() << endl;
return 0;
}
Inorder is declared like this:
void Inorder(TreeNode *p)
It needs a TreeNode * argument. Perhaps you intended to pass BT.Inorder( BT.root )?
Well, your void Inorder(TreeNode *p) takes an argument, while your function call cout << "Inorder: " << BT.Inorder() << endl;gives none.