二叉树建立和遍历
来源:互联网 发布:校园网络设计方案ppt 编辑:程序博客网 时间:2024/06/05 07:18
二叉树创建遍历规则:
1.先序:根-左-右
2.中序:左-根-右
3.后序:左-右-根
二叉树定义和辅助函数如下:
struct node { int data; struct node* left; struct node* right; }; void visit(int data) { printf("%d ", data); } int indata(){ int data; scanf("%d",&data); return data;}
先序创建二叉树:
struct node* CreateBiTree()//先序建立一个二叉树 { char x; //x为根节点 struct node* t; x=indata(); if (x==' ') /* 判断当前子树是否创建完成*/ return NULL; else { t=(struct node*)malloc(sizeof(struct node)); t->data=x; t->left=CreateBiTree(); t->right=CreateBiTree(); } return t; }先序遍历二叉树:
void preOrder(struct node* root) { if (root == NULL) return; visit(root->data); preOrder(root->left); preOrder(root->right); }
中序创建二叉树:
struct node* CreateBiTree()//先序建立一个二叉树 { char x; //x为根节点 struct node* t; x=indata(); if (x==' ') /* 判断当前子树是否创建完成*/ return NULL; else { t=(struct node*)malloc(sizeof(struct node)); t->left=CreateBiTree(); t->data=x; t->right=CreateBiTree(); } return t; }中序遍历二叉树:
void inOrder(struct node* root) { if (root == NULL) return; inOrder(root->left); visit(root->data); inOrder(root->right); }
后序创建二叉树
struct node* CreateBiTree()//先序建立一个二叉树 { char x; //x为根节点 struct node* t; x=indata(); if (x==' ') /* 判断当前子树是否创建完成*/ return NULL; else { t=(struct node*)malloc(sizeof(struct node)); t->left=CreateBiTree(); t->right=CreateBiTree(); t->data=x; } return t; }后序遍历二叉树
void inOrder(struct node* root) { if (root == NULL) return; inOrder(root->left); inOrder(root->right); visit(root->data); }
转载请注明作者:小刘
3 0
- 二叉树建立和遍历
- 二叉树建立和遍历
- 二叉树建立和遍历
- 二叉树建立和遍历
- 二叉树建立和遍历
- 二叉树建立和遍历
- 二叉树的建立和遍历
- 二叉树 普通的 建立和遍历
- 二叉树的建立和遍历
- 二叉树的建立和遍历
- Tree 二叉树的建立 和遍历
- 二叉树的建立和遍历
- [转]二叉树的建立和遍历
- 二叉树的建立和遍历
- 二叉树的建立和遍历
- 递归建立和遍历二叉树等
- 二叉树的建立和遍历
- 二叉树的建立和遍历
- office文档在线预览 (doc、docx、ppt、pptx、xls、xlsx)
- symbol dyld_stub_binding_helper not found linker command failed with exit code 1 (use -v to see invo
- Oracle:Authid Current_User的使用
- Uninstall Samsung Kies 3 with WindowsUninstaller.Org Removal Tips
- poj 1125 Stockbroker Grapevine(多源最短路)
- 二叉树建立和遍历
- 多线程编程 - NSOperationQueue
- 关于Hadoop2.2.0在虚拟机centOS6.5下的安装配置
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- Array与ArrayList的关系与区别
- HDU 1849 Rabbit and Grass【尼姆博弈】
- XShell VIM 右击粘贴不了
- HttpURlconnection 学习笔记
- 大型网站技术架构 笔记