C. Implement basic Binary Tree operations

Source: Internet
Author: User

 

/*** @ File gm_btree.h * @ brief * @ author Don Hao * @ date 2011-8-22 21:51:35 * @ version * <PRE> <B> copyright: </B> </PRE> * <PRE> <B> Email: </B> hao.limin@gmail.com </PRE> * <PRE> <B> company: </B> http://blog.csdn.net/donhao </PRE> * <PRE> <B> All Rights Reserved. </B> </PRE> * <PRE> <B> modification: </B> </PRE> * <PRE> write modifications here. </PRE> */# ifndef _ gm_btree_h # DEFINE _ gm_btree_h # include <stdlib. h> # include <stdio. h> typedef struct btree {char value; struct btree * lchild; struct btree * rchild;} btree_struct; # ifdef _ cplusplusextern "C" {# endif/** <_ cplusplus * // *** @ brief gm_btree_create ** create a tree. * @ return btree_struct **/btree_struct * gm_btree_create ();/*** @ brief gm_btree_preorder ** first traverse. * @ Param [in] tree */void gm_btree_preorder (btree_struct * tree);/*** @ brief gm_btree_inorder. * @ Param [in] tree */void gm_btree_inorder (btree_struct * tree);/*** @ brief gm_btree_postorder ** in the descending order of traversal. * @ Param [in] tree */void gm_btree_postorder (btree_struct * tree);/*** @ brief gm_btree_search_preorder ** first query. * @ Param [in] tree * @ Param [in] value */void gm_btree_search_preorder (btree_struct * tree, int value);/*** @ brief gm_btree_search_inorder ** in the search order. * @ Param [in] tree * @ Param [in] value */void gm_btree_search_inorder (btree_struct * tree, int value);/*** @ brief gm_btree_search_postorder ** searches in sequence. * @ Param [in] tree * @ Param [in] value */void gm_btree_search_postorder (btree_struct * tree, int value);/*** @ brief gm_btree_clear ** clear. * @ Param [in] tree */void gm_btree_clear (btree_struct * tree ); # ifdef _ cplusplus} # endif/** <_ cplusplus */# endif/** <_ gm_btree_h */

/*** @ File gm_btree.c * @ brief * @ author Don Hao * @ date 2011-8-22 21:51:13 * @ version * <PRE> <B> copyright: </B> </PRE> * <PRE> <B> Email: </B> hao.limin@gmail.com </PRE> * <PRE> <B> company: </B> http://blog.csdn.net/donhao </PRE> * <PRE> <B> All Rights Reserved. </B> </PRE> * <PRE> <B> modification: </B> </PRE> * <PRE> write modifications here. </PRE> */# include "inline" static btree_struct * ptree = NULL; btree_struct * gm_btree_create () {btree_struct * node = (btree_struct *) malloc (sizeof (btree_struct )); if (null = node) {printf ("malloc memory error when creating Tree"); return NULL;} scanf ("% C", & node-> value ); // enter the first order Abd. F .. eg .. H .. c .., where. null/* A/\ B C/\ D E \/\ f g H */If (node-> value = '. ') {return NULL;} node-> lchild = gm_btree_create (); node-> rchild = gm_btree_create (); Return node;} void gm_btree_preorder (btree_struct * tree) {If (null = tree) {return;} printf ("% C \ n", tree-> value); gm_btree_preorder (tree-> lchild ); gm_btree_preorder (tree-> rchild);} void gm_btree_inorder (btree_struct * tree) {If (null = tree) {return;} gm_btree_inorder (tree-> lchild ); printf ("% C \ n", tree-> value); gm_btree_inorder (tree-> rchild);} void gm_btree_postorder (btree_struct * tree) {If (null = tree) {return;} gm_btree_postorder (tree-> lchild); gm_btree_postorder (tree-> rchild); printf ("% C \ n", tree-> value );} void gm_btree_search_preorder (btree_struct * tree, int value) {If (null = tree) {return;} If (value = tree-> value) {printf ("find \ n");} gm_btree_search_preorder (tree-> lchild, value); gm_btree_search_preorder (tree-> rchild, value);} void trim (btree_struct * tree, int value) {If (null = tree) {return;} gm_btree_search_inorder (tree-> lchild, value); If (value = tree-> value) {printf ("find \ n");} gm_btree_search_inorder (tree-> rchild, value);} void gm_btree_search_postorder (btree_struct * tree, int value) {If (null = tree) {return;} gm_btree_search_postorder (tree-> lchild, value); gm_btree_search_postorder (tree-> rchild, value); If (value = tree-> value) {printf ("find \ n") ;}} void gm_btree_clear (btree_struct * tree) {If (null = tree) {return;} gm_btree_clear (tree-> lchild ); gm_btree_clear (tree-> rchild); tree-> lchild = NULL; tree-> rchild = NULL; tree = NULL;} void main () {ptree = gm_btree_create (); gm_btree_preorder (ptree); printf ("\ n"); gm_btree_inorder (ptree); printf ("\ n"); gm_btree_postorder (ptree); printf ("\ n "); gm_btree_search_preorder (ptree, 'F'); gm_btree_search_preorder (ptree, 'J'); values (ptree, 'F'); gm_btree_search_inorder (ptree, 'J'); values (ptree, 'F'); gm_btree_search_postorder (ptree, 'J'); gm_btree_clear (ptree); ptree = NULL ;}

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.