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

modify

上级 8af97102
#ifndef BINARYTREE_H_ #ifndef BINARYTREE_H_
#define BINARYTREE_H_ #define BINARYTREE_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
// //
// //
// Define the data structure here // Define the data structure here
// //
...@@ -25,18 +20,19 @@ ...@@ -25,18 +20,19 @@
typedef char ElemType; typedef char ElemType;
typedef struct BiTNode{ typedef struct BiTNode
{
ElemType data; // Binary tree node data ElemType data; // Binary tree node data
struct BiTNode* lchild; // Left child pointer struct BiTNode* lchild; // Left child pointer
struct BiTNode* rchild; // Right child pointer struct BiTNode* rchild; // Right child pointer
}BiTNode, *BiTree; }BiTNode, *BiTree;
typedef enum PushTimes{ typedef enum PushTimes
{
FirstTime, FirstTime,
SecondTime SecondTime
}PushTimes; }PushTimes;
// //
// Declare the function here // Declare the function here
// //
...@@ -46,12 +42,10 @@ BiTNode* CreateNode(ElemType data); ...@@ -46,12 +42,10 @@ BiTNode* CreateNode(ElemType data);
BiTree InitTree(); BiTree InitTree();
void DeleteTree(BiTree pTree); void DeleteTree(BiTree pTree);
// //
// Declare global variables here // Declare global variables here
// //
extern char g_string[MAX_STRING_SIZE]; extern char g_string[MAX_STRING_SIZE];
extern int g_length; extern int g_length;
#endif /* BINARYTREE_H_ */ #endif /* BINARYTREE_H_ */
#include "BinaryTree.h" #include "BinaryTree.h"
#include <stdlib.h>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
...@@ -16,7 +16,7 @@ int main(int argc, char* argv[]) ...@@ -16,7 +16,7 @@ int main(int argc, char* argv[])
// //
PostOrder(pTree); PostOrder(pTree);
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]);
} }
...@@ -41,13 +41,17 @@ returned value: ...@@ -41,13 +41,17 @@ 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_STRING_SIZE]; // String. Used to save the postscript sequence of a binary tree during traversal
// String. Used to save the postscript sequence of a binary tree during traversal
char g_string[MAX_STRING_SIZE];
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 PostOrder(BiTree pTree) int PostOrder(BiTree pTree)
{ {
BiTNode* Stack[MAX_STACK_SIZE]; // Stack. Used to store parent nodes BiTNode* Stack[MAX_STACK_SIZE]; // Stack. Used to store parent nodes
enum PushTimes TimesStack[MAX_STACK_SIZE]; // Stack. Used to store the number of times the parent node is pushed // Stack. Used to store the number of times the parent node is pushed
int top = 0; // Both stacks use the same top of the stack.0 means empty stack enum PushTimes TimesStack[MAX_STACK_SIZE];
// Both stacks use the same top of the stack.0 means empty stack
int top = 0;
BiTNode* pNode; // Binary tree node pointer BiTNode* pNode; // Binary tree node pointer
enum PushTimes Times; // Number of parent pushed on the stack enum PushTimes Times; // Number of parent pushed on the stack
...@@ -80,6 +84,15 @@ BiTNode* CreateNode(ElemType data) ...@@ -80,6 +84,15 @@ BiTNode* CreateNode(ElemType data)
return pNode; return pNode;
} }
// A binary tree preordered sequence string.
// notice:Using only preordered sequences does not determine a unique binary tree.
// So, two Spaces after the leaf node,
// Also, the sequence ends with the character '\0' at the end of the string.
// In this way, the preordering sequence determines the unique binary tree.
static const char* data = "-*a -b c /d e";
static int nIndex = 0; // The index of the binary tree first order sequence
/* /*
function: function:
Create a binary tree from the preordered sequence of the binary tree. Create a binary tree from the preordered sequence of the binary tree.
...@@ -87,28 +100,24 @@ function: ...@@ -87,28 +100,24 @@ function:
returned value: returned value:
Return pointer to a binary tree Return pointer to a binary tree
*/ */
static const char* data = "-*a -b c /d e"; // A binary tree preordered sequence string.
// notice:Using only preordered sequences does not determine a unique binary tree.
// So, two Spaces after the leaf node,
// Also, the sequence ends with the character '\0' at the end of the string.
// In this way, the preordering sequence determines the unique binary tree.
static int nIndex = 0; // The index of the binary tree first order sequence
BiTree InitTree() BiTree InitTree()
{ {
BiTNode* pRootNode; BiTNode* 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
...@@ -135,7 +144,7 @@ void DeleteTree(BiTree pTree) ...@@ -135,7 +144,7 @@ void DeleteTree(BiTree 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->lchild); DeleteTree(pTree->lchild);
DeleteTree(pTree->rchild); DeleteTree(pTree->rchild);
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论