The self-built algorithm library of data structure--chain storage and basic operation of two-fork tree

Source: Internet
Author: User

This paper is a basic data structure series (6): Tree and two-fork tree in the 9th Class two binary tree operation and the implementation of the routine.

The single-link list algorithm library uses the program's multi-file organization, including two files:
  
1. header file: Btree.h, contains the code that defines the sequential table data structure, the macro definition, the declaration of the function to implement the algorithm;

#ifndef btree_h_included#define Btree_h_included#define MaxSizetypedefCharElemtype;typedefstructnode{elemtype data;//Data Elements    structNode *lchild;//pointing to left child    structNode *rchild;//pointing to right child} Btnode;voidCreatebtnode (Btnode *&b,Char*STR);//Two fork chain created by str stringBtnode *findnode (Btnode *b,elemtype x);//Returns a node pointer to the data field xBtnode *lchildnode (Btnode *p);//Returns the left child node pointer of the *P nodeBtnode *rchildnode (Btnode *p);//Returns the right child node pointer to the *P nodeintBtnodedepth (Btnode *b);//Find the depth of binary tree BvoidDispbtnode (Btnode *b);//output binary tree in parentheses notationvoidDestroybtnode (Btnode *&b);//Destroy binary tree#endif//btree_h_included

2. source file: Btree.cpp, which contains definitions of functions that implement various algorithms

#include <stdio.h>#include <malloc.h>#include "btree.h"voidCreatebtnode (Btnode *&b,Char*STR)//Two fork chain created by str string{Btnode *st[maxsize],*p=NULL;inttop=-1, k,j=0;CharCh b=NULL;//Established two-fork tree initially emptyCH=STR[J]; while(ch!=' + ')//str Loop when not finished scanning{Switch(CH) { Case ' (': top++;            St[top]=p; k=1; Break;//For left node         Case ' ) ': top--; Break; Case ', ': k=2; Break;//For right node        default: p= (Btnode *) malloc (sizeof(Btnode));            p->data=ch; P->lchild=p->rchild=NULL;if(b==NULL)//p point to the root node of the binary treeB=p;Else                            //Two fork root node established{Switch(k) { Case 1: st[top]->lchild=p; Break; Case 2: st[top]->rchild=p; Break;        }}} j + +;    CH=STR[J]; }}btnode *findnode (btnode *b,elemtype x)//Returns a node pointer to the data field x{Btnode *p;if(b==NULL)return NULL;Else if(b->data==x)returnbElse{P=findnode (b->lchild,x);if(p!=NULL)returnPElse            returnFindNode (B-&GT;RCHILD,X); }}btnode *lchildnode (Btnode *p)//Returns the left child node pointer of the *P node{returnP->lchild;} Btnode *rchildnode (Btnode *p)//Returns the right child node pointer to the *P node{returnP->rchild;}intBtnodedepth (Btnode *b)//Find the depth of binary tree B{intLCHILDDEP,RCHILDDEP;if(b==NULL)return(0);//The height of the empty tree is 0    Else{lchilddep=btnodedepth (b->lchild);//Find the height of the left subtree to be LCHILDDEPRchilddep=btnodedepth (B->rchild);//The height of the right sub-tree is RCHILDDEP        return(LCHILDDEP&GT;RCHILDDEP)? (lchilddep+1):(rchilddep+1); }}voidDispbtnode (Btnode *b)//output binary tree in parentheses notation{if(b!=NULL) {printf ("%c", B->data);if(b->lchild!=NULL|| b->rchild!=NULL) {printf ("("); Dispbtnode (B->lchild);if(b->rchild!=NULL) printf (",");            Dispbtnode (B->rchild); printf")"); }    }}voidDestroybtnode (Btnode *&b)//Destroy binary tree{if(b!=NULL) {Destroybtnode (b->lchild);        Destroybtnode (B->rchild);    Free (b); }}

3. In the process of building the algorithm library, in order to complete the test, build a source file (such as main.cpp) in the same project, and compile the main function to complete the relevant testing work.

#include <stdio.h>#include "btree.h"intMain () {Btnode*b,*p,*LP,*RP;;printf("(1) Create a two-fork tree:"); Createbtnode (b,"A (B (D,e (H (J,k (L,m (N)))), C (F,g (, I) ))");printf("\ n");printf("(2) output binary tree:"); Dispbtnode (b);printf("\ n");printf("(3) Find H node:"); P=findnode (b,' H ');if(P!=null) {Lp=lchildnode (P);if(Lp!=null)printf("left child for %c ", Lp->data);Else            printf("No left Child"); Rp=rchildnode (P);if(Rp!=null)printf("Right child is %c", Rp->data);Else            printf("No Right Child"); }Else        printf("Not Found!" ");printf("\ n");printf("(4) Depth of binary tree B:%d\ n", btnodedepth (b));printf("(5) Release two fork tree b\n"); Destroybtnode (b);return 0;}

Note: In the main function, the two fork tree created for testing is as follows--

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The self-built algorithm library of data structure--chain storage and basic operation of two-fork tree

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.