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

modify

上级 2d0ad6b2
#ifndef BINARYSEARCHTREE_H_ #ifndef BINARYSEARCHTREE_H_
#define BINARYSEARCHTREE_H_ #define BINARYSEARCHTREE_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 <stdlib.h> #include <stdlib.h>
// //
// Define the data structure here // Define the data structure here
// //
typedef int KeyType; // Key field type typedef int KeyType; // Key field type
typedef enum _TreeNodeType{ typedef enum _TreeNodeType
{
TREE_ROOT, // root node TREE_ROOT, // root node
TREE_LEFT, // left child TREE_LEFT, // left child
TREE_RIGHT // right child TREE_RIGHT // right child
}TreeNodeType; }TreeNodeType;
typedef struct BinSearchNode{ typedef struct BinSearchNode
{
KeyType Key; // The key field of the node KeyType Key; // The key field of the node
struct BinSearchNode* lchild; // Left child pointer struct BinSearchNode* lchild; // Left child pointer
struct BinSearchNode* rchild; // Right child pointer struct BinSearchNode* rchild; // Right child pointer
}BinSearchNode, *PBinSearchNode; }BinSearchNode, *PBinSearchNode;
typedef struct BinSearchNodeExtra{ typedef struct BinSearchNodeExtra
{
TreeNodeType enumType; // The node type TreeNodeType enumType; // The node type
int flag; // The binary on which the tree is constructed int flag; // The binary on which the tree is constructed
int nLevel; // The node hierarchy in the binary tree. The root node is 1. int nLevel; // The node hierarchy in the binary tree. The root node is 1.
...@@ -56,7 +53,6 @@ typedef struct BinSearchNode* PBinSearchTree; // Binary sort tree pointer ...@@ -56,7 +53,6 @@ typedef struct BinSearchNode* PBinSearchTree; // Binary sort tree pointer
// Declare the function here // Declare the function here
// //
int Search(PBinSearchTree* pTree, KeyType Key, PBinSearchNode* position); int Search(PBinSearchTree* pTree, KeyType Key, PBinSearchNode* position);
int CreateSearchTree(PBinSearchTree* pTree, KeyType* Key, int Length); int CreateSearchTree(PBinSearchTree* pTree, KeyType* Key, int Length);
void DeleteTree(PBinSearchTree pTree); void DeleteTree(PBinSearchTree pTree);
......
...@@ -116,7 +116,7 @@ void DeleteTree(PBinSearchTree pTree) ...@@ -116,7 +116,7 @@ void DeleteTree(PBinSearchTree 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);
...@@ -125,8 +125,6 @@ void DeleteTree(PBinSearchTree pTree) ...@@ -125,8 +125,6 @@ void DeleteTree(PBinSearchTree pTree)
} }
} }
/* /*
function: function:
Get the binary sort tree node.Use recursion to implement preorder traversal. Get the binary sort tree node.Use recursion to implement preorder traversal.
...@@ -139,7 +137,8 @@ parameter: ...@@ -139,7 +137,8 @@ parameter:
flag -- The binary on which the tree is constructed flag -- The binary on which the tree is constructed
returned value: returned value:
If the expression constructed from the address evaluates successfully, return 1. Otherwise, return 0. If the expression constructed from the address evaluates
successfully, return 1. Otherwise, return 0.
*/ */
int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag) int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag)
{ {
...@@ -147,8 +146,10 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag ...@@ -147,8 +146,10 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
int i; int i;
// The recursion ends here // The recursion ends here
if(NULL == *pTree) if (NULL == *pTree)
{
return 1; return 1;
}
i = count; i = count;
nodeArray[i].node.Key = (*pTree)->Key; nodeArray[i].node.Key = (*pTree)->Key;
nodeArray[i].node.lchild = (*pTree)->lchild; nodeArray[i].node.lchild = (*pTree)->lchild;
...@@ -160,20 +161,30 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag ...@@ -160,20 +161,30 @@ int CopyNode(PBinSearchTree *pTree, int nLevel, TreeNodeType nodeType, int flag
++count; ++count;
temp2 = temp1 = flag; temp2 = temp1 = flag;
if(nodeType == TREE_ROOT) if (nodeType == TREE_ROOT)
{
temp1 = flag; temp1 = flag;
}
else if (flag == 0) else if (flag == 0)
{
temp1 = nodeType == TREE_LEFT ? (flag + 1) << 1 : flag << 1; temp1 = nodeType == TREE_LEFT ? (flag + 1) << 1 : flag << 1;
}
else else
{
temp1 = flag << 1; temp1 = flag << 1;
}
temp2 = temp1; temp2 = temp1;
// Recursively compute left child and right child // Recursively compute left child and right child
if(!CopyNode(&(nodeArray[i].node.lchild), nLevel+1, TREE_LEFT, temp1)) if (!CopyNode(&(nodeArray[i].node.lchild), nLevel + 1, TREE_LEFT, temp1))
{
return 0; return 0;
}
if(!CopyNode(&(nodeArray[i].node.rchild), nLevel+1, TREE_RIGHT, temp2)) if (!CopyNode(&(nodeArray[i].node.rchild), nLevel + 1, TREE_RIGHT, temp2))
{
return 0; return 0;
}
return 1; return 1;
} }
...@@ -192,29 +203,35 @@ returned value: ...@@ -192,29 +203,35 @@ returned value:
void OutputResult(PBinSearchTree* pTree) void OutputResult(PBinSearchTree* pTree)
{ {
int i, flag, deep, j; int i, flag, deep, j;
if(CopyNode(pTree, 1, TREE_ROOT, 0) == 0) if (CopyNode(pTree, 1, TREE_ROOT, 0) == 0)
{ {
printf("Binary sort tree construct failed\n"); printf("Binary sort tree construct failed\n");
return; return;
} }
for(i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
flag = nodeArray[i].nodeExtra.flag; flag = nodeArray[i].nodeExtra.flag;
deep = nodeArray[i].nodeExtra.nLevel - 1; deep = nodeArray[i].nodeExtra.nLevel - 1;
for(j=deep-1; j>0; j--) for (j=deep-1; j>0; j--)
{
if (flag & (1 << j))
{ {
if(flag & (1 << j))
printf(" | "); printf(" | ");
else if(deep > 1) }
else if (deep > 1)
{
printf(" "); printf(" ");
} }
if(deep > 0) }
if (deep > 0)
{
printf(" +---"); printf(" +---");
}
printf("%2d ",nodeArray[i].node.Key); printf("%2d ", nodeArray[i].node.Key);
switch (nodeArray[i].nodeExtra.enumType) switch (nodeArray[i].nodeExtra.enumType)
{ {
case TREE_LEFT: case TREE_LEFT:
...@@ -225,7 +242,7 @@ void OutputResult(PBinSearchTree* pTree) ...@@ -225,7 +242,7 @@ void OutputResult(PBinSearchTree* pTree)
break; break;
case TREE_ROOT: case TREE_ROOT:
default: default:
; break;
} }
printf("\n"); printf("\n");
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论