//二叉树的前序、中序递归遍历与先序生成 #include <bits/stdc++.h> using namespace std; typedef struct TNode *PtrToTNode; typedef PtrToTNode BinTree; typedef char ElementType; struct TNode{ ElementType Data; BinTree Left; BinTree Right; }; //二叉树的先序递归遍历 void PreorderTraversal(BinTree BT){ if(BT){ printf("%c ",BT->Data); PreorderTraversal(BT->Left); PreorderTraversal(BT->Right); } } //二叉树的中序递归遍历 void InOrderTraversal(BinTree BT){ if(BT){ InOrderTraversal(BT->Left); printf("%c ",BT->Data); InOrderTraversal(BT->Right); } } BinTree CreateBinTreeNode(ElementType X){ BinTree node = (BinTree)malloc(sizeof(struct TNode)); node->Data = X; node->Left = NULL; node->Right = NULL; return node; } BinTree PreCreateBinTree(){ ElementType X; scanf("%c",&X); if(X==' '){ return NULL; } else{ BinTree BT = CreateBinTreeNode(X); BT->Left = PreCreateBinTree(); BT->Right = PreCreateBinTree(); return BT; } } //释放二叉树所有结点内存 void FreeBinTree(BinTree BT){ if(BT){ BinTree BTL=BT->Left,BTR=BT->Right; free(BT); BT = NULL; FreeBinTree(BTL); FreeBinTree(BTR); } } int main(){ BinTree BT = PreCreateBinTree(); PreorderTraversal(BT); cout << endl; InOrderTraversal(BT); cout << endl; InOrderTraversal(BT); FreeBinTree(BT); return 0; }
0.0分
2 人评分
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:530 |
C语言程序设计教程(第三版)课后习题5.7 (C++代码)浏览:879 |
简单的a+b (C语言代码)浏览:878 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:573 |
A+B for Input-Output Practice (III) (C语言代码)浏览:594 |
1051(奇了怪了)浏览:747 |
C语言训练-亲密数 (C语言描述,反正怎么都能对)浏览:2256 |
单词个数统计 (C语言代码)浏览:1046 |
C二级辅导-求偶数和 (C语言指针动态内存法)浏览:661 |
【密码】 (C语言代码)浏览:574 |