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

modify

上级 d6bd63ef
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
//
// Include the C standard library header file here
//
#include <stdio.h>
//
// Other header files are included here
//
//
// Define the data structure here
//
......@@ -25,13 +20,13 @@
typedef char ElemType;
typedef struct BiTNode{
typedef struct BiTNode
{
ElemType data; // Binary tree node data
struct BiTNode* lchild; // Left child pointer
struct BiTNode* rchild; // Right child pointer
}BiTNode, *BiTree;
//
// Declare the function here
//
......@@ -41,7 +36,6 @@ BiTNode* CreateNode(ElemType data);
BiTree InitTree();
void DeleteTree(BiTree pTree);
//
// Declare global variables here
//
......
#include "BinaryTree.h"
#include <stdlib.h>
int main(int argc, char* argv[])
{
......@@ -16,7 +16,7 @@ int main(int argc, char* argv[])
//
InOrder(pTree);
for(i = 0; i < g_length - 1; i++)
for (i = 0; i < g_length - 1; i++)
{
printf("%c", g_string[i]);
}
......@@ -41,8 +41,10 @@ returned value:
Returns 1 if the traversal succeeds
Returns 0 if the traversal fails
*/
char g_string[MAX_STRING_SIZE]; // String. Used to save the middle order sequence of a binary tree during traversal
int g_length = 0; // The string length.0 indicates an empty string
// String. Used to save the middle order 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 InOrder(BiTree pTree)
{
BiTNode* Stack[MAX_STACK_SIZE]; // Stack. Used to store parent nodes
......@@ -78,6 +80,15 @@ BiTNode* CreateNode(ElemType data)
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:
Create a binary tree from the preordered sequence of the binary tree.
......@@ -85,28 +96,24 @@ function:
returned value:
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()
{
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;
}
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++;
}
if(pRootNode != NULL)
if (pRootNode != NULL)
{
//
// Recursion is used to implement the first order traversal algorithm
......@@ -133,7 +140,7 @@ void DeleteTree(BiTree pTree)
//
// The sequential traversal algorithm is implemented by recursion
//
if(pTree != NULL)
if (pTree != NULL)
{
DeleteTree(pTree->lchild);
DeleteTree(pTree->rchild);
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论