数据结构——树
2016-02-01 14:00
519 查看
一:二叉树
1.二叉树的建立和三种遍历#include<stdio.h> #include<stdlib.h> typedef struct node { char data; struct node *left; struct node *right; }bitnode, *tree; void create_tree(tree *t)//先序递归建立树 { //因为形参不改变实参,所以传递指向指针的指针 char ch; scanf("%c", &ch); while (getchar() != '\n') continue; if (ch == ' ') //空格代表为空 *t = NULL; else { *t = (bitnode*)malloc(sizeof(bitnode)); (*t)->data = ch; //有优先度的问题,->的优先度貌似大于*,所以加个括号 printf("Enter the left child\n"); create_tree(&(*t)->left); //要传递指向指针的指针 printf("Enter the right child\n"); create_tree(&(*t)->right); } } void preorder(tree t)//先序遍历 { if (t != NULL) { printf("%c ", t->data); preorder(t->left); preorder(t->right); } } void inorder(tree t)//中序遍历 { if (t != NULL) { inorder(t->left); printf("%c ", t->data); inorder(t->right); } } void postorder(tree t)//后序遍历 { if (t != NULL) { postorder(t->left); postorder(t->right); printf("%c ", t->data); } } int main() { tree t; create_tree(&t); preorder(t); printf("\n"); inorder(t); printf("\n"); postorder(t); return 0; }
相关文章推荐
- 【数据结构学习笔记】——根据中缀表达式构建二叉树并输出
- [数据结构与算法分析] 单链表基本操作的实现
- 数据结构《17》---- 自己主动补齐之《二》----Ternary Search Tree
- 数据结构图文解析之:数组、单链表、双链表介绍及C++模板实现
- 基本概念和基础知识
- linux0.11相关进程数据结构 GDT,LDT,GDTR,LDTR
- 数据结构之堆排序
- 数据结构——采用分离链接解决冲突问题的散列表
- 数据结构——平衡二叉搜索树(AvlTree)的实现
- 数据结构——二叉搜索树(BinarySearchTree)的实现
- 数据结构——LinkedQueue的实现
- 数据结构——ArrayStack和LinkedStack的实现
- 数据结构——SimpleArrayList和SimpleLinkedList的实现
- 数据结构——SimpleList的实现
- 数据结构与算法概念
- 数据结构与算法——先序输出叶结点(C语言)
- 数据结构与算法——二分查找(C语言)
- 数据结构(寒假小结)--3.3串
- HDU1004 Let the Balloon Rise 数据结构
- Lua数据结构 — 闭包(四)