二叉树的建立——广度优先
来源:互联网 发布:java调用docker api 编辑:程序博客网 时间:2024/05/06 00:49
好了,废话不多说,正文开始~~
前言
大家现在在网上查找“二叉树的建立”时,有很多都是基于递归建立的。虽然使用递归遍历比较简洁,但是个人认为有些不符合广大人们的直观想法,于是,我特意写了一篇广度优先的二叉树建立程序,希望能够对大家有所帮助。
算法描述
算法的流程图如下图所示:
图1 算法流程图
算法实现
我用C语言实现了该算法,并用VC++ 编译运行,先序遍历验证,结果见后。
#include<stdio.h>#include<stdlib.h>struct tree * creattree(); void Inorder(struct tree * root);struct tree{struct tree *lchildren,*rchildren,*parent;int data;};struct parent{struct tree *root;struct parent *n;};struct parent *p;int main(){struct tree *root;p=NULL;printf("请输入数据(1~n为从左至右的编号):\n");root=creattree();printf("创建成功!\n");printf("该二叉树先序遍历结果如下:");Inorder(root);//中序遍历二叉树来完成数的验证}struct tree *creattree( ){int i=0;//i为节点编号struct tree *root,*t;struct parent *now_parent,*q,*now;char data;//输入的数据now=NULL;now_parent=NULL;scanf("%c",&data);getchar();if (data!='#')//创建根节点{root=(struct tree *)malloc(sizeof(struct tree));root->data=data;root->lchildren=NULL; root->rchildren=NULL;root->parent=NULL;p=(struct parent *)malloc(sizeof(struct parent)); p->root=root;p->n=NULL;now_parent=p;now=p;i++;}else{printf("创建了空子树!\n");return NULL;}while(now!=NULL){//创建其余节点scanf("%c",&data);getchar(); t=(struct tree *)malloc(sizeof(struct tree));t->lchildren=NULL;t->rchildren=NULL;t->data=data;t->parent=now->root;i++;if (i%2==0){ now->root->lchildren=t;}else{now->root->rchildren=t;}//当节点内容不为“#”时,则该节点可能为父节点if (data!='#'){ q=(struct parent *)malloc(sizeof(struct parent));q->root=t;q->n=NULL;now_parent->n=q; now_parent=q;}if (i%2==1)now=now->n;}return root;}void Inorder(struct tree * root){if (root==NULL)return ;printf("%c ",root->data);Inorder(root->lchildren);Inorder(root->rchildren);}
图2 运行结果
#include<stdio.h>#include<stdlib.h>struct tree * creattree(); void Inorder(struct tree * root);struct tree{struct tree *lchildren,*rchildren,*parent;int data;};struct parent{struct tree *root;struct parent *n;};struct parent *p;int main(){struct tree *root;p=NULL;printf("请输入数据(1~n为从左至右的编号):\n");root=creattree();printf("创建成功!\n");printf("该二叉树先序遍历结果如下:");Inorder(root);//中序遍历二叉树来完成数的验证}struct tree *creattree( ){int i=0;//i为节点编号struct tree *root,*t;struct parent *now_parent,*q,*now;char data;//输入的数据now=NULL;now_parent=NULL;scanf("%c",&data);getchar();if (data!='#')//创建根节点{root=(struct tree *)malloc(sizeof(struct tree));root->data=data;root->lchildren=NULL; root->rchildren=NULL;root->parent=NULL;p=(struct parent *)malloc(sizeof(struct parent)); p->root=root;p->n=NULL;now_parent=p;now=p;i++;}else{printf("创建了空子树!\n");return NULL;}while(now!=NULL){//创建其余节点scanf("%c",&data);getchar(); t=(struct tree *)malloc(sizeof(struct tree));t->lchildren=NULL;t->rchildren=NULL;t->data=data;t->parent=now->root;i++;if (i%2==0){ now->root->lchildren=t;}else{now->root->rchildren=t;}//当节点内容不为“#”时,则该节点可能为父节点if (data!='#'){ q=(struct parent *)malloc(sizeof(struct parent));q->root=t;q->n=NULL;now_parent->n=q; now_parent=q;}if (i%2==1)now=now->n;}return root;}void Inorder(struct tree * root){if (root==NULL)return ;printf("%c ",root->data);Inorder(root->lchildren);Inorder(root->rchildren);}
阅读全文
1 0
- 二叉树的建立——广度优先
- 二叉树的最小深度——广度优先搜索
- 二叉树的广度优先遍历
- 二叉树的广度优先遍历
- 二叉树的广度优先搜索
- 二叉搜索树的广度优先遍历
- 二叉树的深度优先和广度优先遍历
- 二叉树的深度优先和广度优先遍历
- 二叉树的深度优先和广度优先遍历
- 二叉树的深度优先和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历、广度优先遍…
- 二叉树的深度优先遍历、广度优先遍…
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历与广度优先遍历
- 二叉树的深度优先搜索和广度优先搜索
- 二叉树的深度优先遍历和广度优先遍历
- ubuntu装cuda8.0用到的材料1
- 【SHOI2015】bzoj4591 超能粒子炮·改
- Zuul智能路由使用
- 拖拽改变层大小例如:浏览器的窗口大小改变
- 关于jsp中的绝对地址 与 相对地址
- 二叉树的建立——广度优先
- 2、jQuery插件之cxSelect城市选择(三级级联效果国际版和国内版)
- 统计一个英文文本的单词词频
- AndroidStudio快捷键和使用相关
- h5 canvas 仿知乎动态粒子背景
- 关于django form验证是否用户名已存在
- USACO section 1.2 Dual Palindromes
- 磁盘IO高原因查找
- poj1001(java 大数处理)