Skip to content

Commit

Permalink
完善查找的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
NICE-FUTURE committed Aug 19, 2018
1 parent 215289f commit b39c830
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 79 deletions.
152 changes: 102 additions & 50 deletions cprogram/binaryTree/binaryTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,11 @@ bool TreeIsFull(const Tree *tree)

int TrnodesCount(const Tree *tree)
{
// //创建临时树作为参数树的拷贝
// Tree tempTree;
// InitializeTree(&tempTree);
// tempTree = *tree;
//
// //记录临时树的根节点
// Trnode *p = tempTree.root;
//
// //创建计数器(静态)
// static int cnt = 0;
// cnt++;
//
// //递归遍历
// if (TreeHasEmpty(&tempTree)) {
// return 0;
// } else {
// if (p->left != NULL) {
// tempTree.root = p->left;
// TrnodesCount(&tempTree);
// }
// if (NULL != p->right) {
// tempTree.root = p->right;
// TrnodesCount(&tempTree);
// }
// }
//
// return cnt;
return tree->total;
}


void toLowerString(char *name, const char *name0)
static void toLowerString(char *name, const char *name0)
{
strcpy(name, name0);

Expand All @@ -92,17 +65,46 @@ void toLowerString(char *name, const char *name0)
}
}

static bool toLeft(const char *originName, const char *targetName)
{
char name0[NAME_LENGTH], name[NAME_LENGTH];

//忽略大小写,一律转换为小写进行比较
toLowerString(name0, originName);
toLowerString(name, targetName);

if (strcmp(name0, name) > 0) {
return true;
} else {
return false;
}
}

bool AddTrnode(Tree *tree, const Item *item)
static bool toRight(const char *originName, const char *targetName)
{
char name0[NAME_LENGTH], name[NAME_LENGTH];

//忽略大小写,一律转换为小写进行比较
toLowerString(name0, originName);
toLowerString(name, targetName);

if (strcmp(name0, name) < 0) {
return true;
} else {
return false;
}
}

bool AddTrnode(Tree *tree, Item item)
{
//申请一块新内存
Trnode *p = (Trnode*) malloc(sizeof(Trnode));

//空间有效
if (NULL != p) {
p->item = *item;
p->item = item;
if (TreeIsFull(tree)) {
printf("Sorry, space is full.\n"); //空间已满
fprintf(stderr, "Sorry, space is full.\n"); //空间已满
return false;
} else {
if (TreeIsEmpty(tree)) { //空树
Expand All @@ -111,24 +113,9 @@ bool AddTrnode(Tree *tree, const Item *item)
p->right = NULL;
} else {
//找到节点添加的位置

/*只存储小写,是大写则转换*/
//忽略大小写,一律转换为小写
char name[NAME_LENGTH], name0[NAME_LENGTH];
toLowerString(name, item->name);

//根据小写字母顺序确定添加的位置
Trnode *temp = tree->root;
int flag = 0;
do {
/*每次和不同的节点比较,节点的名字也要转换成小写*/
toLowerString(name0, (temp->item).name);
flag = strcmp(name0, name); //字符串1在字符串2前面,返回 负数;否则相反
if (0 == flag) {
printf("Sorry, name: %s already exists.\n", name);
return false;
}
if (flag > 0) { //往左走
if (toLeft((temp->item).name, item.name)) { //往左走
if (temp->left != NULL) {
temp = temp->left; //继续往下遍历
} else {
Expand All @@ -137,7 +124,8 @@ bool AddTrnode(Tree *tree, const Item *item)
p->right = NULL;
break;
}
} else { //往右走
}
else if (toRight((temp->item).name, item.name)) { //往右走
if (temp->right != NULL) {
temp = temp->right;
} else {
Expand All @@ -147,6 +135,10 @@ bool AddTrnode(Tree *tree, const Item *item)
break;
}
}
else { //关键值重复
fprintf(stderr, "Sorry, name: %s already exists.\n", item.name);
return false;
}
}while (temp != NULL);
}
}
Expand All @@ -155,11 +147,70 @@ bool AddTrnode(Tree *tree, const Item *item)
return true;
}

Trnode* SearchTrnode(const Tree *tree, const Item *item)
bool SearchTrnode(const Tree *tree, const char *name, Trnode **pnode, Trnode **plast)
{
Trnode *temp = tree->root;

if (TreeIsEmpty(tree)) {
fprintf(stderr, "Empty tree, no data.\n");
return false;
} else {
do {
if (toLeft((temp->item).name, name)) {
/*printf("to left...\n");*/
*plast = temp; //记录上一个节点的地址
temp = temp->left;
}
else if (toRight((temp->item).name, name)) {
/*printf("to right...\n");*/
*plast = temp; //记录上一个节点的地址
temp = temp->right;
}
else {
*pnode = temp; //返回当前节点的地址
return true;
}
}while (NULL != temp);
}
return false;
}

void printTrnode(const Tree *tree)
{
if (TreeIsEmpty(tree)) {
fprintf(stderr, "Empty tree, no data.\n");
return;
}

//创建临时树作为参数树的拷贝
Tree tempTree;
InitializeTree(&tempTree);
tempTree = *tree;

//记录临时树的根节点
Trnode *p = tempTree.root;

//递归遍历
if (TreeHasEmpty(&tempTree)) { //无路可走,返回上一层
printf("%-20s\t %-10d\t %-15p\t %-15p\t %-15p\n",(p->item).name, (p->item).value, p, p->left, p->right);
return ;
} else {
if (p->left != NULL) { //左边有路,往左走
tempTree.root = p->left;
printTrnode(&tempTree);
}
if (NULL != p->right) { //右边有路,往右走
tempTree.root = p->right;
printTrnode(&tempTree);
}
}

//递归处理节点都在递归后(返回前)处理, 或者在递归前处理, 避免重复调用
printf("%-20s\t %-10d\t %-15p\t %-15p\t %-15p\n",(p->item).name, (p->item).value, p, p->left, p->right);
return ;
}

/*
void TraverseWithParameter(const Tree *tree, void(*pfun)(Item item))
{
//创建临时树作为参数树的拷贝
Expand All @@ -185,8 +236,9 @@ void TraverseWithParameter(const Tree *tree, void(*pfun)(Item item))
TraverseWithParameter(&tempTree, pfun);
}
}
/*递归处理节点都在递归后(返回前)处理, 或者在递归前处理, 避免重复调用*/
//递归处理节点都在递归后(返回前)处理, 或者在递归前处理, 避免重复调用
(*pfun)(p->item); //调用一次参数函数
printf("p: %p, left: %p, right: %p\n", p, p->left, p->right);
return ;
}
*/
22 changes: 12 additions & 10 deletions cprogram/binaryTree/binaryTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,29 @@ void InitializeTree(Tree *tree);

//树是空的吗
bool TreeIsEmpty(const Tree *tree);
//树的两个节点都是空的吗
//树的两个子节点都是空的吗
bool TreeHasEmpty(const Tree *tree);
//树是满的吗
bool TreeIsFull(const Tree *tree);
//树的两个节点都是满的吗
//树的两个子节点都是满的吗
bool TreeHasFull(const Tree *tree);

//获得树的项数
int TrnodesCount(const Tree *tree);
//项在树里吗
//bool InTree(const Tree *tree, Item item);

//添加一个树节点
bool AddTrnode(Tree *tree, const Item *item);
bool AddTrnode(Tree *tree, Item item);
//查找一个树节点
Trnode* SearchTrnode(const Tree *tree, const Item *item);
//项在树里吗
bool InTree(const Tree *tree, const Item *item);
bool SearchTrnode(const Tree *tree, const char *name, Trnode **pnode, Trnode **plast);
//删除一个树节点
bool DeleteTrnode(Tree *tree, const Item *item);
bool DeleteTrnode(Tree *tree, Item item);
//打印节点
void printTrnode(const Tree *tree);

//删除所有树节点
void DeleteAllTrnode(Tree *tree);

//遍历树的工具, 传递一个函数, 函数会对每一项做处理
void TraverseWithParameter(const Tree *tree, void(*pfun)(Item item));
//void TraverseWithParameter(const Tree *tree, void(*pfun)(Item item));

#endif
105 changes: 87 additions & 18 deletions cprogram/binaryTree/testBinaryTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,95 @@
#include "testBinaryTree.h"
//#include "showTree.h"

static void help()
{
printf("可用选项: -1:退出 0:显示帮助 1:增加数据 2:显示数据 3:查找数据\n");
}

int main(void)
{
bool quit = false;
int option;

Tree tree;
InitializeTree(&tree);

printf("欢迎来到二叉树测试系统");
help();

do {
printf("输入你的选项:");
scanf("%d", &option);

switch (option) {
case HELP: help(); break;
case SHOW: printItems(&tree); break;
case ADD: addItems(&tree); break;
case SEARCH: searchItem(&tree); break;
case QUIT: quit = true; break;
default: printf("无效选项\n"); break;
}
} while(!quit);

printf("已退出\n");

return 0;
}
void printItems(const Tree *tree)
{
printf("\nname\t\t\t value\t\t Trnode\t\t\t left\t\t\t right\n\n");
printTrnode(tree);
printf("共%d条记录\n\n", TrnodesCount(tree));
}

void addItems(Tree *tree)
{
char name[NAME_LENGTH];
int value = 0;
Item item;

printf("增加: 输入name, value. 空格隔开, 任意字符串加-1 结束\n");

scanf("%s %d", name, &value);
while (value != -1) {

strcpy(item.name, name);
item.value = value;

AddTrnode(tree, item);

scanf("%s %d", name, &value);

}
}

void searchItem(const Tree *tree)
{
char name[NAME_LENGTH];
Trnode *pnode = NULL, *plast = NULL;

printf("查找: 输入name:");
scanf("%s", name);

if (SearchTrnode(tree, name, &pnode, &plast)) {
printf("\nname\t\t\t value\t\t left\t\t\t right\n\n");
printf("%-20s\t %-10d\t %-15p\t %-15p\n",(pnode->item).name, (pnode->item).value, pnode->left, pnode->right);
} else {
printf("无结果\n");
}
}

void readTreeFromFile()
{

}

void saveTreeToFile()
{

}

/*
int main(void)
{
Tree tree;
Expand Down Expand Up @@ -51,23 +137,6 @@ int main(void)
return 0;
}
*/

static void printItem(const Item item)
{
printf("name: %s, value: %d\n", item.name, item.value);
}

void printItems(const Tree *tree)
{
TraverseWithParameter(tree, &printItem);
}

void readTreeFromFile()
{

}

void saveTreeToFile()
{

}
4 changes: 3 additions & 1 deletion cprogram/binaryTree/testBinaryTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "binaryTree.h"

//enum options {HELP=0, ADD=1 };
enum options {QUIT=-1, HELP=0, ADD=1, SHOW=2, SEARCH=3};

//函数原型
void printItems(const Tree *tree); //遍历打印所有项
void addItems(Tree *tree);
void searchItem(const Tree *tree);

#endif

0 comments on commit b39c830

Please sign in to comment.