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

modify

上级 85ce66dc
#ifndef CSTREE_H_ #ifndef CSTREE_H_
#define CSTREE_H_ #define CSTREE_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
// //
...@@ -21,7 +19,8 @@ ...@@ -21,7 +19,8 @@
#define MAX_NUMBER 50 #define MAX_NUMBER 50
typedef struct CSNode{ typedef struct CSNode
{
char data; // Node data char data; // Node data
struct CSNode *firstchild; // Child Node struct CSNode *firstchild; // Child Node
struct CSNode *nextsibling; // Brother Node struct CSNode *nextsibling; // Brother Node
......
...@@ -30,8 +30,10 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem) ...@@ -30,8 +30,10 @@ struct CSNode* Push(Stack* pS, struct CSNode* 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 +58,10 @@ struct CSNode* Pop(Stack* pS) ...@@ -56,8 +58,10 @@ struct CSNode* 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
// //
#define MAX_STACK_LENGTH 64 // Maximum length of stack #define MAX_STACK_LENGTH 64 // Maximum length of stack
// Stack // Stack
struct CSNode; struct CSNode;
typedef struct Stack{ typedef struct Stack
{
struct CSNode* buffer[MAX_STACK_LENGTH]; // Stack buffer struct CSNode* 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
// //
...@@ -38,12 +33,8 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem); ...@@ -38,12 +33,8 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem);
struct CSNode* Pop(Stack* pS); struct CSNode* Pop(Stack* pS);
int StackEmpty(Stack* pS); int StackEmpty(Stack* pS);
// //
// Declare global variables here // Declare global variables here
// //
#endif /* STACK_H_ */ #endif /* STACK_H_ */
#include "CSTree.h" #include "CSTree.h"
#include <stdlib.h>
Stack stack; // Stack. Used to store nodes Stack stack; // Stack. Used to store nodes
...@@ -45,7 +46,9 @@ returned value: ...@@ -45,7 +46,9 @@ returned value:
Returns 1 if the traversal succeeds Returns 1 if the traversal succeeds
Returns 0 if the traversal fails Returns 0 if the traversal fails
*/ */
char g_string[MAX_NUMBER]; // String. Used to save the preordered sequence of a tree during traversal
// String. Used to save the preordered sequence of a tree during traversal
char g_string[MAX_NUMBER];
int g_length = 0; // The string length.0 indicates an empty string int g_length = 0; // The string length.0 indicates an empty string
int PreOrder(CSTree pTree) int PreOrder(CSTree pTree)
{ {
...@@ -98,7 +101,7 @@ void CreateSubTree(char* data, CSTree pRootNode) ...@@ -98,7 +101,7 @@ void CreateSubTree(char* data, CSTree pRootNode)
pRootNode->firstchild = CreateNode(data[1]); pRootNode->firstchild = CreateNode(data[1]);
pFirstChild = pRootNode->firstchild; pFirstChild = pRootNode->firstchild;
for(i = 2; data[i] != '\0'; i++) for (i = 2; data[i] != '\0'; i++)
{ {
pFirstChild->nextsibling = CreateNode(data[i]); pFirstChild->nextsibling = CreateNode(data[i]);
pFirstChild = pFirstChild->nextsibling; pFirstChild = pFirstChild->nextsibling;
...@@ -119,22 +122,45 @@ CSNode* PreOrderCreate(CSTree pTree, char Key) ...@@ -119,22 +122,45 @@ CSNode* PreOrderCreate(CSTree pTree, char Key)
{ {
CSNode* pNode = NULL; CSNode* pNode = NULL;
if(pTree != NULL) if (pTree != NULL)
{ {
if(pTree->data == Key) if (pTree->data == Key)
{
return pTree; return pTree;
}
pNode = PreOrderCreate(pTree->firstchild, Key); pNode = PreOrderCreate(pTree->firstchild, Key);
if(pNode != NULL) if (pNode != NULL)
{
return pNode; return pNode;
}
pNode = PreOrderCreate(pTree->nextsibling, Key); pNode = PreOrderCreate(pTree->nextsibling, Key);
if(pNode != NULL) if (pNode != NULL)
{
return pNode; return pNode;
}
} }
return NULL; return NULL;
} }
// Two dimensional array for initializing the tree,
// one subtree for each row of the two dimensional array,
// The first character initializes the root node of the subtree,
// The second character initializes the child node,
// The rest of the characters are used to initialize sibling nodes.notice:
// The first character of the first line is used to
// construct the root node of the entire tree,
// The first character of the remaining lines should be
// the child or sibling of the previous line.
const char data[MAX_NUMBER][MAX_NUMBER] =
{
{ 'R', 'A', 'B', 'C' },
{ 'A', 'D', 'E' },
{ 'C', 'F' },
{ 'F', 'G', 'H', 'K' }
};
/* /*
function: function:
Initialize the tree with a two-dimensional array. Initialize the tree with a two-dimensional array.
...@@ -142,20 +168,13 @@ function: ...@@ -142,20 +168,13 @@ function:
returned value: returned value:
Return tree pointer Return tree pointer
*/ */
const char data[MAX_NUMBER][MAX_NUMBER] =
{ { 'R', 'A', 'B', 'C' }, // Two dimensional array for initializing the tree, one subtree for each row of the two dimensional array,
{ 'A', 'D', 'E' }, // The first character initializes the root node of the subtree,The second character initializes the child node,
{ 'C', 'F' }, // The rest of the characters are used to initialize sibling nodes.notice:
{ 'F', 'G', 'H', 'K' } }; // The first character of the first line is used to construct the root node of the entire tree,
// The first character of the remaining lines should be the child or sibling of the previous line.
CSTree InitTree() CSTree InitTree()
{ {
int i = 0; int i = 0;
CSNode* pNode = NULL; CSNode* pNode = NULL;
CSTree pRootNode = CreateNode(data[0][0]); CSTree pRootNode = CreateNode(data[0][0]);
for(i = 0; data[i][0] != 0; i++) for (i = 0; data[i][0] != 0; i++)
{ {
pNode = PreOrderCreate(pRootNode, data[i][0]); pNode = PreOrderCreate(pRootNode, data[i][0]);
CreateSubTree((char*)data[i], pNode); CreateSubTree((char*)data[i], pNode);
...@@ -179,7 +198,7 @@ void DeleteTree(CSTree pTree) ...@@ -179,7 +198,7 @@ void DeleteTree(CSTree pTree)
// //
// The sequential traversal algorithm is implemented by recursion // The sequential traversal algorithm is implemented by recursion
// //
if(pTree != NULL) if (pTree != NULL)
{ {
DeleteTree(pTree->firstchild); DeleteTree(pTree->firstchild);
DeleteTree(pTree->nextsibling); DeleteTree(pTree->nextsibling);
...@@ -201,7 +220,7 @@ returned value: ...@@ -201,7 +220,7 @@ returned value:
void OutputResult() void OutputResult()
{ {
int i; int i;
for(i = 0; i < g_length - 1; i++) for (i = 0; i < g_length - 1; i++)
{ {
printf("%c", g_string[i]); printf("%c", g_string[i]);
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论