Home > other >  Please look at the binary tree node number statistics function, why two different calculated? Thank
Please look at the binary tree node number statistics function, why two different calculated? Thank

Time:10-06

# define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include

Static int index=0;

# define MAX_SIZE 24
Typedef char SeqTree [MAX_SIZE];

Void InitSeqTree (SeqTree tree)
{
int i=0;
For (I; I & lt; MAX_SIZE; I++)
{
Tree [I]='\ 0';
}
}

Void CreateSeqTree (SeqTree tree, int index)
{

Char ch;
Ch=getchar ();
The fflush (stdin);
If (ch=='^')
{
Tree [index]='\ 0';
return;
}

Tree [index]=ch;
Printf (" the left child: ");
CreateSeqTree (tree, 2 * index + 1);
Printf (" right child: ");
CreateSeqTree (tree, 2 * (index + 1));

}

Void Test (SeqTree tree)
{
InitSeqTree (tree);
Printf (" please enter the root node: ");
CreateSeqTree (tree, 0);
int i;
for ( i=0; I & lt; 15. I++)
{
Printf (" % d, % d) ", I, tree [I]);
}


}

Int GetLength (SeqTree tree)
{
Int len=0;
Int I=0;
For (I; I & lt; MAX_SIZE; I++)
{

If (tree [I]=='\ 0')
{

continue;
}
Len++;
}

Return len.
}

Int the Get (SeqTree tree)
{

Int len=0;
For (len=MAX_SIZE; Len & gt;=0; Len -)
{

If (tree [len - 1].='\ 0')
{

break;
}
}
Return len.
}
Int main ()
{
SeqTree tree;
The Test (tree);
Printf (" % d - & gt; \ n ", GetLength (tree));
Printf (" % d - & gt;" To Get (tree));




system("pause");
}

CodePudding user response:

1: if in the get function (tree [len - 1].='\ 0'), len=0, crossing the line
2: SeqTree tree if they are sparse, the elements on both sides not to 0, and the middle elements is zero, the two functions is not the same as the return value
  • Related