我正在一個二叉搜索樹.. 我有一個問題讓節點左對齊輸出。我正在打印陣列順序,預訂,inorder,postorder ...陣列順序格式左對齊,就像我想要它.. 我基本上使用相同的代碼(setw和左;在同一順序)作爲數組順序因爲我與preo訂單,其餘.. 但是,它不會左對齊..問題格式化輸出c + +
我已經嘗試了一切.. 任何技巧?
正確的輸出..
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
這裏是序代碼。功能其餘的都是在其成立輸出的方式幾乎相同..
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);
}
}
這裏是左對齊排列順序代碼..
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);
}
}
這個功課? – 2009-12-08 07:14:54