#include<stdio.h> #define TREEMAX 100 typedef struct BT { char data; BT *lchild; BT *rchild; }BT; BT *CreateTree(); void Preorder(BT *T); void Postorder(BT *T); void Inorder(BT *T); void Leafnum(BT *T); void Nodenum(BT *T); int TreeDepth(BT *T); int count=0; void main() { BT *T=NULL; char ch1,ch2,a; ch1='y'; while(ch1=='y'||ch1=='y') { printf("\n"); printf("\n\t\t 二叉樹子系統"); printf("\n\t\t*****************************************"); printf("\n\t\t 1---------建二叉樹 "); printf("\n\t\t 2---------先序遍歷 "); printf("\n\t\t 3---------中序遍歷 "); printf("\n\t\t 4---------后序遍歷 "); printf("\n\t\t 5---------求葉子數 "); printf("\n\t\t 6---------求結點數 "); printf("\n\t\t 7---------求樹深度 "); printf("\n\t\t 0---------返 回 "); printf("\n\t\t*****************************************"); printf("\n\t\t 請選擇菜單號 (0--7)"); scanf("%c",&ch2); getchar(); printf("\n"); switch(ch2) { case'1': printf("\n\t\t請按先序序列輸入二叉樹的結點:\n"); printf("\n\t\t說明:輸入結點(‘0’代表后繼結點為空)后按回車。\n"); printf("\n\t\t請輸入根結點:"); T=CreateTree(); printf("\n\t\t二叉樹成功建立!\n");break; case'2': printf("\n\t\t該二叉樹的先序遍歷序列為:"); Preorder(T);break; case'3': printf("\n\t\t該二叉樹的中序遍歷序列為:"); Inorder(T);break; case'4': printf("\n\t\t該二叉樹的后序遍歷序列為:"); Postorder(T);break; case'5': count=0;Leafnum(T); printf("\n\t\t該二叉樹有%d個葉子。\n",count);break; case'6': count=0;Nodenum(T); printf("\n\t\t該二叉樹總共有%d個結點。\n",count);break; case'7': printf("\n\t\t該樹的深度為:%d",TreeDepth(T)); break; case'0': ch1='n';break; default: printf("\n\t\t***請注意:輸入有誤!***"); } if(ch2!='0') { printf("\n\n\t\t按【Enter】鍵繼續,按任意鍵返回主菜單!\n"); a=getchar(); if(a!='\xA') { getchar(); ch1='n'; } } } } BT *CreateTree() { BT *t; char x; scanf("%c",&x); getchar(); if(x=='0') t=NULL; else { t=new BT; t->data=x; printf("\n\t\t請輸入%c結點的左子結點:",t->data); t->lchild=CreateTree(); printf("\n\t\t請輸入%c結點的右子結點:",t->data); t->rchild=CreateTree(); } return t; } void Preorder(BT *T) { if(T) { printf("%3c",T->data); Preorder(T->lchild); Preorder(T->rchild); } } void Inorder(BT *T) { if(T) { Inorder(T->lchild); printf("%3c",T->data); Inorder(T->rchild); } } void Postorder(BT *T) { if(T) { Postorder(T->lchild); Postorder(T->rchild); printf("%3c",T->data); } } void Leafnum(BT *T) { if(T) { if(T->lchild==NULL&&T->rchild==NULL) count++; Leafnum(T->lchild); Leafnum(T->rchild); } } void Nodenum(BT *T) { if(T) { count++; Nodenum(T->lchild); Nodenum(T->rchild); } } int TreeDepth(BT *T) { int ldep,rdep; if(T==NULL) return 0; else { ldep=TreeDepth(T->lchild); rdep=TreeDepth(T->rchild); if(ldep>rdep) return ldep+1; else return rdep+1; } }
上傳時間: 2020-06-11
上傳用戶:ccccy
網絡監控工具服務器端
標簽:
上傳時間: 2015-01-19
上傳用戶:xcy122677
採用ROM監控器的調試技巧分析
上傳時間: 2015-02-23
上傳用戶:wfl_yy
該程序是基于PC104的工控板的多參數監護儀的源程序.監護內容包含:心電\血氧\呼吸\無創血壓等等.
上傳時間: 2013-12-18
上傳用戶:2404
利用msp430作網路資料傳輸,可監控溫度..,的源碼
上傳時間: 2013-12-10
上傳用戶:冇尾飛鉈
本章講解微軟Visual Basic Wi n s o c k控件的問題。這是一種非常新的控件,用于將Wi n s o c k 接口簡化成易于使用的Visual Basic內部接口
上傳時間: 2013-12-19
上傳用戶:Thuan
一個簡易的流量監控程式,可進行網路封包流量監控
上傳時間: 2013-12-18
上傳用戶:wsf950131
Delphi/BCB 各種版本都支持的Excel 讀寫控件.一成功應用在N個項目中 .
上傳時間: 2016-05-06
上傳用戶:busterman
有限期作業安排問題”描述如下:有n個任務J1,J2,...,Jn,每個任務Ji都有一個完成期限di,若任務Ji在它的期限di內完成,則可以獲利Ci(1[i[n) 問如何安排使得總的收益最大(假設完成每一個任務所需時間均為一個單位時間).這個問題適合用貪心算法來解決,貪心算法的出發點是每一次都選擇利潤大的任務來完成以期得到最多的收益 但是對于本問題由于每一個任務都有一個完成的期限,因此在任務安排過程中除了考慮利潤Ci外,還要考慮期限di.
上傳時間: 2016-06-27
上傳用戶:s363994250
基於VA7205的充電電路設計. 具電壓,電流和溫度監控功能.
上傳時間: 2016-12-05
上傳用戶:litianchu