這兩天清明放假,學習效率很低,二叉樹的創建中指針的使用讓我頗爲不爽,我對指針的理解仍是膚淺。
php
待解決的問題:node
http://bbs.51cto.com/viewthread.php?tid=1103326&extra=page%3D1&frombbs=1linux
還望高人指點迷津。編程
二叉樹的存儲使用二叉鏈表結構:ide
typedef struct node{ char data; struct node *lchild, *rchild; }btree, *bitree;
(我在學習linux C編程,因此編程風格傾向於linxu c編程風格。)post
下面是所有代碼:學習
#include<malloc.h> #include<stdio.h> typedef struct node{ char data; struct node *lchild, *rchild; }btree, *bitree; //先序遍歷二叉樹 void preorder(bitree root) { if(root) { printf("%3c", root->data); preorder(root->lchild); preorder(root->rchild); } } //中序遍歷二叉樹 int inorder(bitree root) { if(root) { inorder(root->lchild); printf("%3c", root->data); inorder(root->rchild); } } //後序遍歷二叉樹 void postorder(bitree root) { if(root) { postorder(root->lchild); postorder(root->rchild); printf("%3c", root->data); } } //構造二叉樹 void create_tree(bitree *root) { char c; c = getchar(); if(c == '.') *root = NULL; else{ *root = (bitree)malloc(sizeof(btree)); (*root)->data = c; create_tree(&(*root)->lchild); create_tree(&(*root)->rchild); } } int main() { bitree tree; printf("enter you tree \n"); create_tree(&tree); printf("create done\n"); printf(" preorder start: "); preorder(tree); printf(" preorder end\n"); printf(" inorder start: "); inorder(tree); printf(" inorder end\n"); printf(" postorder start: "); postorder(tree); printf(" postorder end\n"); return 0; }