Data structure of the---C language implementation of the two-fork tree two-linked list storage representation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.