#define MAX_TREE_NUMBER 200
typedef struct item
{
char petname[20];
char petkind[20];
}Item;
typedef struct node
{
Item item;
struct node *left;
struct node *right;
}Node;
typedef struct tree
{
Node *root;
int size;
}Tree;
typedef struct pair
{
Node *parent;
Node *child;
}Pair;
void initializeTree(Tree *ptree)
{
ptree->root = NULL;
ptree->size = 0;
}
bool TreeIsEmpey(const Tree *ptree)
{
return ptree->size == 0;
}
bool TreeIsFull(const Tree *ptree)
{
return ptree->size == MAX_TREE_NUMBER;
}
int TreeItemCount(const Tree *ptree)
{
return ptree->size;
}
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 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 Pair SeekItem(const Item *pi, const Tree *ptree)
{
Pair look;
look.parent = NULL;
look.child = ptree->root;
if(look.child == NULL)
return look;
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
break;
}
return look;
}
static void AddNode(Node *new_node, Node *root)
{
if(ToLeft(&new_node->item, &root->item))
{
if(root->left == NULL)
root->left = new_node;
else
AddNode(new_node, root->left);
}
else if(ToRight(&new_node->item, &root->item))
{
if(root->right == NULL)
root->right = new_node;
else
AddNode(new_node, root->right);
}
else
{
fprintf(stderr, "location error in AddNode()\n");
exit(1);
}
}
bool AddItem(const Item *pi, Tree *ptree)
{
Node *new_node;
if(TreeIsFull(ptree))
{
fprintf(stderr, "Tree is full\n");
return false;
}
if(SeekItem(pi, ptree).child != NULL)
{
fprintf(stderr, "Attempted to add duolucate item\n");
return false;
}
new_node = MakeNode(pi);
if(new_node == NULL)
{
fprintf(stderr, "Cound't create node\n");
return false;
}
ptree->size++;
if(ptree->root == NULL)
ptree->root = new_node;
else
AddNode(new_node, ptree->root);
return true;
}
bool InTree(const Tree *ptree, const Item *pi)
{
return (SeekItem(pi, ptree).child == NULL) ? true : false;
}
static void DeleteNode(Node **ptr)
{
Node *temp;
if((*ptr)->left == NULL)
{
temp = *ptr;
*ptr = (*ptr)->right;
free(temp);
}
else if((*ptr)->right == NULL)
{
temp = *ptr;
*ptr = (*ptr)->left;
free(temp);
}
else
{
for(temp = (*ptr)->left; temp->right != NULL;temp = temp->right)
continue;
temp->right = (*ptr)->right;
temp = *ptr;
*ptr = (*ptr)->left;
free(temp);
}
}
bool DeleteItem(const Item *pi, Tree *ptree)
{
Pair look;
look = SeekItem(pi, ptree);
if(look.child == NULL)
return false;
if(look.parent == NULL)
DeleteNode(&ptree->root);
else if(look.parent->left == look.child)
DeleteNode(&look.parent->left);
else
DeleteNode(&look.parent->right);
ptree->size--;
return true;
}
static void InOrder(const Node *root, void (*pfun)(Item item))
{
if(root != NULL)
{
InOrder(root->left, pfun);
(*pfun)(root->item);
InOrder(root->right, pfun);
}
}
void Traverse(const Tree *ptree, void (*pfun)(Item item))
{
if(ptree != NULL)
InOrder(ptree->root, pfun);
}
static void DeleteAllNodes(Node *root)
{
Node *pright;
if(root != NULL)
{
pright = root->right;
DeleteAllNodes(root->left);
free(root);
DeleteAllNodes(pright);
}
}
void DeleteAll(Tree *ptree)
{
if(ptree != NULL)
DeleteAllNodes(ptree->root);
ptree->root = NULL;
ptree->size = 0;
}
二叉搜索树
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- BST树即二叉搜索树:1.所有非叶子结点至多拥有两个儿子(Left和Right);2.所有结点存储一个关键字;3....
- 树 概念它是由n(n>0)个有限节点组成一个具有层次关系的集合。 特点 每个节点有零个或多个子节点; 没有父节点的...
- -二叉搜索树 查找问题:静态查找和动态查找,静态查找可以用二分查找-判定树,那么针对动态查找数据如何组织?(树的动...
- 中序遍历,但是记住要保存前一个节点的指针,因此要用到指针的指针作为参数。同时,因为最后要返回头指针,不要把返回头指...