纯C语言实现的AVL树(插入&删除&前序遍历输出)
2017-11-06 22:07
357 查看
突发奇想的敲了一遍,主要是想加深一下理解,毕竟AVL还是很重要的。
敲完了就想着发上来方便以后考前看看……..
顺便推荐一下这位大神的文章http://www.cnblogs.com/skywang12345/p/3576969.html
敲完了就想着发上来方便以后考前看看……..
顺便推荐一下这位大神的文章http://www.cnblogs.com/skywang12345/p/3576969.html
#include <stdio.h> #include <stdlib.h> #define MAX(a,b) ((a)>(b)?(a):(b)) //比较大小 #define Height(p) ((p==NULL)?0:(((Tree)(p))->height)) // 求高度 typedef struct Node* Tree; struct Node { int Key; int height; Tree Left; Tree Right; }; Tree nodeCreate(int Key) //创建节点 { Tree p = (Tree)malloc(sizeof(struct Node)); if(p == NULL)return p; p->height = 0; p->Key = Key; p->Left = NULL; p->Right = NULL; return p; } Tree lL(Tree k2) { Tree k1 = k2->Left; k2->Left = k1->Right; k1->Right = k2; k2->height = MAX(Height(k2->Left) , Height(k2->Right))+1; k1->height = MAX(Height(k1->Left) , Height(k1->Right))+1; return k1; } Tree rR(Tree k1) { Tree k2 = k1->Right; k1->Right = k2->Left; k2< 4000 span class="hljs-subst">->Left = k1; k1->height = MAX(Height(k1->Left) , Height(k1->Right))+1; k2->height = MAX(Height(k2->Left) , Height(k2->Right))+1; return k2; } Tree lR(Tree k3) { k3->Left = rR(k3->Left); return lL(k3); } Tree rL(Tree k3) { k3->Right = lL(k3->Right); return rR(k3); } Tree nodeInsert(Tree root,int data) //插入 { if(root == NULL) { root = nodeCreate(data); if(root == NULL) { printf("ERROR of the memory!!!\n"); return NULL; } } else if(root->Key == data) { printf("ERROR of the data!!!\n"); } else if(root->Key > data) { root->Left = nodeInsert(root->Left,data); if(Height(root->Left) - Height(root->Right) == 2) { if(data < root->Left->Key) { root = lL(root); } else if(data >= root->Left->Key) { root = lR(root); } } } else if(root->Key < data) { root->Right = nodeInsert(root->Right,data); if(Height(root->Right) - Height(root->Left) == 2) { if(data > root->Right->Key) { root = rR(root); } else if(data <= root->Right->Key) { root = rL(root); } } } root->height = MAX( Height(root->Left) , Height(root->Right) )+1; return root; } Tree nodeDelete(Tree root,int data) //删除 { if(root == NULL) { printf("Don`t have this data!!!\n"); return NULL; } if(data < root->Key) { root->Left = nodeDelete(root->Left,data); if(Height(root->Right) - Height(root->Left) == 2) { if(Height(root->Right->Left) > Height(root->Right->Right)) { root = rL(root); } else root = rR(root); } } else if(data > root->Key) { root->Right = nodeDelete(root->Right,data); if(Height(root->Left) - Height(root->Right) == 2) { if(Height(root->Left->Left) > Height(root->Left->Right)) { root = lL(root); } else root = lR(root); } } else if(data == root->Key) { if(root->Left && root->Right) { if(Height(root->Left) > Height(root->Right)) { Tree max = root->Left; Tree tr = root; int flag = 1; while(max->Right) { if(flag) { flag = 0; tr = tr->Left; } else tr = tr->Right; max = max->Right; } root->Key = max->Key; if(flag)tr->Left = NULL; else tr->Right = NULL; free(max); } else { Tree max = root->Right; Tree tr = root; int flag = 1; while(max->Left) { if(flag) { flag = 0; tr = tr->Right; } else tr = tr->Left; max = max->Left; } root->Key = max->Key; if(flag)tr->Right = NULL; else tr->Left = NULL; free(max); } } else { Tree t = root; root = (root->Left ? root->Left : root->Right); free(t); } } return root; } void preorderPrintf(Tree tree) // 前序遍历 { if(tree != NULL) { printf("%d ", tree->Key); preorderPrintf(tree->Left); preorderPrintf(tree->Right); } } int main() { Tree root = NULL; int N; scanf("%d",&N); while(N--) { int mid; scanf("%d",&mid); root = nodeInsert(root,mid); } int M; scanf("%d",&M); while(M--) { int mid; scanf("%d",&mid); root = nodeDelete(root,mid); } preorderPrintf(root); return 0; }
相关文章推荐
- c语言:顺序表的实现(一) 创建,插入,删除,查找,输出等基本操作实现
- C语言实现双向链表删除节点、插入节点、双向输出等操作
- C语言实现双向链表删除节点、插入节点、双向输出等操作
- [原]C语言实现双向链表删除节点、插入节点、双向输出等操作
- C语言实现动态链表的创建、删除、插入、输出(CentOS6.5上调试成功)
- C语言实现双向链表删除、插入、双向输出
- C语言实现双向链表删除节点、插入节点、双向输出等操作
- AVL树的旋转、插入、删除及遍历C语言实现
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 链表的建立,输出,删除,插入运算的c语言实现
- objecttive-C语言使用二叉排序树实现查找、插入、删除、查看元素
- AVL树的插入删除查找算法实现和分析-1(平衡因子法)
- 数据结构&&AVL树原理、插入操作详解及实现
- 常见算法:C语言中链表的操作(创建,插入,删除,输出)
- C语言实现链表的插入、删除、查询操作
- C语言实现二叉搜索树的创建、插入、删除和查找
- 经典算法:红黑树的C语言实现 ( 插入 、删除 )
- c语言实现二叉树的插入、查找、删除、打印树
- 单链表插入和删除结点c语言的实现