-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.c
More file actions
111 lines (86 loc) · 1.79 KB
/
Copy pathAVLTree.c
File metadata and controls
111 lines (86 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "AVLTree.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef struct node{
struct node *lc, *rc;
Element data;
int balance;
}Node;
struct avl{
struct node *root;
int dataNum;
};
//--declaration
void rightRotate(Node *target);
void leftRotate(Node *target);
//--end
//--helper
void rightRotate(Node *target){
Node *child = target->lc;
assert(child != NULL);
target->lc = child->rc;
child->rc = target;
return;
}
void leftRotate(Node *target){
Node *child = target->rc;
assert(child != NULL);
target->rc = child->lc;
child->lc = target;
return;
}
//-end
AVLTree *initAVL(){
AVLTree *tree = (AVLTree *)malloc(sizeof(AVLTree));
tree->root = NULL;
tree->dataNum = 0;
return tree;
}
void
void insertAVL(AVLTree *tree, Element data){
assert(tree == NULL);
}
Element findAVL(AVLTree *tree, int key){
Node *now = tree->root;
while(now != NULL){
if(now->data.key == key)
return now->data;
else if(now->data.key > key)
now = now->lc;
else
now = now->rc;
}
//not find
Element temp = {.key = -1};
return temp;
}
Element deleteAVL(AVLTree *tree, int key);
void destroyHelper(Node *a){
if(a == NULL)
return;
destroyHelper(a->lc);
destroyHelper(a->rc);
free(a);
return;
}
void destroyAVL(AVLTree *tree){
destroyHelper(tree->root);
free(tree);
return;
}
void printHelper(Node *now){
if(now == NULL)
return;
//check
assert(now->balance <= 1 && now->balance >= -1);
printHelper(now->lc);
printf("%d ", now->data.key);
printHelper(now->rc);
return;
}
void printAVL(AVLTree *tree){
printf("AVL Tree\n");
printHelper(tree->root);
return;
}