我试图通过遵循以下算法来构建一个基于数组的“二叉搜索树”:
...使用我想出了以下代码的算法:
void BST::insert( const data &aData )
{
item *y = &items[root_index]; // Algorithm calls for NULL assignment..
item *x = &items[root_index];
while ( ! items[root_index].empty )
{
y->theData = x->theData; // Ptrs are required or else a crash occurs.
if ( aData < x->theData )
{
x->leftChild = aData;
}
else
{
x->rightChild = items[root_index].theData;
}
// what is p[z] = y? is it outside the looping scheme?
root_index++; // and make the new child the root?
}
if ( y->empty )
{
items[root_index].theData = aData;
items[root_index].empty = false;
}
else if ( aData < y->theData )
{
y->leftChild = aData;
// If we already have a left/right child...make it the root? re-cmpr?
}
else
{
y->rightChild = items[root_index].theData;
}
}
问题:
我无法弄清楚 p[z] <- y 是什么意思....我只是增加根来模仿遍历。
如果我已经有一个左/右孩子,那么我应该让那个左/右孩子让我即将覆盖根?其中我应该让它递归,所以它会切换回原来的根,“R”?
插入 insert("R"); 插入(“A”);插入(“F”);插入(“L”);插入(“B”);插入(“C”);插入(“T”);