当前位置:网站首页>Binary tree traversal
Binary tree traversal
2022-06-30 07:09:00 【HL_ Aeolus】

Child representation :
typedef struct treeNode {
char data;
struct treeNode * lChild;
struct treeNode * rChild;
}treeNode,* pTreeNode;
Create a tree node
pTreeNode createTreeNode(char data)
{
pTreeNode p = new treeNode;
p->data = data;
p->lChild = nullptr;
p->rChild = nullptr;
return p;
}
Insert tree node
void insertTreeNode(pTreeNode parentNode,pTreeNode l,pTreeNode r)
{
if(parentNode != nullptr)
{
parentNode->lChild = l;
parentNode->rChild = r;
}
}
Print out current tree node data
void printfCurTreeNode(pTreeNode node)
{
if(nullptr != node)
{
O << node->data << " ";
}
}
Three traversal methods , Recursive implementation
// The former sequence traversal root Left Right
void preOrder(pTreeNode root)
{
if(nullptr != root)
{
printfCurTreeNode(root);
preOrder(root->lChild);
preOrder(root->rChild);
}
}
// In the sequence traversal Left root Right
void midOrder(pTreeNode root)
{
if(nullptr != root)
{
midOrder(root->lChild);
printfCurTreeNode(root);
midOrder(root->rChild);
}
}
// After the sequence traversal Left Right root
void lastOrder(pTreeNode root)
{
if(nullptr != root)
{
lastOrder(root->lChild);
lastOrder(root->rChild);
printfCurTreeNode(root);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
pTreeNode A = createTreeNode('A');
pTreeNode B = createTreeNode('B');
pTreeNode C = createTreeNode('C');
pTreeNode D = createTreeNode('D');
pTreeNode E = createTreeNode('E');
pTreeNode F = createTreeNode('F');
pTreeNode G = createTreeNode('G');
insertTreeNode(A,B,C);
insertTreeNode(B,D,nullptr);
insertTreeNode(D,nullptr,G);
insertTreeNode(C,E,F);
O << "\n The former sequence traversal :\n";
preOrder(A);
O << " In the sequence traversal :\n";
midOrder(A);
O << "\ After the sequence traversal :\n";
lastOrder(A);
return 0;
}
Output effect :
边栏推荐
- 大学刚毕业不知道做什么工作怎么办?
- [mask RCNN] target detection and recognition based on mask RCNN
- Vs2019 and SQL
- Introduction to go language pointer
- Ftplib+ tqdm upload and download progress bar
- [hot100] palindrome substring and longest palindrome substring
- The class imported by idea import clearly exists, but it is red?
- Linux服務器安裝Redis
- Out of class implementation of member function of class template
- Linux服务器安装Redis
猜你喜欢

Installation du serveur linux redis

Pit stepping record: Supervisor log return information: redis extension is not installed

Unity中实现溶解(Dissolve)特效及其原理解析

IDEA import导入的类明明存在,却飘红?

The maximum expression in Oracle database message list is 1000 error
![[hot100] palindrome substring and longest palindrome substring](/img/a5/10dec640f02023c4d55cb42e6309fb.png)
[hot100] palindrome substring and longest palindrome substring

Go常用命令

Google Earth engine (GEE) - Murray global tidal wetland change V1 (1999-2019) data set

leetcode:98. 验证二叉搜索树

【最全】linux服务器上安装Mysql
随机推荐
踩坑记录:supervisor 日志返回信息:redis扩展未安装
leetcode:98. Validate binary search tree
已解决:initialize specified but the data directory has files in it. Aborting
memcpy内存重叠的解决
[my advanced OpenGL learning journey] about the access methods of vector and matrix classification of OpenGL shaders: xyzw/rgba/stpq and array subscripts
六,购物⻋与订单
如果我在珠海,到哪里开户比较好?另外,手机开户安全么?
Skillfully use 5 keys to improve office efficiency
IDEA import导入的类明明存在,却飘红?
[semidrive source code analysis] [x9 chip startup process] 34 - RTOS side display module SDM_ display_ Init display initialization source code analysis
【Hot100】11. 盛最多水的容器
【Hot100】15. Sum of three numbers
JS widget wave JS implementation of wave progress bar animation style
2021-07-02
Connection Flood攻击原理
Resolution: div failed to get keyboard event
Four great happenings on earth
安装Go语言开发工具
【Hot100】11. Container with the most water
[transfer] analysis of memory structure, cache and DMA architecture