当前位置:   article > 正文

数据结构 二叉树常用简单应用_数据结构 二叉树 在实际 应用

数据结构 二叉树 在实际 应用
  1. #define MAX 100
  2. typedef struct {
  3. datatype SqBiTree[Max+1];
  4. int nodemax;
  5. }Bitree;
  1. typedef struct {
  2. DataType data;
  3. Struct Node * Lchild;
  4. Struct Node * Rchild;
  5. }BiTNode,*BiTree;
1.先序遍历
  1. void preOrder(BiTree root){
  2. if(root){
  3. Visit(root->data);
  4. preOrder(root->Lchild);
  5. preOrder(root->Rchild);
  6. }
  7. }

2.中序遍历

  1. void InOrder(BiTree root){
  2. if(root){
  3. preOrder(root->Lchild);
  4. Visit(root->data);
  5. preOrder(root->Rchild);
  6. }
  7. }
3.后序遍历
  1. void PostOrder(BiTree root){
  2. if(root){
  3. preOrder(root->Lchild);
  4. preOrder(root->Rchild);
  5. Visit(root->data);
  6. }
  7. }
4.先序非递归 

  1. void PreOrder(BiTree root){
  2. SeqStack *s;Bitree p;
  3. InitStack(s);p = root;
  4. while(p != NULL){
  5. Visit (p->data);
  6. Push(s,p);
  7. p = p->Lchild;
  8. }
  9. if(!IsEmpty(S)){
  10. Pop(s,&p);
  11. p= p->Rchild;
  12. }
  13. }
5.中序非递归 

  1. void InOrder(BiTree root){
  2. SeqStack *S;BiTree p;
  3. InitStack(S);p = root;
  4. while(p!=NULL || !IsEmpty(S)){
  5. while(p!=null){
  6. Push(S,p);
  7. p=p->Lchild;
  8. }
  9. if(!IsEmpty(S))
  10. {
  11. Pop(S,&p);
  12. Visit(p->data);
  13. p=p->Rchild;
  14. }
  15. }
  16. }
6.后序非递归

  1. void PostOrder(BiTree root){
  2. SeqStack *S;BiTree p,q;
  3. InitStack(S);p = root;q=null;
  4. while(p!=null || IsEmpty(S)){
  5. while(p!= null){
  6. Push(S,p);
  7. p = p->Lchild;
  8. }
  9. if(!IsEmpty(S)){
  10. Top(S,&p);
  11. p=p->Rchild;
  12. }
  13. }
7.二叉树层次遍历

  1. void LevelOrder(BiTree root){
  2. SeqQueue *Q;BiTree p;
  3. InitQueue(Q);EnterQueue(Q,root);
  4. while(!IsEmpty(Q)){
  5. DeleteQueue(Q,&p);
  6. Visit(p->data);
  7. if(p->Lchild!=null){
  8. EnterQueue(Q,p->Lchild);
  9. }
  10. if(p->Rchild!=null){
  11. EnterQueue(Q,p->Rchild);
  12. }
  13. }
  14. }
8.先序遍历统计结点 

  1. void PreOrder(BiTree root){
  2. if(root){
  3. Count++;
  4. PreOrder(root->Lchild);
  5. PreOrder(root->Rchild);
  6. }
  7. }
9.输出叶子结点

  1. void InOrder(BiTree root){
  2. if(root){
  3. InPreOrder(root->Lchild);
  4. if(root->Lchild == NULL && root ->Rchild==NULL){
  5. printf(root->data);
  6. }
  7. InOrder(root->Echild);
  8. }
  9. }
10.统计叶子结点数目

  1. int leaf(BiTree root){
  2. int nl,nr;
  3. if(root == NULL) return 0;
  4. if((root->Lchild==NULL) && (root->Rchild==NULL) ) return 1;
  5. nl = leaf(root->Lchild);
  6. nr = leaf(root->Rchild);
  7. return (nl+nr);
  8. }
11.二叉树高度

  1. void TreeDepth(BiTree root,int h){
  2. if(root){
  3. if(h>depth) depth = h;
  4. TreeDepth(root->Lchild,h+1);
  5. TreeDepth(root->Rchild,h+1);
  6. }
  7. }
12.按树状打印二叉树

  1. void PrintTree(BiTree root){
  2. if(root = Null) return ;
  3. PrintTree(root->Rchild,h+1);
  4. int i=0;
  5. for(i=0;i<h;i++){
  6. Printf(" ");
  7. }
  8. printf("%c\n",root->data);
  9. PrintTree(root->Lchild,h+1);
  10. }
























声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/718410
推荐阅读
相关标签
  

闽ICP备14008679号