在 AVL 树上设置父级

发布于 2024-09-12 17:57:04 字数 1195 浏览 1 评论 0原文

我正在尝试实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2024-09-19 17:57:04

由于您这样做是为了教育,我建议您手动解决一些案例,然后将它们编码为

 Insert(6);
 Insert(11);
 // ...
 Insert(3);

 Node test = root;
 assert(root->height == 3);
 assert(root->value == 6);
 test = root->right;
 assert( ... );

完全由数字组成的形式的测试。

这将给

  1. 您带来更多关于您的代码是否正常工作的感觉。 (提示:如果您不确定您的代码是否正常工作,那么它可能无法正常工作)
  2. 为您提供一个开始找出问题所在的地方。
  3. 养成测试的习惯。对于某些人来说,编写两倍的代码(测试+代码)比一开始就编写代码要快,这是违反直觉的,但请尝试一下。加上编写更多代码=更多练习。

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

 Insert(6);
 Insert(11);
 // ...
 Insert(3);

 Node test = root;
 assert(root->height == 3);
 assert(root->value == 6);
 test = root->right;
 assert( ... );

the numbers are completely made up.

This will

  1. Give you more than a feeling as to whether or not your code is working. (Hint: if your are not sure your code is working, it probably isn't)
  2. Give you a place to start figuring out what is going wrong.
  3. Develop the habit of testing. It is counterintuitive to some people that writing twice as much code (tests + code) is faster than just writing the code in the first place, but try it. Plus writing more code = more practice.
零度° 2024-09-19 17:57:04

树和节点有什么区别? (树只是根节点的占位符,仅此而已。有时说一个节点有两个子树。树和节点没有什么不同。一个类就足够了。)

为什么不只在需要时计算高度?更容易编程和证明正确性。如果性能对您造成影响,您可以随时更改该函数以返回缓存的值。

节点不应该知道它的父节点(在我看来)。所以插入函数需要一个父参数。创建新节点后,比较父子节点的深度,看看是否需要进行任何旋转。对旋转进行编程很棘手:尝试、调试和测试!

Node::insert(int number,Node * parent)
{
  //code
  left=new node(number);
  if (parent->left->depth() > parent->right->depth()+1)
    parent->rotate_tree_or_something();
  //
}

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!

Node::insert(int number,Node * parent)
{
  //code
  left=new node(number);
  if (parent->left->depth() > parent->right->depth()+1)
    parent->rotate_tree_or_something();
  //
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文