数据结构实验十一——树的基本操作
来源:互联网 发布:wifi网络密码破解 编辑:程序博客网 时间:2024/06/05 02:57
#include <stdio.h>#include <stdlib.h>typedef struct BiNode{ char data; struct BiNode *lchild,*rchild;}BiNode,*BiTree;void CreateTree(BiTree *T)//输入有技巧!!{ char ch; scanf("%c",&ch); if(ch==' ') *T=NULL; else { *T=(BiTree)malloc(sizeof(BiNode)); (*T)->data=ch; CreateTree(&(*T)->lchild); CreateTree(&(*T)->rchild); }}void PreDisplay(BiTree T){ if(T) { printf("%c",T->data); PreDisplay(T->lchild); PreDisplay(T->rchild); }}void InDisplay(BiTree T){ if(T) { InDisplay(T->lchild); printf("%c",T->data); InDisplay(T->rchild); }}void PostDisplay(BiTree T){ if(T) { PostDisplay(T->lchild); PostDisplay(T->rchild); printf("%c",T->data); }}void Display(BiTree T){ if(T) { printf("%c",T->data); if(T->lchild!=NULL||T->rchild!=NULL) { printf("("); if(T->lchild!=NULL) Display(T->lchild); printf(","); if(T->rchild!=NULL) Display(T->rchild); printf(")"); } }}int NodeNum(BiTree T){ if(T==NULL) return 0; else if(T->lchild==NULL&&T->rchild==NULL) return 1; else return NodeNum(T->lchild)+NodeNum(T->rchild)+1;}int LeafNodeNum(BiTree T){ if(T==NULL) return 0; else if(T->lchild==NULL&&T->rchild==NULL) return 1; else return LeafNodeNum(T->lchild)+LeafNodeNum(T->rchild);}int DeepthTree(BiTree T){ if(T==NULL) return 0; else if(DeepthTree(T->lchild)>DeepthTree(T->rchild)) return 1+DeepthTree(T->lchild); else return 1+DeepthTree(T->rchild);}BiTree FindNode(BiTree T,char e){ BiTree p; if(T==NULL){ printf("tree empty!\n"); return NULL; } else if(T->data==e) return T; else { p=FindNode(T->lchild,e); if(p!=NULL) return p; else return FindNode(T->rchild,e); }}void Display2(BiTree T){ BiTree S[20],p; int level[100][2],top=-1,n,i; char e; if(T) { top++; S[top]=T; level[top][0]=1; level[top][1]=2; while(top>-1) { p=S[top]; n=level[top][0]; switch(level[top][1]) { case 0: e='L'; break; case 1: e='R'; break; case 2: e='B'; break; } for(i=0;i<n;i++) printf(" "); printf("%c(%c)",p->data,e); for(i=n+1;i<40;i+=2) printf("--"); printf("\n"); top--; if(p->rchild!=NULL) { top++; S[top]=p->rchild; level[top][1]=1; level[top][0]+=4; } if(p->lchild!=NULL) { top++; S[top]=p->lchild; level[top][1]=0; level[top][0]+=4; } } }}int main(){ BiTree T; printf("请按先序遍历依次输入二叉树:\n"); CreateTree(&T); printf("二叉树的先序遍历是:\n"); PreDisplay(T); printf("\n"); printf("二叉树的中序遍历是:\n"); InDisplay(T); printf("\n"); printf("二叉树的后序遍历是:\n"); PostDisplay(T); printf("\n"); printf("二叉树的广义表输出:\n"); Display(T); printf("\n"); printf("二叉树的凹形输出:\n"); Display2(T); printf("\n"); printf("二叉树T是%s\n",(T==NULL)?"empty tree":"no empty tree"); printf("二叉树T的节点个数=%d\n",NodeNum(T)); printf("二叉树T的叶子节点个数=%d\n",LeafNodeNum(T)); printf("二叉树T的深度=%d\n",DeepthTree(T)); return 0;}大量参考了资料书的代码!对非线性的数据结构还是不熟悉!
0 0
- 数据结构实验十一——树的基本操作
- 数据结构实验2——二叉树的基本操作
- 数据结构实验——基于数组的栈基本操作
- 数据结构学习(十一)——二叉树的操作
- 南邮数据结构实验6.1二叉树的基本操作
- 数据结构实验报告单链表的基本操作
- 数据结构—基本树的操作小结
- 数据结构实验——图的基本操作(未完待续)
- 数据结构实验——二叉树的常见操作
- Java数据结构与算法分析《十一》二叉树的基本操作
- 数据结构(十一)循环队列的基本操作----6个基本操作
- 数据结构实验一--单链表的基本操作的算法
- 数据结构——图的基本操作
- 数据结构——单链表的基本操作
- 数据结构——栈的基本操作
- 数据结构——单链表的基本操作
- 数据结构——单链表的基本操作
- 数据结构——栈的基本操作
- fstat、stat和lstat 区别(转)
- lower_bound()和upper_bound()(有删改)
- 如何撰写网络营销策划方案?
- 反多个的风格的风格的风格
- OS系统下搭建jenkins环境
- 数据结构实验十一——树的基本操作
- BestCoder #26 1002 Card 期望 解题报告
- wing ftp server网页无法访问
- C++:复合类型————引用(左值引用)
- 9.2、Libgdx的输入处理之鼠标、触摸和键盘
- 让流量箭步如飞 只需做好十点
- css颜色匹配代码
- Java_日期操作_日期之差
- OAuth 2.0 教程 http://tutorials.jenkov.com/oauth2/index.html