项目环境: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;
}
}
}
}
}