/* tree.c -- tree support functions */
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
/* local data type */
typedef struct pair {
Node * parent;
Node * child;
} Pair;
/* protototypes for local functions */
static Node * MakeNode(const Item * pi);
static bool ToLeft(const Item * i1, const Item * i2);
static bool ToRight(const Item * i1, const Item * i2);
static void AddNode (Node * new_node, Node * root);
static void InOrder(const Node * root, void (* pfun)(Item item));
static Pair SeekItem(const Item * pi, const Tree * ptree);
static void DeleteNode(Node **ptr);
static void DeleteAllNodes(Node * ptr);
/* function definitions */
void InitializeTree(Tree * ptree)
{
ptree->root = NULL;
ptree->size = 0;
}
bool TreeIsEmpty(const Tree * ptree)
{
if (ptree->root == NULL)
return true;
else
return false;
}
bool TreeIsFull(const Tree * ptree)
{
if (ptree->size == MAXITEMS)
return true;
else
return false;
}
int TreeItemCount(const Tree * ptree)
{
return ptree->size;
}
bool AddItem(const Item * pi, Tree * ptree)
{
Node * new_node;
if (TreeIsFull(ptree))
{
fprintf(stderr,"Tree is full\n");
return false; /* early return */
}
if (SeekItem(pi, ptree).child != NULL)
{
fprintf(stderr, "Attempted to add duplicate item\n");
return false; /* early return */
}
new_node = MakeNode(pi); /* points to new node */
if (new_node == NULL)
{
fprintf(stderr, "Couldn't create node\n");
return false; /* early return */
}
/* succeeded in creating a new node */
ptree->size++;
if (ptree->root == NULL) /* case 1: tree is empty */
ptree->root = new_node; /* new node is tree root */
else /* case 2: not empty */
AddNode(new_node,ptree->root); /* add node to tree */
return true; /* successful return */
}
bool InTree(const Item * pi, const Tree * ptree)
{
return (SeekItem(pi, ptree).child == NULL) ? false : true;
}
bool DeleteItem(const Item * pi, Tree * ptree)
{
Pair look;
look = SeekItem(pi, ptree);
if (look.child == NULL)
return false;
if (look.parent == NULL) /* delete root item */
DeleteNode(&ptree->root);
else if (look.parent->left == look.child)
DeleteNode(&look.parent->left);
else
DeleteNode(&look.parent->right);
ptree->size--;
return true;
}
void Traverse (const Tree * ptree, void (* pfun)(Item item))
{
if (ptree != NULL)
InOrder(ptree->root, pfun);
}
void DeleteAll(Tree * ptree)
{
if (ptree != NULL)
DeleteAllNodes(ptree->root);
ptree->root = NULL;
ptree->size = 0;
}
/* local functions */
static void InOrder(const Node * root, void (* pfun)(Item item))
{
if (root != NULL)
{
InOrder(root->left, pfun);
(*pfun)(root->item);
InOrder(root->right, pfun);
}
}
static void DeleteAllNodes(Node * root)
{
Node * pright;
if (root != NULL)
{
pright = root->right;
DeleteAllNodes(root->left);
free(root);
DeleteAllNodes(pright);
}
}
static void AddNode (Node * new_node, Node * root)
{
if (ToLeft(&new_node->item, &root->item))
{
if (root->left == NULL) /* empty subtree */
root->left = new_node; /* so add node here */
else
AddNode(new_node, root->left);/* else process subtree*/
}
else if (ToRight(&new_node->item, &root->item))
{
if (root->right == NULL)
root->right = new_node;
else
AddNode(new_node, root->right);
}
else /* should be no duplicates */
{
fprintf(stderr,"location error in AddNode()\n");
exit(1);
}
}
static bool ToLeft(const Item * i1, const Item * i2)
{
int comp1;
if ((comp1 = strcmp(i1->petname, i2->petname)) < 0)
return true;
else if (comp1 == 0 &&
strcmp(i1->petkind, i2->petkind) < 0 )
return true;
else
return false;
}
static bool ToRight(const Item * i1, const Item * i2)
{
int comp1;
if ((comp1 = strcmp(i1->petname, i2->petname)) > 0)
return true;
else if (comp1 == 0 &&
strcmp(i1->petkind, i2->petkind) > 0 )
return true;
else
return false;
}
static Node * MakeNode(const Item * pi)
{
Node * new_node;
new_node = (Node *) malloc(sizeof(Node));
if (new_node != NULL)
{
new_node->item = *pi;
new_node->left = NULL;
new_node->right = NULL;
}
return new_node;
}
static Pair SeekItem(const Item * pi, const Tree * ptree)
{
Pair look;
look.parent = NULL;
look.child = ptree->root;
if (look.child == NULL)
return look; /* early return */
while (look.child != NULL)
{
if (ToLeft(pi, &(look.child->item)))
{
look.parent = look.child;
look.child = look.child->left;
}
else if (ToRight(pi, &(look.child->item)))
{
look.parent = look.child;
look.child = look.child->right;
}
else /* must be same if not to left or right */
break; /* look.child is address of node with item */
}
return look; /* successful return */
}
static void DeleteNode(Node **ptr)
/* ptr is address of parent member pointing to target node */
{
Node * temp;
puts((*ptr)->item.petname);
if ( (*ptr)->left == NULL)
{
temp = *ptr;
*ptr = (*ptr)->right;
free(temp);
}
else if ( (*ptr)->right == NULL)
{
temp = *ptr;
*ptr = (*ptr)->left;
free(temp);
}
else /* deleted node has two children */
{
/* find where to reattach right subtree */
for (temp = (*ptr)->left; temp->right != NULL;
temp = temp->right)
continue;
temp->right = (*ptr)->right;
temp = *ptr;
*ptr =(*ptr)->left;
free(temp);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
培训用的C++课件并附上源代码
共449个文件
c:233个
cpp:165个
ppt:21个
5星 · 超过95%的资源 需积分: 10 1.3k 下载量 71 浏览量
2008-04-17
22:08:30
上传
评论 1
收藏 6.25MB RAR 举报
温馨提示
参加培训时用的课件,并带有源代码
资源推荐
资源详情
资源评论
收起资源包目录
培训用的C++课件并附上源代码 (449个子文件)
tree.c 6KB
dualview.c 4KB
petclub.c 4KB
mall.c 3KB
pe15-7.c 3KB
list.c 3KB
func_ptr.c 3KB
checking.c 3KB
booksave.c 2KB
append.c 2KB
fields.c 2KB
queue.c 2KB
running.c 2KB
menuette.c 2KB
films2.c 2KB
strings.c 2KB
animals.c 2KB
ptr_ops.c 2KB
enum.c 2KB
rain.c 2KB
sort_str.c 2KB
films3.c 2KB
names3.c 1KB
invert4.c 1KB
randbin.c 1KB
wordcnt.c 1KB
manybook.c 1KB
use_q.c 1KB
reducto.c 1KB
friend.c 1KB
array2d.c 1KB
qsorter.c 1KB
electric.c 1KB
vararr2d.c 1KB
reverse.c 1KB
hotel.c 1KB
flexmemb.c 1KB
lethead2.c 1KB
divisors.c 1KB
factor.c 1KB
mems.c 1KB
friends.c 1KB
vowels.c 1KB
manydice.c 1KB
binbit.c 1010B
skippart.c 999B
book.c 991B
films1.c 988B
rect_pol.c 954B
showchar2.c 948B
flc.c 948B
funds4.c 939B
names2.c 928B
wheat.c 922B
dyn_arr.c 921B
complit.c 912B
names_str.c 899B
names_st.c 889B
names1.c 882B
power.c 857B
showchar1.c 852B
varargs.c 823B
scores_in.c 823B
arf.c 820B
addaword.c 797B
talkback.c 797B
usehotel.c 783B
byebye.c 779B
diceroll.c 770B
min_sec.c 764B
rhodium.c 761B
loccheck.c 730B
copy3.c 717B
count.c 712B
mod_str.c 704B
colddays.c 691B
parta.c 691B
zippo1.c 685B
zippo2.c 684B
convert.c 680B
sum_arr1.c 674B
cypher1.c 654B
file_eof.c 651B
put_put.c 649B
mac_arg.c 648B
strcnvt.c 641B
defines.c 638B
compback.c 636B
sum_arr2.c 635B
copy1.c 629B
binary.c 623B
pound.c 620B
join_chk.c 617B
funds2.c 613B
funds1.c 611B
shoes2.c 603B
funds3.c 593B
lesser.c 591B
partb.c 586B
starsrch.c 583B
共 449 条
- 1
- 2
- 3
- 4
- 5
hzl26634434
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页