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

modify

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