Home > Back-end >  Binary tree non-recursive storage type
Binary tree non-recursive storage type

Time:09-16



The Btree=Bnode *

Would you please tell me why the here use Btree * in the node is not just need a structure pointer Btree?

CodePudding user response:

The code is not complete, how you look at it, each have each use

CodePudding user response:

reference 1/f, Simple, Soft reply:
code is not complete, how you look at it, each have each use

#include
#include
# define maxsize 20

//-- -- -- -- -- -- -- the operation of the tree -- -- -- -- -- -- -- --
//declare
Typedef int elemtype;
Typedef struct Tnode
{
Elemtype data;
Struct Tnode * left;
Struct Tnode * right;
} Bnode, * Btree;

//2. Create
Btree Btree_create ()
{
Btree T;
Elemtype m;
The scanf (" % d ", & amp; m);
if(! M)
{
T=NULL;
return;
}
T=(Btree) malloc (sizeof (Bnode));
T - & gt; data=https://bbs.csdn.net/topics/m;
T - & gt; Left=Btree_create ();
T - & gt; Right=Btree_create ();
}

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

//4. Empty
Void Btree_clear (Btree * T)
{
if(!
(* T).return;
Btree_clear (& amp; (* T) - & gt; Left);
Btree_clear (& amp; (* T) - & gt; Right);
Free (* T);
* T=NULL;
}

//5. Locate
Btree Btree_search (Btree T, elemtype e)
{
if(! T)
return NULL;
If (T - & gt; data=https://bbs.csdn.net/topics/=e)
Return T;
if(! Btree_search (T - & gt; Left, e))
Return Btree_search (T - & gt; Right, e);
}

The depth of//6.
Int Btree_depth (Btree T)
{
Int m, n;
if(! T)
return 0;
M=Btree_depth (T - & gt; Left);
N=Btree_depth (T - & gt; Right);
Return (m> n? M: n) + 1;
}

//7.
leaf nodesInt Btree_leaf (Btree T)
{
Int m, n;
if(! T)
return 0;
if(! T - & gt; Left& & ! T - & gt; Right)
return 1;
M=Btree_leaf (T - & gt; Left);
N=Btree_leaf (T - & gt; Right);
The return of m + n.
}
//8. Summarize some
Int Btree_node (Btree T)
{
Int m, n;
if(! T)
return 0;
M=Btree_node (T - & gt; Left);
N=Btree_node (T - & gt; Right);
The return of m + n + 1;
}

Insert
//9.Void Btree_insert (Btree * T, elemtype e)
{
Btree p;
if(!
(* T).{
P=(Btree) malloc (sizeof (Bnode));
P - & gt; data=https://bbs.csdn.net/topics/e;
* T=p;
return;
}
If (e> (* T) - & gt; Data)
Btree_insert (& amp; (* T) - & gt; Right, e);
The else
Btree_insert (& amp; (* T) - & gt; Left, e);
}

//-- -- -- -- -- - the recursive traversal -- -- -- -- -- -- --
//1. Statement
Typedef struct node
{
Btree elem [maxsize];
Int top;
} sq_stack;

//3. Into the stack
Void stack_push (Btree sq_stack * s, T)
{
If (s - & gt; Top==maxsize)
return;
S - & gt; Elem [s - & gt; top]=T;
S - & gt; Top++;
}
//4. The stack
Btree stack_pop (sq_stack * s)
{
If (s - & gt; Top==0)
return NULL;
S - & gt; Top -;
The return of s - & gt; Elem [s - & gt; top];
}
//2. Traversal
Void Btree_travel2 (Btree T)
{
Sq_stack s;
Elemtype p [maxsize], I;
S.t op=0; i=0;
if(! T)
return;
While (T | | s.t op)
{
While (T)
{
P=T -/+ + I & gt; The data;
Stack_push (& amp; S, T);
T=T - & gt; Right;
}
T=stack_pop (& amp; s);
T=T - & gt; The left;
}
While (I)
Printf (" % d ", [I p -]);
}
//1. Traversal
Void Btree_travel1 (Btree T)
{
Sq_stack s;
S.t op=0;
if(! T)
return;
While (T | | s.t op)
{
While (T)
{
Printf (" % d ", T - & gt; The data);
Stack_push (& amp; S, T);
T=T - & gt; The left;
}
T=stack_pop (& amp; s);
T=T - & gt; Right;
}
}
//3. Traversal
Void Btree_travel3 (Btree T)
{
Sq_stack s;
S.t op=0;
Btree Bbool [maxsize];
if(! T)
return;
While (T | | s.t op)
{
While (T)
{
Stack_push (& amp; S, T);
Bbool [s.t op - 1]=0;
T=T - & gt; The left;
}
if(! Bbool [s.t op - 1])
{
Bbool [s.t op - 1]=1;
T=s.e lem [s.t op - 1] - & gt; Right;
}
The else
{
T=stack_pop (& amp; s);
Printf (" % d ", T - & gt; The data);
T=NULL;
}
}
}

//-- -- -- -- -- -- -- the sequence traversal -- -- -- -- -- -- -- --

//1. Statement
Typedef struct sq_queue
{
Btree base [maxsize];
Int the front;
Int rear;
} sq_queue;

//2. The team
Void queue_in (sq_queue * Q, Btree T)
{
If (Q - & gt; Rear - Q - & gt; The front==maxsize)
return;
Q - & gt; [Q - base & gt; rear++]=T;
}

//3. The team
Btree queue_out (sq_queue * Q)
{
If (Q - & gt; Rear==Q - & gt; Front)
return NULL;
Return the Q - & gt; Base [Q - & gt; front++];
}
//4. Found empty
Int queue_isempty (sq_queue Q)
{
If (Q.r ear==Q.f ront)
return 1;
return 0;
}
//1. Traversal
Void Btree_travel4 (Btree T)
{
Sq_queue Q;
Q.r ear=Q.f ront=0;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related