1
나는 그것을 재귀 적으로 시도했다. 부모 정수인 varaible은 2*i +1
이 leftChild
이고 오른쪽이 2*i +2
인 공식과 일치한다.배열 BST의 삽입 정렬은 어떻게 작동합니까?
void BST::insert(const data &aData)
{
if (items[Parent].empty)
{
items[Parent].theData = aData;
items[Parent].empty = false;
}
else if (aData < items[Parent].theData)
{
Parent = 2 * Parent + 1;
if (Parent >= maxSize) this->reallocate();
this->insert(aData);
}
else
{
Parent = (2 * rightChild++)+ 2;
if (Parent >= maxSize) this->reallocate();
this->insert(aData);
}
}
원래 부모보다 작은 항목 ... 를 삽입 할 때 그것은 잘 작동하지만 난 뭔가를 찾을 때가 모든 나사 그것을 큰 : X 여기
void BST::reallocate()
{
item *new_array = new item[maxSize*2];
for (int array_index = 0; array_index < maxSize; array_index++)
{
if (! items[array_index].empty)
{
new_array[array_index].theData = items[array_index].theData;
}
}
maxSize *= 2;
delete [] items;
items = NULL;
items = new_array;
}
내 ctor에 이렇게이다 아무도 내가 다음 더 이상 혼란을 얻을 수 없습니다 :
BST::BST(int capacity) : items(new item[capacity]), size(0), Parent(0),
leftChild(0), rightChild(0)
{
items->empty = true;
maxSize = capacity;
}
private:
int size; // size of the ever growing/expanding tree :)
int Parent;
int maxSize;
int leftChild;
int rightChild;
struct item
{
bool empty;
data theData;
};
item *items; // The tree array
위의 삽입 기능은 .. 사실은 내가이 얻을 수있는 최선
R
/\
/ \
/ \
L X
/\ /\
J V K T <--The only out of place node.
/\ \
/NULL \
G /
P
삽입 할 때 : R, L, J, G, X, K, V, P, T
를 나는 당신의 문제를 의심 할
이것은 재귀 적으로 정의 된 삽입 함수로, 완료하려고했습니다. – user40120
T가 삽입 된 마지막 항목입니다. 그 원인입니까? – user40120