定义结构体
#define Type int
typedef struct AVLNode {
Type data;
AVLNode* leftchild;
AVLNode* rightchild;
int bf;
}AVLNode;
typedef struct AVLTree {
AVLNode* root;
}AVLTree;
插入结点
void InitAVLTree(AVLTree* avl) {
avl->root = NULL;
}
bool Insert(AVLTree* avl, Type x) {
return Insert(avl->root, x);
}
bool Insert(AVLNode* &t, Type x) {
AVLNode* parent = NULL;
AVLNode* p = t;
LinkStack st;
InitStack(&st);
while (p != NULL) {
if (x == p->data)
return 0;
parent = p;
Push(&st, parent);
if (x < p->data)
p = p->leftchild;
else
p = p->rightchild;
}
p = CreateNode(x);
if (parent==NULL) {
t = p;
return 1;
}
else
{
if