#include"stdio.h"
struct Btree{
int data;
struct Btree *right;
struct Btree *left;
};
//查找节点
struct Btree *findNode(struct Btree *tree,int data)
{
while(tree)
{
if(tree->data ==data)
return tree;
else if(tree->data >data)
tree=tree->left ;
else
tree=tree->right ;
}
return NULL;
};
//增加节点
struct Btree *addNode(struct Btree *tree,int data)
{
struct Btree *p;
struct Btree *q;
struct Btree *temp;
p=(struct Btree *)malloc(sizeof(struct Btree));
p->data =data;
p->left =NULL;
p->right =NULL;
if(tree==NULL)
{
tree=(struct Btree *)malloc(sizeof(struct Btree));
tree->data =data;
tree->left =NULL;
tree->right =NULL;
return tree;
}
q=tree;
while(q)
{
temp=q;
if(q->data >data)
q=q->left ;
else
q=q->right ;
}
if(temp->data >data)
temp->left =p;
else
temp->right =p;
return tree;
}
//创建二叉树
struct Btree *createBtree(struct Btree *tree,int *p,int num)
{
int i=0;
if(num<1)
{
printf("输入的二叉树的节点数不正确\n");
return NULL;
}
for(i=0;i<num;i++)
{
tree=addNode(tree,p[i]);
}
return tree;
}
//打印二叉树
void printBtree(struct Btree *p)
{
if(p)
{
printBtree(p->left );
printf("%d\n",p->data );
printBtree(p->right );
}
}
//删除节点
struct Btree *delNode(struct Btree *p,int data)
{
struct Btree *root=p;
struct Btree *next;
struct Btree *cur=(struct Btree *)malloc(sizeof(struct Btree));
struct Btree *par=(struct Btree *)malloc(sizeof(struct Btree));
int is_l=0;
int is_r=0;
cur=NULL;
par=NULL;
while(p)
{
if(p->data ==data)
{
cur=p;
break;
}
else if(p->data >data)
{
is_l=1;
is_r=0;
par=p;
p=p->left ;
}else
{
is_r=1;
is_l=0;
par=p;
p=p->right ;
}
}
if(cur==NULL)
return NULL;
if(cur->left ==NULL&&cur->right ==NULL)
{
if(cur==root)
{
cur=NULL;
}
if(is_l)
{
par->left =NULL;
}
if(is_r)
{
par->right=NULL;
}
return root;
}
//左节点为空 右节点不为空
if(cur->left ==NULL&&cur->right !=NULL)
{
if(cur==root)
root=root->right ;
else if(is_l)
par->left =cur->right ;
else if(is_r)
par->right =cur->right ;
return root;
}
//右节点为空,左节点不为空
if(cur->left !=NULL&&cur->right ==NULL)
{
if(cur==root)
root=cur->left ;
else if(is_l)
par->left =cur->left ;
else if(is_r)
par->right =cur->left ;
return root;
}
//左节点不为空,右节点不为空
if(cur->left !=NULL&&cur->right !=NULL)
{
par=cur;
next=cur->left ;
while(next->right !=NULL)
{
par=next;
next=next->right ;
}
cur->data =next->data ;
if(par->left ==next)
par->left =next->left ;
else
par->right =next->right ;
free(next);
return root;
}
}
void main()
{
struct Btree *head;
struct Btree *node;
struct Btree *is_del;
int num_del;
int data[9]={3,222,5,6,8,1,2,4,233};
head=(struct Btree *)malloc(sizeof(struct Btree));
is_del=(struct Btree *)malloc(sizeof(struct Btree));
head=NULL;
//创建二叉树
printf("创建二叉树\n");
head=createBtree(head,data,9);
printf("打印创建的二叉树\n");
printBtree(head);
//增加节点
head=addNode(head,33);
printf("打印增加节点后的二叉树\n");
printBtree(head);
//查找节点
node=(struct Btree *)malloc(sizeof(struct Btree));
node=findNode(head,1);
printf("\n\n查找节点%d\n",1);
if(node==NULL)
printf("节点不存在\n");
else
printf("%d节点存在\n",node->data );
//删除节点
num_del=3;
printf("删除节点操作\n");
is_del=delNode(head,num_del);
if(is_del!=NULL)
{
printf("删除节点%d后的二叉树\n",num_del);
printBtree(is_del);
}else
printf("删除节点%d失败\n",num_del);
getchar();
}
二叉树操作
最新推荐文章于 2022-06-16 16:26:27 发布