二叉树建立和遍历
2013-11-28 13:38
155 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/haiwaer611024/article/details/17005047
#include #include #include typedef struct BTNode { char data; struct BTNode *lchild; struct BTNode *rchild; }BTNode; //建立二叉树 BTNode *createBT() //前序分配 { BTNode *t; char c; scanf("%c",&c); if(c == '#') t = NULL; else { t=(BTNode*) malloc(sizeof(BTNode)); t->data = c; t->lchild = createBT(); t->rchild = createBT(); }; return t; } void visit(BTNode *p) { printf("%c",p->data); } //二叉树前序遍历 void preorder(BTNode *p) { if(p != NULL) { visit(p); preorder(p->lchild); preorder(p->rchild); } } int main() { BTNode *t; printf("按先序次序输入二叉树中结点的值,输入0表示节点为空,输入范例:ac##b##\n"); /*注意BTNode中data数据类型为int,则出入时要有空格如1 2 0 0 3 0 0,因为scanf在出入int时,会进行筛选 空格和回车都丢弃,也就相当于空格和回车都是分割,即一个该数据类型的出入结束。但是输入char时并无筛选, 也就是说凡是输入一个就认为是一个char。*/ t=createBT(); printf("先序递归遍历二叉树:\n"); preorder(t); while(1); return 0; };
相关文章推荐
- 二叉树的建立与遍历(C语言简单实现)
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- 定时作业-二叉树的建立与遍历
- 二叉树建立和遍历
- 二叉树的建立与遍历
- 二叉树的建立、遍历
- 温习二叉树的建立和先序 中序 后序遍历 嵌套
- 二叉树的建立与插入和遍历
- 二叉树的建立,非递归前序、中序、后续遍历
- 12.遍历二叉树与二叉树的建立
- 二叉树的建立和遍历算法 – 数据结构和算法47
- java实现二叉树的建立,前中后序遍历,层次遍历,深度,节点个数等
- 从零开始学swift(四)-算法之二叉树的建立与遍历
- 数据结构之二叉树(遍历、建立、深度)
- 二叉树的建立(广义表)及3种遍历
- 数据结构实验之二叉树的建立与遍历
- 二叉树的建立与遍历
- 二叉树的建立(先序输入)与遍历输出模板
- 二叉树的建立与遍历
- [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)