首页 > 其他 > 详细

第四章 树和二叉树

时间:2021-06-05 11:13:00      阅读:20      评论:0      收藏:0      [点我收藏+]

二叉链表的类型定义——教材101页

typedef struct btnode

{

  DataType data;

  struct btnode *lchild,*rchild;//指向左右孩子的指针

} *BinTree;

三叉链表的类型定义——教材102页

typedef struct ttnode

{

  DataType data;

  struct ttnode *lchild,*parent,*rchild;//在二叉链表的基础上多了一个 指向双亲的指针

} *TBinTree;

TBinTree root;

二叉链表的三种遍历的递归算法

1 先序遍历-根,左,右

void preorder(BinTree bt)

{

  if (bt != NULL)

  {

    visit (bt);//根

    preorder (bt->lchild);//左

    preorder (bt->rchild);//右

  }

}

2 中序遍历-左,根,右

void preorder(BinTree bt)

{

  if (bt != NULL)

  {    

    inorder (bt->lchild);//左

    visit (bt);

    inorder (bt->rchild);//右

  }

}

3 后序遍历-左,右,根

void postorder(BinTree bt)

{

  if (bt != NULL)

  {    

    postorder (bt->lchild);//左

    postorder (bt->rchild);//右

    visit (bt);//根

  }

}

利用二叉树遍历的递归算法,求二叉树的高度——教材106页

int Height(BinTree bt)

{

  int lh,rt;

  if (bt == NULL)

    return 0;

  else

    {

      lh=Height(bt->lchild);

      rh=Height(bt->rchild);

      return 1+(lh>rh ? lh : rh);

    }

}

树的储存结构

1 孩子链表——教材112页

const int MAXND=20;//树中结点的最大个数

typedef struct bnode//表结点类型

{

  int child;

  struct bnode *next;

} node,*childlink;

typedef struct

{

  DataType data;

  childlink hp;

} headnode;

headnode link[MAXND]

2 带双亲的孩子链表

const int MAXND=20;//树中结点的最大个数

typedef struct bnode//表结点类型

{

  int child;

  struct bnode *next;

} node,*childlink;

typedef struct

{

  DataType data;

  int parent;

  childlink hp;

} headnode;

headnode link[MAXND]

3 孩子兄弟链表表示法

typedef struct tnode

{

  DataType data;

  struct tnode *son,*brother;

} *Tree;

4 双亲表示法

const int size=10;

typedef struct

{

  DataType data;

  int parent;

} Node;

Node slist [size];//用数组实现双亲表

第四章 树和二叉树

原文:https://www.cnblogs.com/cnxm/p/14852023.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!