Binary tree Two-fork list storage representation//Xin Yang # include <stdio.h> #include <stdlib.h> #define MAX (A, b) a > B? A:B//Custom Max () function typedef char telemtype;//definition knot two fork tree structure typedef struct BTREE{TELEMTYPE data;struct BTree *lchild;struct BTree *rchild;} bintree;//two fork tree created bintree* createtree (Bintree *t) {char temp;scanf ("%c", &temp), if (temp = = ' 0 ') return 0; T = (Bintree *) malloc (sizeof (bintree)); T->data = temp; T->lchild = Createtree (t->lchild);//recursive creation of left subtree T->rchild = Createtree (t->rchild);//recursive creation of right subtree return T;} Calculate the number of leaf nodes int sumleft (Bintree *t) {int sum = 0, Leftnum, rightnum;if (T) {if (! T->lchild) && (! T->rchild)) {sum++;} Leftnum = Sumleft (t->lchild); sum + = Leftnum;rightnum = Sumleft (t->lchild); sum + = Rightnum;} return sum;} First Order traversal binary tree void Preordertraverse (Bintree *t) {if (T) {printf ("%c", t->data); Preordertraverse (T->lchild); Preordertraverse (T->rchild);}} The middle sequence traverses the binary tree void Inordertraverse (Bintree *t) {if (T) {inordertraverse (t->lchild);p rintf ("%c", t->data); InordeRtraverse (T->rchild);}} Post-sequential traversal of the binary tree void Postordertraverse (Bintree *t) {if (T) {postordertraverse (t->lchild); Postordertraverse (T->rchild); printf ("%c", t->data); }}//Statistics Tree Depth int getdepth (Bintree *t) {int dep = 0, Depleft, depright;if (! T) dep = 0;else{depleft = getdepth (t->lchild);d epright = getdepth (t->rchild);d EP = 1 + Max (depleft, depright);} return DEP;} int main () {Bintree *tree; Tree = Createtree (tree);p rintf ("========= delimiter ============\n\n");p rintf ("Binary tree's first order traversal: \ n"); Preordertraverse (tree);p rintf ("\ n");p rintf ("Binary tree in the middle sequence traversal: \ n"), Inordertraverse (tree);p rintf ("\ n");p rintf ("post-order traversal of the binary tree: \ n "); Postordertraverse (tree);p rintf ("\ n");p rintf ("\n=========================\n");p rintf ("Leaf node count:%d\n", Sumleft (tree ));p rintf ("Binary Tree Depth:%d\n", getdepth (tree)); return 0;}
Data structure of the---C language implementation of the two-fork tree two-linked list storage representation