Home > other >  Binary tree basic operational problems
Binary tree basic operational problems

Time:09-24

# include
# include
Typedef int elemtype;

//declare
Typedef struct Tnode
{
Elemtype data;
Struct Tnode * left;
Struct Tnode * right;
} Bnode, * Btree;
//1. Initialization
Void Btree_init (Btree * T)
{
* T=NULL;
}

//2. Remove
Void Btree_clear (Btree * T)
{
if(! (* T)) return;
Btree_clear (& amp; (* T) - & gt; Left);
Btree_clear (& amp; (* T) - & gt; Right);
Free (* T);
T=NULL;
}
//1. Create
Btree Btree_create ()
{
Int m;
Btree T;
The scanf (" % d ", & amp; M);
T=(Btree) malloc (sizeof (Bnode));
If (m<=0) return NULL;
T - & gt; data=https://bbs.csdn.net/topics/m;
Printf (" please enter a value: % d left node ", m);
T - & gt; Left=Btree_create ();
Printf (" please enter a value: % d right node ", m);
T - & gt; Right=Btree_create ();
Return T;
}

//2. Printed
Void Btree_print (Btree T)
{
if(! T)
return;
Printf (" % d ", T - & gt; The data);
Btree_print (T - & gt; Left);
Btree_print (T - & gt; Right);
}

Depth
//3.Int Btree_depth (Btree T)
{
if(! T)
return 0;
Int m=Btree_depth (T - & gt; Left);
Int n=Btree_depth (T - & gt; Right);
If (m> N)
The return of m + 1;
Return the n + 1;
}

//4.
leaf nodesInt Btree_leaf (Btree T)
{
if(! T)
return 0;
if(! T - & gt; Left& & ! T - & gt; Right)
return 1;
Return Btree_leaf (T - & gt; Left) + Btree_leaf (T - & gt; Right);
}

//5. Locate
Void Btree_search (Btree T, elemtype e)
{
if(! T)
return NULL;
If (T - & gt; data=https://bbs.csdn.net/topics/=e)
{
Printf (" \ n find success ");
return;
}
Btree_search (T - & gt; Left, e);
Btree_search (T - & gt; Right, e);
}
Insert
//5.Void Btree_insert (Btree * T, elemtype e)
{
Btree p;
Printf (" hello ");
if(!
(* T).{
P=(Btree) malloc (sizeof (Bnode));
p-> data=https://bbs.csdn.net/topics/e;
* T=p;
Printf (" hello ");
return;
}
Printf (" hello ");
If (e> (* T) - & gt; Data)
Btree_insert ((* T) - & gt; Right, e);
The else
Btree_insert ((* T) - & gt; Left, e);
}

Delete//6.
Btree Btree_delete (Btree * T)
{
if(!
(* T).return NULL;

}
Statistical
//6.Int Btree_count (Btree T)
{
if(! T)
return 0;
Return Btree_count (T - & gt; Left) + Btree_count (T - & gt; Right) + 1;
}

Int main ()
{
Btree T;
T=Btree_create ();
Btree_print (T);
Depth of printf (" % d \ n ", Btree_depth (T));
Printf (" % d \ n "leaf nodes, Btree_leaf (T));
Btree_search (T, 5);
Printf (" summary points % d \ n ", Btree_count (T));
Btree_insert (& amp; T, 6);
Btree_print (T);
}



Everybody is good, can you tell me why can't insert operation
  • Related