Feb 13, 2010 at 1:31am Feb 13, 2010 at 1:31am UTC
Hi i have weird doubt. My assingment is C++ program to insert and delete elements in Binary Search Tree.I am almost done wid the assingment. But the major part lies here. the strucure of the output is difficult to obtain.. i will explzin the question now.
Input:
F
D
R
P
M
N
V
Output:
F
+-- D
| +-- NULL
| \-- NULL
\-- R
+-- P
| +-- M
| | +-- NULL
| | \-- N
| | +-- NULL
| | \-- NULL
| \-- NULL
\-- V
+-- NULL
\-- NULL
I am done wid the ouput but iam not able to do the Vertical lines "|".. can anyone help me.. wid the logic. I am doing preorder...
Feb 13, 2010 at 2:08am Feb 13, 2010 at 2:08am UTC
I'd like to help, but I can't understand your problem....could you try and reword it for me?
maybe post some code as well?
Last edited on Feb 13, 2010 at 2:09am Feb 13, 2010 at 2:09am UTC
Feb 13, 2010 at 2:31am Feb 13, 2010 at 2:31am UTC
yea sure...
CODE for PREORDER arrangement of ELEMENTS....
void BST::inorder(struct Node *p,int b)
{
if(p!=NULL)
{
printf("%s",p->element);
cout<<endl;
if(p->left == NULL)
{
for(int i=4;i<b;i++)
{
cout<<" ";
}
cout<<"+-- NULL"<<endl;
}
else if(p->left!= NULL)
{
for(int i=4;i<b;i++)
{
cout<<" ";
}
cout<<"+-- ";
inorder(p->left,b+5);
}
if(p->right==NULL)
{
for(int i=4;i<b;i++)
{
cout<<" ";
}
cout<<"\\-- NULL"<<endl;
}
else if(p->right!=NULL)
{
for(int i=4;i<b;i++)
{
cout<<" ";
}
cout<<"\\-- ";
inorder(p->right,b+5);
}
}
}
CODE FOR INSERT OPERATION
void BST::insert(char* data)
{
Node* n = new Node;
strcpy(n->element,data);
n->left = NULL;
n->right = NULL;
if(root == NULL)
{
root = n;
cout<<endl;
cout<<"INSERTED: "<<data<<" as ROOT Element"<<endl;
cout<<endl;
n->breadth=0;
inorder(root,n->breadth);
return;
}
Node* tmp = root;
while(tmp)
{
if(strcmp(data,tmp->element)<0)
{
if(tmp->left == NULL)
{
tmp->left = n;
n->breadth=n->breadth+5;
cout<<endl;
cout<<"INSERTED: "<<data<<endl;
cout<<endl;
inorder(root,n->breadth);
return;
}
else
{
tmp = tmp->left;
}
}
else
{
if(tmp->right == NULL)
{
tmp->right = n;
n->breadth=n->breadth+5;
cout<<endl;
cout<<"INSERTED: "<<data<<endl;
cout<<endl;
inorder(root,n->breadth);
return;
}
else
{
tmp = tmp->right;
}
}
}
}
CLASS DECLARATION
class BST
{
public:
struct Node
{
char element[10];
Node* left;
Node* right;
int breadth;
};
Node* root;
BST()
{
root = NULL;
}
~BST()
{
}
void insert(char*);
void remove(char*);
void inorder(struct Node*,int);
void delorder(struct Node*,int);
};
Feb 13, 2010 at 3:43am Feb 13, 2010 at 3:43am UTC
Ok, we've got some code to work with, now what is the exact problem you're having?
Feb 13, 2010 at 7:06am Feb 13, 2010 at 7:06am UTC
HI.. i am able to display the output except the vertical lines.. "|".. i am not able to do dat... and my prof needs the output as given in ques... Can u help me...
Feb 17, 2010 at 11:33pm Feb 17, 2010 at 11:33pm UTC
What do you mean you can't display the pipes? It looked like you were able to type it well enough.