在 AVL 树上设置父级
我正在尝试实现 AVL 树,但不确定插入和跟踪每个节点的父节点的最佳方法。这是有教育意义的,所以请不要建议“使用 boost”:)
这可以编译,但我不相信它的正确性或它是最好的方法。 具体来说
insert(someNumber,root,root);
,当我重新平衡并向上移动树时,我将重做高度部分。
我这样插入
myTree.insert(someNumber);
这是方法。我不确定我的第二个参数应该在这里。我本以为 NULL 但编译器不喜欢这样(不同的函数定义)。
void Tree::insert(int someNumber){
insert(someNumber,root,root);
}
这是我的插入内容
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);
}
}
-
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);
}
}
-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您这样做是为了教育,我建议您手动解决一些案例,然后将它们编码为
完全由数字组成的形式的测试。
这将给
Since you are doing this for education, I would suggest that you work some cases out by hand, then code them into tests of the form
the numbers are completely made up.
This will
树和节点有什么区别? (树只是根节点的占位符,仅此而已。有时说一个节点有两个子树。树和节点没有什么不同。一个类就足够了。)
为什么不只在需要时计算高度?更容易编程和证明正确性。如果性能对您造成影响,您可以随时更改该函数以返回缓存的值。
节点不应该知道它的父节点(在我看来)。所以插入函数需要一个父参数。创建新节点后,比较父子节点的深度,看看是否需要进行任何旋转。对旋转进行编程很棘手:尝试、调试和测试!
What's the difference between a Tree and a Node? (A Tree is only place holder for the root Node, nothing more. A node is sometimes said to have two subtrees. Tree and Node are no different. One class would suffice.)
Why don't just calculate the height whenever you need it? Much easier to program and proof correct. If performance is hurting you, you can always change the function to return a cached value.
A node should not know it's parent (in my opinion). So the insert function needs a parent parameter. After creating the new node compare the parents-childrens depths to see if you need to do any rotations. Programming the rotations is tricky: try, debug and test!