二叉链表的类型定义——教材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