I am trying to implement an AVL tree and not sure about the best way to do insert and track each node's parent. This is educational so please do not suggest "use boost" :)
This compiles but I am not convinced of its correctness or that it is the best way. Specifically this
insert(someNumber,root,root);
Also, I will redo the height part when I rebalance and move up the tree.
I insert like this
myTree.insert(someNumber);
Here is the method. I am not sure what my second param should be here. I would have thought NULL but the compiler does not like that (different function definition).
void Tree::insert(int someNumber){
insert(someNumber,root,root);
}
Here is my insert
void Tree::insert(int someNumber,Node*& subTreeRoot,Node*& parent){
if(subTreeRoot==NULL)
{
subTreeRoot = new Node(someNumber,parent);
if(subTreeRoot->myParent)
}
else if (someNumber<subTreeRoot->myNumber)
{
if((subTreeRoot->right==NULL)||((subTreeRoot->left!=NULL)&&(subTreeRoot->right!=NULL)))
subTreeRoot->height++;
insert(someNumber,subTreeRoot->left,subTreeRoot);
}
else
{
if((subTreeRoot->left==NULL)||((subTreeRoot->left!=NULL)&&(subTreeRoot->right!=NULL)))
subTreeRoot->height++;
insert(someNumber,subTreeRoot->right,subTreeRoot);
}
}
-