tags:

views:

100

answers:

2

I am working on a binary search tree.. I am having a problem getting the nodes to left align on output. I am printing array order, pre-order,inorder,postorder... Array order format is left aligned like i want it to be.. Im basically using the same code( setw and left; in the same order) as array order as i am with preo order, and the rest.. However, it wont left align..

I have tried everything.. any tricks?

correct output..

Database Of Great Computer Scientists

>>> array order:

name                       leaf?  index
----                       -----  -----
Ralston, Anthony                      0
Liang, Li                             1
Von Neumann, John                     2
Jones, Doug                           3
Trigoboff, Michael                    5
Goble, Colin               leaf       7
Knuth, Donald                         8
Turing, Alan               leaf      12
Kay, Alan                  leaf      17
(items printed)                          (9)

>>> preorder:


name                       leaf?  index
----                       -----  -----
Ralston, Anthony                      0
Liang, Li                             1
Jones, Doug                           3
Goble, Colin               leaf       7
Knuth, Donald                         8
Kay, Alan                  leaf       17
Von Neumann, John                     2
Trigoboff, Michael                    5
Turing, Alan               leaf       12
(items printed)                          (9)

>>> inorder:


name                       leaf?  index
----                       -----  -----
Goble, Colin               leaf       7
Jones, Doug                           3
Kay, Alan                  leaf       17
Knuth, Donald                         8
Liang, Li                             1
Ralston, Anthony                      0
Trigoboff, Michael                    5
Turing, Alan               leaf       12
Von Neumann, John                     2
(items printed)                          (9)

>>> postorder:


name                       leaf?  index
----                       -----  -----
Goble, Colin               leaf       7
Kay, Alan                  leaf       17
Knuth, Donald                         8
Jones, Doug                           3
Liang, Li                             1
Turing, Alan               leaf       12
Trigoboff, Michael                    5
Von Neumann, John                     2
Ralston, Anthony                      0
(items printed)                          (9)

>>> retrieve Trigoboff, Michael

Trigoboff, Michael

>>> retrieve Kaye, Danny

not found

here is the code for inorder. The rest of the functions are almost identical in the way its set up to output..

void BST::displayInOrder(ostream& out, int parent)const
{
if (parent <= maxsize) 
{

this->displayInOrder(out, 2 * parent + 1);  

if (!items[parent].empty)
{
out << left
<< setw(27) << items[parent].instanceData;
out << right
<< setw(10);
if(!items[parent].isLeaf)
{
out << left
 << setw(11) << "leaf";

}
out << setw(12) << parent;
out << endl;
}  

this->displayInOrder(out, 2 * parent + 2 ); 

}

if(!parent)
{
itemsPrinted(out,size);
}

}

and here is the array order code that left aligns..

void BST::displayArrayOrder (ostream& out) const
{

out << ">>> array order:" << endl;
displayHeaders(out);


for(int index=0; index < maxsize+1; index++)
{

 if (!items[index].empty) 
 {
 out << left
 << setw(27) << items[index].instanceData;
 out << right
 << setw(10);

 if(index > size)
 {
 items[index].isLeaf = false;
 out << left

 << setw(10) << "leaf";
 }
 if(items[2*index+1].empty && items[2*index+2].empty && index < size)
 {
  items[index].isLeaf = false;
  out << left

 << setw(11) << "leaf";
 }

out << setw(12) << index;
 out << endl;


 }


 }
 if(!parent)
 {
 itemsPrinted(out,size);
 }


   }
+3  A: 

Do yourself a favor: write one function for displaying a record, and use it anywhere you need to display that type of record. Since you already have code that works in one place, just use that code inside the body of your print_record() (or whatever you decide to call it).

One thing you should learn nearly as soon as possible: duplicating the same basic code three or four times over is a really bad thing, and you should avoid it.

Jerry Coffin
The order in which setw and left are held within the function is the same. Each function displays inorder, or postorder and that code is different. In this situation i have to check for different things and therefore would be too difficult to get another display function to work with the display function.
Corey
The order in which you visit the nodes should make no difference to the way in which you display data from an individual node.
Jerry Coffin
+1  A: 

As I see your code, the displayArrayOrder doesn't just display the order but also sets the isLeaf property, that property is then used in the other display functions directly.

As Jerry Coffin noted, split those things of, and use the same print record function in all your display functions.

Something like this should get you started. (apparently, you print leaf when isLeaf is false, I kept that convention)

bool BST::hasToPrintLeaf(int index) {
  if (!items[index].isLeaf) {
    return true
  }
  if (index > size ||
      (index < size && items[2*index+1].empty && items[2*index+2].empty)) {
    items[index].isLeaf = false;
    return true;
  }
}

void BST::printRecord(std::ostream & out, int index) {
  out << left << setw(27) << items[index].instanceData;
  out << right << setw(10);

  if(hasToPrintLeaf(index)) {
    out << left << setw(11) << "leaf";
  }

  out << setw(12) << index << endl;
}

Note that I didn't really change anything with regard to your logic, while I have my doubts about some things... (your two leaf tests exclude index == size as a case for example)

Pieter
This actually works well. I used it for the post order, inorder, preorder.. However, when i print "leaf" i cant get the 7 to line up like the other node leaf numbers do. Probably because its a single digit and the others are not.
Corey