Teaching Purpose: grasping the chain-type storage structure of binary tree
Teaching emphases: A chain-type storage implementation method of two-fork tree
Teaching Difficulties:
Teaching Content:
The following two fork trees are generated and three traversal results are obtained:
A chain-type storage structure representation of a one or two-fork tree
typedef struct bitnode{
Telemtype data;
struct Bitnode *lchild,*rchild;
}bitnode,*bitree;
Implementation of chain storage algorithm for two or two-fork tree
Createbitree (&t,definition);
Insertchild (T,P,LR,C);
Recursive traversal of three or two-fork tree
Preordertraverse (T,visit ());
Inordertraverse (T,visit ());
Postordertraverse (T,visit ());
Sample source Program
#include <alloc.h> #define ERROR 0;
#define OK 1;
typedef int ELEMTYPE;
typedef struct BINARYTREE {elemtype data;
struct BinaryTree *l;
struct BinaryTree *r;
}*bitree,binode;
Binode * NEW () {return ((Binode *) malloc (sizeof (Binode)));} Createsubtree (bitree *t,elemtype *all,int i) {if (all[i]==0) | |
I>16) {*t=null;
return OK;
} *t=new ();
if (*t==null) return ERROR;
(*t)->data=all[i];
Createsubtree (& (*t)->l), all,2*i);
Createsubtree (& (*t)->r), all,2*i+1);
} createbitree (Bitree *t) {elemtype all[16]={0,1,2,3,0,0,4,5,0,0,0,0,6,0,0,0,};
Createsubtree (t,all,1);
} printelem (Elemtype d) {printf ("%d\n", d);} Preordertraverse (Bitree t,int (*visit) (Elemtype D)) {if (T) {if (Visit (T->data)) if (preordertraverse
, Visit)) if (Preordertraverse (t->r,visit)) return OK;
return ERROR;
else return OK;
Main () {Bitree root;
Createbitree (&root); Preordertraverse (Root,printelem);
}