# Demo entry 6686586

c

Submitted by anonymous on Dec 24, 2017 at 08:41
Language: C. Code size: 7.2 kB.

```#include <stdio.h>
#include <stdlib.h>
typedef struct AVL_Node
{
int data; //数据
int height;//节点高度
}Node;
typedef Node* PNode;
int R(PNode *node);
int L(PNode *node );
void LL(PNode *node);
void RR(PNode *node);
int Height(PNode *node);
int Insert(PNode *node,int x);
int search(PNode node,int x);
void Delete(PNode *node,int x);

int R(PNode *node)
{
{
// printf("*\n");
RR(node);
}
else RR(node);
return 0;
}
int L(PNode *node )
{
{
// printf("*\n");
// printf("*\n");
LL(node);
}
else LL(node);
return 0;
}
void LL(PNode *node)
{
*node = temp;
}
void RR(PNode *node)
{

//    printf("!!!\n");

*node = temp;

}
int Height(PNode* node)
{
//    printf("***\n");
return (*node)== NULL ? -1 : (*node)->height;
}
int Insert(PNode *node,int x)
{
if((*node)==NULL)//节点为空插入这里
{
(*node) = (PNode)malloc(100*sizeof(Node));
(*node)->data = x;
(*node)->height = 0;
return 1;
}
else if(x == (*node)->data)//原来存在该节点返回0，递归结束
return 0;
else if(x<(*node)->data)//待插入节点的值小于当前的节点
{
return 0;  //返回0
{
//                printf("#1\n");
L(node); //左边高，再判断是LL型旋转还是LR型旋转
}
//        else printf("PP\n");
}
else if(x>(*node)->data)
{
return 0;
{
R(node);
//             printf("#2\n");
}
}
//    printf("*\n");
return 1;//返回1，保证递归继续向下走
}

int search(PNode node,int x)//查找
{
//PNode temp;
while(node!=NULL)
{
//  temp = node;
if(node->data == x)
{
return 1;
}
else if(x>node->data)
{
}
else if(x<node->data)
{
}
}
return 0;
}
void Inoreder(PNode tree)//中序遍历,打印树
{
if(tree == NULL)
return ;
else
{
printf("%d  ",tree->data);
}
}
void Delete(PNode *node,int x)//删除节点
{
if((*node)==NULL)//节点为空，不存在删除节点
return ;
else if(x>(*node)->data)
else if(x<(*node)->data)
else if(x == (*node)->data)
{
{
PNode temp = (*node);
free(temp);
}
{
PNode temp = (*node);
free(temp);
}
else
{
if(temp==NULL)
{
temp = (*node);
free(temp);
}
else
{
(*node)->data = temp->data;
}
}
}
if(*node)
{
}
}
int main()
{
int c,d;
PNode root = NULL;
"0：打印树\n"
"1: 插入结点\n"
"2: 查找节点\n"
"3: 删除节点\n"
"4: 退出\n"
);
while(1){
c = 9;
scanf("%d",&c);
switch(c)
{
case 0:Inoreder(root);
printf("\n");
break;
case 1:
printf("Input the data\n");
scanf("%d",&d);
getchar();
Insert(&root,d);
d = 0;
break;
case 2:
printf("Input you search\n");
scanf("%d",&d);
getchar();
search(root,d);
d = 0;
break;
case 3:
printf("Input you delete\n");
scanf("%d",&d);
getchar();
Delete(&root,d);
d = 0;
break;
case 4: exit(0);break;
default:printf("error!!!\n");

}
}
}
```

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.