提交 46b215ee 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 dc2d0a5c
#include "Stack.h" #include "Stack.h"
/* /*
function: function:
Initialize the stack. Initialize the stack.
...@@ -30,8 +29,10 @@ struct BiThrNode* Push(Stack* pS, struct BiThrNode* Elem) ...@@ -30,8 +29,10 @@ struct BiThrNode* Push(Stack* pS, struct BiThrNode* Elem)
// //
// Stack full, push failed. // Stack full, push failed.
// //
if(MAX_STACK_LENGTH < pS->top) if (MAX_STACK_LENGTH < pS->top)
{
return 0; return 0;
}
pS->top++; pS->top++;
pS->buffer[pS->top] = Elem; // Insert the element at the top of the stack pS->buffer[pS->top] = Elem; // Insert the element at the top of the stack
...@@ -56,8 +57,10 @@ struct BiThrNode* Pop(Stack* pS) ...@@ -56,8 +57,10 @@ struct BiThrNode* Pop(Stack* pS)
// //
// The stack is empty and the pop fails // The stack is empty and the pop fails
// //
if(StackEmpty(pS)) if (StackEmpty(pS))
{
return 0; return 0;
}
Elem = pS->buffer[pS->top]; Elem = pS->buffer[pS->top];
pS->top--; pS->top--;
......
#ifndef STACK_H_ #ifndef STACK_H_
#define STACK_H_ #define STACK_H_
// //
// Include the C standard library header file here // Include the C standard library header file here
// //
// //
// Other header files are included here // Other header files are included here
// //
// //
// Define the data structure here // Define the data structure here
// //
...@@ -22,12 +17,13 @@ ...@@ -22,12 +17,13 @@
// Stack // Stack
struct BiThrNode; struct BiThrNode;
typedef struct Stack{ typedef struct Stack
{
struct BiThrNode* buffer[MAX_STACK_LENGTH]; // Stack buffer struct BiThrNode* buffer[MAX_STACK_LENGTH]; // Stack buffer
int top; // Indicates the position at the top of the stack, not the number of elements in the stack // Indicates the position at the top of the stack, not the number of elements in the stack
int top;
}Stack; }Stack;
// //
// Declare the function here // Declare the function here
// //
...@@ -37,12 +33,8 @@ struct BiThrNode* Pop(Stack* pS); ...@@ -37,12 +33,8 @@ struct BiThrNode* Pop(Stack* pS);
int StackEmpty(Stack* pS); int StackEmpty(Stack* pS);
struct BiThrNode* Push(Stack* pS, struct BiThrNode* Elem); struct BiThrNode* Push(Stack* pS, struct BiThrNode* Elem);
// //
// Declare global variables here // Declare global variables here
// //
#endif /* STACK_H_ */ #endif /* STACK_H_ */
#ifndef THREADBINARYTREE_H_ #ifndef THREADBINARYTREE_H_
#define THREADBINARYTREE_H_ #define THREADBINARYTREE_H_
// //
// Include the C standard library header file here // Include the C standard library header file here
// //
#include <stdio.h> #include <stdio.h>
// //
// Other header files are included here // Other header files are included here
// //
#include "Stack.h" #include "Stack.h"
// //
// Define the data structure here // Define the data structure here
// //
...@@ -26,7 +22,8 @@ ...@@ -26,7 +22,8 @@
typedef char ElemType; typedef char ElemType;
typedef unsigned int PtrTag; typedef unsigned int PtrTag;
typedef struct _BiThrNode{ typedef struct _BiThrNode
{
ElemType data; // Binary tree node data ElemType data; // Binary tree node data
struct _BiThrNode* lchild; // Left child pointer struct _BiThrNode* lchild; // Left child pointer
struct _BiThrNode* rchild; // Right child pointer struct _BiThrNode* rchild; // Right child pointer
...@@ -48,8 +45,6 @@ void OutputResult(PBiThrTree pHead, PBiThrTree pTree); ...@@ -48,8 +45,6 @@ void OutputResult(PBiThrTree pHead, PBiThrTree pTree);
// Declare global variables here // Declare global variables here
// //
extern Stack stack; extern Stack stack;
#endif /* THREADBINARYTREE_H_ */ #endif /* THREADBINARYTREE_H_ */
...@@ -95,18 +95,20 @@ PBiThrTree InitTree() ...@@ -95,18 +95,20 @@ PBiThrTree InitTree()
{ {
BiThrNode* pRootNode; BiThrNode* pRootNode;
if('\0' == data[nIndex]) // End of preordering sequence string of binary tree if ('\0' == data[nIndex]) // End of preordering sequence string of binary tree
{
pRootNode = NULL; pRootNode = NULL;
}
else else
{ {
// //
// Create parent node // Create parent node, Empty nodes must be ignored
// //
pRootNode = (' ' == data[nIndex] ? NULL : CreateNode(data[nIndex])); // Empty nodes must be ignored pRootNode = (' ' == data[nIndex] ? NULL : CreateNode(data[nIndex]));
nIndex++; nIndex++;
} }
if(pRootNode != NULL) if (pRootNode != NULL)
{ {
// //
// Recursion is used to implement the first order traversal algorithm // Recursion is used to implement the first order traversal algorithm
...@@ -123,22 +125,22 @@ void OutputResult(PBiThrTree pHead, PBiThrTree pTree) ...@@ -123,22 +125,22 @@ void OutputResult(PBiThrTree pHead, PBiThrTree pTree)
BiThrNode* pNode2; BiThrNode* pNode2;
BiThrNode* pPreNode2; BiThrNode* pPreNode2;
if(pHead->lchild == NULL) if (pHead->lchild == NULL)
{ {
return 0; return 0;
} }
pNode2 = pHead->lchild; pNode2 = pHead->lchild;
pPreNode2 = NULL; pPreNode2 = NULL;
while(1) while (1)
{ {
while(1) while (1)
{ {
if(pNode2->ltag == 0 && (pPreNode2 == NULL || pPreNode2 != pNode2->lchild)) if (pNode2->ltag == 0 && (pPreNode2 == NULL || pPreNode2 != pNode2->lchild))
{ {
pNode2 = pNode2->lchild; pNode2 = pNode2->lchild;
} }
else if(pNode2->rtag == 0) else if (pNode2->rtag == 0)
{ {
printf("%c ", pNode2->data); printf("%c ", pNode2->data);
pNode2 = pNode2->rchild; pNode2 = pNode2->rchild;
...@@ -155,13 +157,15 @@ void OutputResult(PBiThrTree pHead, PBiThrTree pTree) ...@@ -155,13 +157,15 @@ void OutputResult(PBiThrTree pHead, PBiThrTree pTree)
pNode2 = pNode2->rchild; pNode2 = pNode2->rchild;
if(pNode2 == pTree) if (pNode2 == pTree)
{ {
if(pNode2->data != '\0') if (pNode2->data != '\0')
{
printf("%c ", pNode2->data); printf("%c ", pNode2->data);
}
pNode2 = pNode2->rchild; pNode2 = pNode2->rchild;
} }
if(pNode2 == pHead) if (pNode2 == pHead)
{ {
break; break;
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论