You are on page 1of 5

#include<iostream>

#include<stack>

#include<queue>

using namespace std;

class BST;

class Node

private:

int data;

Node*left;

Node*right;

public:

Node(int data=0)

this->data=data;

this->left=NULL;

this->right=NULL;

friend class BST;

};

class BST

Node*root;

public:

BST(void)

this->root=NULL;
}

bool empty(void)

return this->root==NULL;

void insert(int data)

Node*node=new Node(data);

if(root==NULL)

this->root=node;

else

Node*temp=this->root;

// Node*prev=NULL;

while(true)

if(data< temp->data)

if(temp->left==NULL)

temp->left=node;

break;

temp=temp->left;

else

if(temp->right==NULL)

temp->right=node;
break;

temp=temp->right;

// balance(this->root,NULL);

Node*getRoot()

return this->root;

void levelorder(void)

if(root==NULL)

return;

queue<Node*>que;

que.push(this->root);

while(!que.empty())

int count=que.size();

while(count>0)

Node*temp=que.front();

cout<<temp->data<<"";
que.pop();

if(temp->left!=NULL)

que.push(temp->left);

if(temp->right!=NULL)

que.push(temp->right);

count--;

cout<<endl;

};

int main()

int root;

BST b;

int cmd;

while(cmd!=-1)

cin>>cmd;

if(cmd!=-1)

b.insert(cmd);
}

b.levelorder();

return 0;

You might also like