zoukankan      html  css  js  c++  java
  • 数据结结构学习 2叉树

    ------ 二叉树的顺序存储表示 ------

    #define MAX_TREE_SIZE 100;

    typedef TElemType SqBiTree[MAX_TREE_SIZE];

    SqBiTree bt;

    ------ 二叉树的 二叉链表存储表示 ------

    typedef struct BiTNode {

      TElemType data;

      struct BiTNode * lchild, *rchild;

    }BiTNode,BiTree;

    遍历2叉树  L,D,R (左子树,根,右子树)  先根遍历,中根遍历,后根遍历。

    status PreOderTraverse(BiTree t, status (* visit)(TElemType e)) {

      if (t) {

        visit (t->data)

        if (PreOderTraverse(t->lchild,visit) )

          if (PreOderTraverse(t->rchild,visit)) return ok;

        return ERROR;

      }else return ok;

    }

    中序遍历二叉树非递归方法

    status InOrderTraverse(BiTree t, status (*visit)(TElemType e) ) {

      InitStack(s) ;  push( s,T); // 根进栈

      while( !StatckEmpty(s)) {

        while ( GetTop(s,p) && p )  push(s,p->lchild); //向左走到尽头

        pop(s,p); //空指针退栈

        if(!StackEmpty(s)) {

          pos(s,p);  if(!visit(p->data)) return ERROR;

          push(s,p->rchild);

        } // if

      } //while

      return ok;

    }

    status InOrderTraverse(BiTree t, status (*visit)(TElemType e) ) {

      InitStack(s); p=T;

      while(p || !StackEmpty(s,p) ) {

        if(p) push(s,p->lchild); //根指针近栈,遍历左子树

        else {

          pop(s,p);  if(!visit(p->data)) return ERROR;

          p = p->rchild;

        } //else

      } //while

      return OK;

    }

     status CreateBiTree( BiTree & t) {

      scanf(&ch);

      if ( ch == '') t= NULL;

      else {

        if(!(t= (BiTNode*) malloc (sizeof(BiTNode) ) ) ) exit;

        t->data = ch;

        CreateBiTree(t->lchild);

        CreateBiTree(t->rchild);

      }//else

    }

    ------ 二叉树的 二叉线索存储表示 ------

    typedef enum PointerTag { Link , Thread } ;

    typedef struct BiThrNode {

      TElemType data;

      struct BiThrNode * lchild ,rchild;

      PointerTag LTag,RTag;

    } BiThrNode,*BiThrTree;

    status InOrderTraverse_Thr(BiThrTree t, status (*visit)(TElemType e) ) {

      p = T->lchild; //p 指向根节点

      while(p!=t) { //空树或遍历结束时,P==t

        while(p->LTag = Link) p = p->lchild;  //

        if (!viist (p->data) return ERROR;

        while( (p->RTag == Thread) && p->rchild!=t) {

          p = p->rchild; visit(p->data);

        }//while

        p = p->rchild;

      }//while

      return OK;

    }

    中序遍历二叉树并线索化链表

    status InOrderThreading(BiThrTree & thr,BiThrTree t) {

      if (! ( thrt= (BiThrTree) malloc ( sizeof(BiThrTree) ) ) ) exit;

      thrt->LTag = link; thrt->RTag = thread;  //建立头结点

      thrt->rchild = thrt;  //右指针回指

      if (!t) thrt->lchild = thrt; //若二叉树为空,则左指针回指

      else {

        thrt->lchild = t ;  pre= thrt;

        InThreading(t); //中序遍历进行中序线索化

        pre->rchild = thrt;  pre->RTag = Thread; //最后一个节点线索化

        thrt->rchild = pre;

      } //else

      return ok;

    }

    void InThreading(BiThrTree p) {

      if (p) {

        InThreading(p->lchild);  //左子树线索化

        if(!p->lchild) { p->LTag = Thread;  p->lchild = pre; } //前驱线索

        if(!pre->rchild) { pre->RTag = Thread; pre->rchild = p; } // 后续线索

        pre = p;

        InThreading(p->rchild);

      } //if

    }

  • 相关阅读:
    Vue 2.x windows环境下安装
    VSCODE官网下载缓慢或下载失败 解决办法
    angular cli 降级
    Win10 VS2019 设置 以管理员身份运行
    XSHELL 连接 阿里云ECS实例
    Chrome浏览器跨域设置
    DBeaver 执行 mysql 多条语句报错
    DBeaver 连接MySql 8.0 报错 Public Key Retrieval is not allowed
    DBeaver 连接MySql 8.0报错 Unable to load authentication plugin 'caching_sha2_password'
    Linux系统分区
  • 原文地址:https://www.cnblogs.com/zhoug2020/p/2785590.html
Copyright © 2011-2022 走看看