내 BST에 조금만 더 도움이 필요합니다. 이것은 내 BST를 삽입 할 때 모습입니다 :이진 검색 트리 C++ (학부모)
R은, L은, J,
R --Root at Index 0
/\
L @ Index1 L NULL
/\
J @ Index3 J NULL
/\
G @ Index7 G NULL
G
이 여기 일이 만드는 코드입니다.void BST::insert(const data &aData)
{
if (items[Parent].empty)
{
items[Parent].theData = aData; // insert at leaf.
items[Parent].empty = false;
size++;
return;
}
for (int i = 0; i <= size; i++)
{
if (aData < items[Parent].theData)
{
if (items[2*i+1].empty)
{
items[2*i+1].theData = aData;
items[2*i+1].empty = false;
}
else
{
// we must already have a left child to some root.
Parent++; So make the previous data the root???
if (items[Parent].empty)
{
items[Parent].theData = items[2*i+1].theData;
items[Parent].empty = false;
Parent = (i-1)/2;
}
}
}
else
{ ...// do the same for data greater than but with items[2*i+2] }
내 질문은 언제 새로운 루트를 만들어야합니까? 언제 새 루트를 만들어야합니까? 재평가를 위해?
이 방법이 맞습니까? 둘 다 내 게시물을 보는 사람들에게 감사드립니다 :)
// 생성자 BST 클래스 및 해당 개인 섹션. (내가 말을해야 오히려 퍼지)
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
전체 코드를 포함 할 수 있도록 문제를 단순화 할 수 있습니까? 또는 (완전한) 의사 코드로 작성 하시겠습니까? 'item','Parent','data' 등이 무엇인지 알려주지 않으면 여러분이하고있는 것을 해독하기가 어렵습니다. –
왜 배열을 사용하고 있습니까? –
나는 데이터에 대한 my 배열을 정의하는 생성자와 private 섹션을 게시한다. 미안합니다! – user40120