Binary tree construction, traversal, and release--writing data structures by itself

Source: Internet
Author: User

Directly on the code

The bitree.h file is as follows:

#ifndef _bitree_h_#define _bitree_h_typedef  char telemtype;typedef struct _bitnode{    telemtype data;    struct _bitnode *lchild,*rchild;                } Bitnode,*pbitnode;int bittree_creat (Bitnode **t); void Pre_order (Bitnode *t); void Mid_order (Bitnode *T); void Post_ Order (Bitnode *t); #endif

bittree.c files are as follows

/*************************** Time: 2014.12.16xiao_ping_ping compilation environment: dev-c++ 4.9.9.2 Content: Two fork tree construction, traversal, and release capabilities: Learn to write data structures ************ (1) scanf ("%c", &ch), and scanf ("%d", &ch), the difference being that the former is a one-time input, The latter need to enter a number after the carriage return (2) C language pointer as a function parameter, the function cannot change the value of the pointer, can only change the pointer to the content of the value (only with a level two pointer), C + + can be referenced by * (&AMP;P) to change the value of the parameter pointer inside the function (3) It is also impossible to implement a pointer (including a struct pointer) as a function parameter to its task within the function of the non-allocated space (but can complete the free operation), another solution is to initialize a pointer inside the function after the allocation of space, and then passed the form of the return value to the external pointer ******** /#include <string.h> #include <stdio.h> #include "bitree.h"/* Create a two-fork tree */int bittree     _creat (Bitnode **t)//c language functions cannot be changed inside the pointer as a parameter {char ch;          scanf ("%c", &ch);     *t = * (T)->lchild;         if (' 0 ' = = ch) {*t = NULL;       return 0;      } *t = (Bitnode *) malloc (sizeof (Bitnode));             if (NULL = = *t) {return-1;          } (*t)->data = ch;        Bittree_creat (& (*t)->lchild);                 Bittree_creat (& (*t)->rchild); return 1;} /* Pre-sequence Traversal binary tree */void PRE_order (Bitnode *t) {if (NULL = = T) {return;     } printf ("%c", t->data);      Pre_order (T->lchild);              Pre_order (T->rchild);    }/* Middle sequence Traversal binary tree */void mid_order (Bitnode *t) {if (NULL = = T) {return;      } mid_order (T->lchild);     printf ("%c", t->data);     Mid_order (T->rchild);    }/* post-traversal binary tree */void post_order (Bitnode *t) {if (NULL = = T) {return;      } post_order (T->lchild);         Post_order (T->rchild); printf ("%c", T->data);}            /* Free two fork tree */int free_btree (Bitnode *t) {if (NULL = = T) {return 0;    } free_btree (T->lchild);    Free_btree (T->rchild);         Free (T);  return 1;}

The test file test.c is as follows:

/*************************************************/#include <string.h> #include <stdio.h> #include < conio.h> #include "bitree.h" int main () {    pbitnode btree;    printf ("Create binary tree:\n");    Bittree_creat (&btree);        printf ("\ n pre-order:");    Pre_order (btree);    printf ("\ n sequence:");    Mid_order (btree);    printf ("N-post:");    Post_order (btree);        printf ("\ nthe binary Tree Destruction");    Free_btree (btree);         Getch ();    return 0;       }

The results of the operation are as follows:


Binary tree construction, traversal, and release--writing data structures by itself

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.