/* ============================================================================ Name : bintree.cpp Author : ntsk13 [email protected] Version : Copyright : GPL Description : Bintree study, complement by C++ Date : 2015.06.29 ============================================================================ */ #include <iostream> using namespace std; typedef struct Bintree{ int data; char name; struct Bintree *left; struct Bintree *right; struct Bintree *parent; }Btree,*pBtree; void btree_init(pBtree & btroot,int data,char name); int btree_add_node(pBtree &p,int data,char name, int left); pBtree btree_get_left(pBtree &p); pBtree btree_get_right(pBtree &p); int btree_get_depth(pBtree &p); void btree_clear(pBtree &p); pBtree btree_find_node(pBtree &p, int data); void btree_traverse_dlr(pBtree &p); int btree_is_empty(pBtree &p); int is_zero(int v) { return v ? 1 :0; } int main(void) { pBtree root=NULL; pBtree one=NULL; pBtree two=NULL; pBtree three=NULL; cout<<"is empty ? "<< btree_is_empty(root) <<endl; btree_init(root,1,'A'); btree_add_node(root,2,'B',1); btree_add_node(root,3,'C',0); btree_add_node(root->left,4,'D',1); btree_add_node(root->left,5,'E',0); btree_add_node(root->right,6,'F',1); btree_traverse_dlr(root); cout<<"is empty ? "<<btree_is_empty(root)<<endl; cout<<"============================================"<<endl; one=btree_get_left(root->left); two=btree_get_right(root->left); btree_add_node(one,7,'G',1); btree_add_node(one,8,'H',0); btree_add_node(two,9,'I',1); btree_add_node(two,10,'J',0); btree_traverse_dlr(root); cout<<"is empty ? "<<btree_is_empty(root)<<endl; cout<<"============================================"<<endl; cout<<"depth is "<<btree_get_depth(root)<<endl; btree_clear(root); btree_traverse_dlr(root); cout<<"is empty ? "<<btree_is_empty(root)<<endl; } void btree_init(pBtree & btroot,int data,char name) { btroot=new Btree; if(0==btroot) { cout<<"error,can not new a Btree node !!!"<<endl; return; } btroot->data=data; btroot->name=name; btroot->left=NULL; btroot->right=NULL; btroot->parent=NULL; } int btree_add_node(pBtree &p,int data,char name, int left) { if(!p) { cout<<"Erros,Parent node is not exist !!!"<<endl; return 0; } if(left)// add left node { p->left=new Btree; if(0==p->left) { cout<<"error,can not new a Btree node !!!"<<endl; return 0; } p->left->data=data; p->left->name=name; p->left->left=NULL; p->left->right=NULL; p->left->parent=p; return 1; }else{ p->right=new Btree; if(0==p->right) { cout<<"error,can not new a Btree node !!!"<<endl; return 0; } p->right->data=data; p->right->name=name; p->right->left=NULL; p->right->right=NULL; p->right->parent=p; return 1; } } pBtree btree_get_left(pBtree &p) { return p->left; } pBtree btree_get_right(pBtree &p) { return p->right; } int btree_get_depth(pBtree &p) { int depth_left,depth_right; if( !p ) return 0; depth_left=btree_get_depth(p->left); depth_right=btree_get_depth(p->right); if(depth_left > depth_right) return depth_left+1; else return depth_right+1; } void btree_clear(pBtree &p) { if(p) { btree_clear(p->left); btree_clear(p->right); free(p); p=NULL; } } pBtree btree_find_node(pBtree &p, int data) { pBtree pt; if(p){ if( p->data==data) return p; else{ if(pt=btree_find_node(p->left,data)) return pt; else if(pt=btree_find_node(p->right,data)) return pt; } }else return NULL; } void btree_traverse_dlr(pBtree &p) { if(p) { cout<<"name is "<<p->name<<",data is "<<p->data<<endl; btree_traverse_dlr(p->left); btree_traverse_dlr(p->right); } } int btree_is_empty(pBtree &p) { return p ? 0 : 1; }