数据结构 二叉树常用简单应用
2018-01-12 15:47
471 查看
#define MAX 100 typedef struct { datatype SqBiTree[Max+1]; int nodemax; }Bitree;
typedef struct { DataType data; Struct Node * Lchild; Struct Node * Rchild; }BiTNode,*BiTree;
1.先序遍历
void preOrder(BiTree root){ if(root){ Visit(root->data); preOrder(root->Lchild); preOrder(root->Rchild); } }
2.中序遍历
void InOrder(BiTree root){ if(root){ preOrder(root->Lchild); Visit(root->data); preOrder(root->Rchild); } }
3.后序遍历
void PostOrder(BiTree root){ if(root){ preOrder(root->Lchild); preOrder(root->Rchild); Visit(root->data); } }
4.先序非递归
void PreOrder(BiTree root){ SeqStack *s;Bitree p; InitStack(s);p = root; while(p != NULL){ Visit (p->data); Push(s,p); p = p->Lchild; } if(!IsEmpty(S)){ Pop(s,&p); p= p->Rchild; } }
5.中序非递归
void InOrder(BiTree root){ SeqStack *S;BiTree p; InitStack(S);p = root; while(p!=NULL || !IsEmpty(S)){ while(p!=null){ Push(S,p); p=p->Lchild; } if(!IsEmpty(S)) { Pop(S,&p); Visit(p->data); p=p->Rchild; } } }
6.后序非递归
void PostOrder(BiTree root){ SeqStack *S;BiTree p,q; InitStack(S);p = root;q=null; while(p!=null || IsEmpty(S)){ while(p!= null){ Push(S,p); p = p->Lchild; } if(!IsEmpty(S)){ Top(S,&p); p=p->Rchild; } }
7.二叉树层次遍历
void LevelOrder(BiTree root){ SeqQueue *Q;BiTree p; InitQueue(Q);EnterQueue(Q,root); while(!IsEmpty(Q)){ DeleteQueue(Q,&p); Visit(p->data); if(p->Lchild!=null){ EnterQueue(Q,p->Lchild); } if(p->Rchild!=null){ EnterQueue(Q,p->Rchild); } } }
8.先序遍历统计结点
void PreOrder(BiTree root){ if(root){ Count++; PreOrder(root->Lchild); PreOrder(root->Rchild); } }
9.输出叶子结点
void InOrder(BiTree root){ if(root){ InPreOrder(root->Lchild); if(root->Lchild == NULL && root ->Rchild==NULL){ printf(root->data); } InOrder(root->Echild); } }
10.统计叶子结点数目
int leaf(BiTree root){ int nl,nr; if(root == NULL) return 0; if((root->Lchild==NULL) && (root->Rchild==NULL) ) return 1; nl = leaf(root->Lchild); nr = leaf(root->Rchild); return (nl+nr); }
11.二叉树高度
void TreeDepth(BiTree root,int h){ if(root){ if(h>depth) depth = h; TreeDepth(root->Lchild,h+1); TreeDepth(root->Rchild,h+1); } }
12.按树状打印二叉树
void PrintTree(BiTree root){ if(root = Null) return ; PrintTree(root->Rchild,h+1); int i=0; for(i=0;i<h;i++){ Printf(" "); } printf("%c\n",root->data); PrintTree(root->Lchild,h+1); }
相关文章推荐
- 【数据结构】二叉树的简单操作及简单应用
- C语言数据结构二叉树简单应用
- 数据结构-------树与二叉树的几个简单应用
- 【郝斌数据结构自学笔记】70-74_已知两种遍历序列求原始二叉树概述_已知先序和中序求后序_已知中序和后序求先序_树的应用简单介绍
- 数据结构 二叉树的应用算法
- 数据结构 二叉树应用:赫夫曼编码一
- Android 基本常用控件ListView的简单应用
- 【数据结构】二叉树的一个应用,哈夫曼编码的生成
- 上机作业之树:递归算法在二叉树中的简单应用
- (数据结构)图的应用,一个简单的学校地图.包含的内容:图的最短路径算法 和 图的深度优先遍历算法
- 【数据结构上机练习】6. 二叉树的简单操作(1)
- 数据结构 二叉树应用:赫夫曼编码二
- 2017年浙江工业大学大学生程序设计迎新赛预赛 D 简单的数据结构【纯 stl -> List 的应用】
- 10---------二叉树的算法的简单应用
- 常用的数据结构(二叉树)
- day02_常用表单%CSS简单应用
- 常用数据结构-二叉树的链式存储、建立和遍历
- 教务处管理系统(数据结构的简单应用)
- 二叉树简单应用
- 3ff0 C语言实现一种简单的应用服务器内部数据结构的思路(二)