#include<iostream>
using namespace std;
//Node class
class Node{
private:
int data;
Node* left;
Node* right;
friend class Tree;
public:
//constructors
Node(){
data=0;
left=right=NULL;
}
Node(int d){
data=d;
left=right=NULL;
}
};
//TREE
class Tree{
private:
Node* root;
public:
//INSERT function
Node* insert(Node* root, int data, bool a){
Node* ptr=new Node(data);
Node* temp_Root=NULL;
if(root==NULL){
root=ptr;
return root;
}
else if(a==0){
temp_Root=root->left=insert(root->left, data, a);
}
else{
temp_Root=root->right=insert(root->right, data, a);
}
return temp_Root;
}
//DISPLAY
void display_Post_Order(Node* root){
if(root==NULL)
return;
display_Post_Order(root->left);
display_Post_Order(root->right);
cout<<root->data<<" ";
}
};
//MAIN function
int main(){
//We create a pointer pointing that will point to a Node
Node* root;
//Initially(i.e tree is empty) root will be pointing to NULL
root=NULL;
Tree tree1;
root = tree1.insert(root, 2, 1);
Node* root_A=tree1.insert(root, 14, 0);
Node* root_B=tree1.insert(root, 5, 1);
Node* root_C=tree1.insert(root_A, 32, 0);
Node* root_D=tree1.insert(root_A, 44, 1);
Node* root_E=tree1.insert(root_B, 18, 0);
tree1.insert(root_B, 90, 1);
tree1.insert(root_C, 50, 0);
tree1.insert(root_C, 64, 1);
tree1.insert(root_D, 53, 0);
tree1.insert(root_D, 43, 1);
tree1.insert(root_E, 57, 0);
tree1.insert(root_E, 23, 1);
//display the contents of a BST in ascending order
tree1.display_Post_Order(root);
}
Comments
Post a Comment