(Qt | C++ | Morris)实现二叉树的可视化遍历和创建

项目环境:win10  Qt5.15.2

运行效果如下:

创建一颗二叉树

 开始遍历过程前,要先选择一种遍历方式

 遍历过程(这里只演示先序遍历和morris遍历

 

 此外,实现了两个侧滑页

 

 

 项目地址:https://github.com/troublemkerrr/VisualTree

喜欢的话点个star吧~若有问题请私信我,或者在github我的项目中发布issue

morris遍历可视化部分代码

void MyGraphicsView::morris(MyGraphicsVexItem * head)
{
    MyGraphicsVexItem * cur = head;
    MyGraphicsVexItem * mostRight = nullptr;//the rightmost node of cur's left child

    while (cur!=nullptr && cur->nameText != "nullptr") { //when cur is nullptr,stop
        addAnimation(cur->visit());
        mostRight = cur->left;
        if (mostRight != nullptr && mostRight->nameText != "nullptr")
        { //cur has left subtree
            while (mostRight->right->nameText != "nullptr" && mostRight->right->nameText != cur->nameText) { //找左树上最右节点
                mostRight = mostRight->right;
            }
            if (mostRight->right->nameText == "nullptr") { //arrived cur for the first time
                addAnimation(changeName(cur->nameText,mostRight->right));
                cur = cur->left;
                continue;
            } else { //second time
                addAnimation(changeName("nullptr",mostRight->right));
            }
        }
        //cur has no left subtree,or come to cur for the second time
        if(cur->right->nameText=="nullptr"){
            cur=cur->right;
        }else{
            for(int i=0;i<vexes.size();i++)
            {
                if(vexes[i]->nameText==cur->right->nameText){
                    cur=vexes[i];
                    break;
                }
            }
        }
    }
}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值