'how to print binary tree in order?
What's wrong with my code?
this is the header file code: I don't know what's the problem with it but the error is in get data function I'm suffering from dealing with private data can you help me with this topic? what I should do to get this code correct?
class Node
{
private:
int Data;
Node* Right;
Node* Left;
public:
Node()
{
Data = 0;
Right = NULL;
Left = NULL;
}
Node(int data)
{
Data = data;
Right = NULL;
Left = NULL;
}
Node* GetLeft()
{
return Left;
}
Node* GetRight()
{
return Right;
}
int GetData()
{
return Data;
}
void SetLeft(Node*n)
{
Right = n;
}
void SetRight(Node*n)
{
Left = n;
}
void SetData(int data)
{
Data = data;
}
};
class MyBinaryTree
{
private:
Node* root;
public:
MyBinaryTree()
{
root = NULL;
}
MyBinaryTree(int data)
{
root->SetData(data);
root->SetLeft(NULL);
root->SetRight(NULL);
}
Node* InsertNode(Node* n)
{
if (root == NULL)
{
root = n;
return root;
}
if (n->GetData() < root->GetData())
root->SetLeft(InsertNode(root->GetLeft()));
else
root->SetRight(InsertNode(root->GetRight()));
return root;
}
void print_inorder()
{
if (root == NULL)
return;
// process the left subtree
root = root->GetLeft();
print_inorder();
// process the node
cout << root->GetData() << " ";
// process the right subtree
root=root->GetRight();
print_inorder();
}
};
and this is the main: (I think the error is here)
#include <iostream>
using namespace std;
#include"Header.h"
int main()
{
int data1[10] = { 50,40,90,30,45,60,180,55,54,81 };
int data2[] = { 50,40,90,30,45,60,180,55,54,68 };
MyBinaryTree tree1(data1[0]);
for (int i = 1; i < 10; i++)
{
Node* n = new Node;
n->SetData(data1[i]);
tree1.InsertNode(n);
tree1.print_inorder();
}
}
Solution 1:[1]
I have two solutions to this problem (written in C++ as it is the language of your problem). But first here is my setup:
#include <iostream>
#include <memory>
struct Node
{
int value;
std::unique_ptr<Node> p_left;
std::unique_ptr<Node> p_right;
Node(int value)
: value(value) {}
Node(int value, std::unique_ptr<Node> p_left, std::unique_ptr<Node> p_right)
: value(value), p_left(std::move(p_left)), p_right(std::move(p_right)) {}
};
- I wrote my own
Nodestruct for simplicity - I used
std::unique_ptrinstead of raw pointers so that I don't have to manually deallocate memory. This is considered the more "modern" approach and should generally be preferred.
Here is my first solution. It prints the given node's value as well as the value of every one of its children on the left-hand side subbranch. Then it repeats for its right-hand side child.
#include <stack>
// Print all left-hand side children of the given node
// Do the same with its right child
void printBranchByBranch(const Node& root)
{
std::stack<const Node*> nodes({&root});
while (!nodes.empty())
{
// Print the top node (starting at root)
const Node& topNode(*nodes.top());
std::cout << topNode.value << '\n';
// Remove it from the stack so that it is not processed again
nodes.pop();
// Push the left and right child nodes to the stack if they exist
if (topNode.p_right)
nodes.push(topNode.p_right.get());
if (topNode.p_left)
nodes.push(topNode.p_left.get());
}
}
As you can see, I used the stack data structure to implement this algorithm, though it is a great candidate for recursion.
Here is my second approach. It prints every layer of the tree which has the given node as root, top to bottom, left to right.
// Print every node of the tree, from top to bottom, left to right
void printLayerByLayer(const Node& root)
{
std::queue<const Node*> nodes({&root});
while (!nodes.empty())
{
// Print the front node (starting at root)
const Node& frontNode(*nodes.front());
std::cout << frontNode.value << '\n';
// Remove it from the stack so that it is not processed again
nodes.pop();
// Push the left and right child nodes to the queue if they exist
if (frontNode.p_left)
nodes.push(frontNode.p_left.get());
if (frontNode.p_right)
nodes.push(frontNode.p_right.get());
}
}
Similarly, I used the stack's sister, the queue in my implementation. This is because I don't want to print the children a given node immediately, I want to finish processing the whole node layer first.
Here is my test case if you want to test my solutions yourself:
int main()
{
Node root {
0,
std::make_unique<Node>(
10,
std::make_unique<Node>(
20, nullptr, nullptr
),
nullptr
),
std::make_unique<Node>(
11,
std::make_unique<Node>(
22,
nullptr,
std::make_unique<Node>(
30, nullptr, nullptr
)
),
std::make_unique<Node>(
23, nullptr, nullptr
)
)
};
std::cout << "Printing the tree branch by branch:\n";
printBranchByBranch(root);
std::cout << "Printing the tree layer by layer:\n";
printLayerByLayer(root);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
