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

modify

上级 85ce66dc
#ifndef CSTREE_H_
#define CSTREE_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
......@@ -21,7 +19,8 @@
#define MAX_NUMBER 50
typedef struct CSNode{
typedef struct CSNode
{
char data; // Node data
struct CSNode *firstchild; // Child Node
struct CSNode *nextsibling; // Brother Node
......
......@@ -30,8 +30,10 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem)
//
// Stack full, push failed.
//
if(MAX_STACK_LENGTH < pS->top)
if (MAX_STACK_LENGTH < pS->top)
{
return 0;
}
pS->top++;
pS->buffer[pS->top] = Elem; // Insert the element at the top of the stack
......@@ -56,8 +58,10 @@ struct CSNode* Pop(Stack* pS)
//
// The stack is empty and the pop fails
//
if(StackEmpty(pS))
if (StackEmpty(pS))
{
return 0;
}
Elem = pS->buffer[pS->top];
pS->top--;
......
#ifndef STACK_H_
#define STACK_H_
//
// Include the C standard library header file here
//
//
// Other header files are included here
//
//
// Define the data structure here
//
#define MAX_STACK_LENGTH 64 // Maximum length of stack
// Stack
struct CSNode;
typedef struct Stack{
typedef struct Stack
{
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;
//
// Declare the function here
//
......@@ -38,12 +33,8 @@ struct CSNode* Push(Stack* pS, struct CSNode* Elem);
struct CSNode* Pop(Stack* pS);
int StackEmpty(Stack* pS);
//
// Declare global variables here
//
#endif /* STACK_H_ */
#include "CSTree.h"
#include <stdlib.h>
Stack stack; // Stack. Used to store nodes
......@@ -45,7 +46,9 @@ returned value:
Returns 1 if the traversal succeeds
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 PreOrder(CSTree pTree)
{
......@@ -98,7 +101,7 @@ void CreateSubTree(char* data, CSTree pRootNode)
pRootNode->firstchild = CreateNode(data[1]);
pFirstChild = pRootNode->firstchild;
for(i = 2; data[i] != '\0'; i++)
for (i = 2; data[i] != '\0'; i++)
{
pFirstChild->nextsibling = CreateNode(data[i]);
pFirstChild = pFirstChild->nextsibling;
......@@ -119,22 +122,45 @@ CSNode* PreOrderCreate(CSTree pTree, char Key)
{
CSNode* pNode = NULL;
if(pTree != NULL)
if (pTree != NULL)
{
if (pTree->data == Key)
{
if(pTree->data == Key)
return pTree;
}
pNode = PreOrderCreate(pTree->firstchild, Key);
if(pNode != NULL)
if (pNode != NULL)
{
return pNode;
}
pNode = PreOrderCreate(pTree->nextsibling, Key);
if(pNode != NULL)
if (pNode != NULL)
{
return pNode;
}
}
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:
Initialize the tree with a two-dimensional array.
......@@ -142,20 +168,13 @@ function:
returned value:
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()
{
int i = 0;
CSNode* pNode = NULL;
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]);
CreateSubTree((char*)data[i], pNode);
......@@ -179,7 +198,7 @@ void DeleteTree(CSTree pTree)
//
// The sequential traversal algorithm is implemented by recursion
//
if(pTree != NULL)
if (pTree != NULL)
{
DeleteTree(pTree->firstchild);
DeleteTree(pTree->nextsibling);
......@@ -201,7 +220,7 @@ returned value:
void OutputResult()
{
int i;
for(i = 0; i < g_length - 1; i++)
for (i = 0; i < g_length - 1; i++)
{
printf("%c", g_string[i]);
}
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论