AvlTree Insert(ElementType e, AvlTree t) { if(t == NULL){ t = malloc(sizeof(struct AvlNode)); t->e = e; t->height = 0; t->left = NULL; t->right = NULL; } if(e < t->e){ t->left = Insert(e, t->left); if(Height(t->left) - Height(t->right) == 2){ if(e < t->left->e){ t = SingleRotateWithLeft(t); }else{ t = DoubleRotateWithLeft(t); } } }else if(e > t->e){ t->right = Insert(e, t->right); if(Height(t->right) - Height(t->left) == 2){ if(e > t->right->e){ t = SingleRotateWithRight(t); }else{ t = DoubleRotateWithRight(t); } } } t->height = Max(Height(t->left), Height(t->right)) + 1; return t; }
AvlTree Insert(ElementType X,AvlTree T){ //T为空树 if(T == NULL){ T = malloc(sizeof(AvlTree)); if(T == NULL){ fatalError("out of space"); } T -> Element = X; T -> Left = NULL; T -> Right = NULL; } //此处遇到问题 应该如何分情况讨论呢 我需求的是X插入位置的节点情况 但是此时只有根节点T //所以插入应该递归地分左右插入 并且在插入后计算高度差值 差值2位中断情况 if(X < T-> Element){ //再次遇到问题 用什么变量存left 要不要return //答案是直接递归修改left的值 最终改变的是叶节点指针 而非根节点 T -> Left = Insert(X , T -> Left); if(Height(T->Left) - Height(T->Right) == 2){ //分情况 if(X < T->Left->Element){ T = SingleRotateWithLeft(T); }else{ T = DoubleRotateWithLeft(T); } } }else{ } return T; }
AvlTree Insert(int x, AvlTree root){ if(root==NULL){ root = (AvlNode *)malloc(sizeof(AvlNode)); if(root == NULL){ printf("Out of memory"); }else{ root->val = x; root->right = root->left = NULL; } } else if(x < root->val){ root->left = Insert(x, root->left); if(Height(root->left)-Height(root->right) == 2){ if(x<root->left->val) root = SingleRotateWithLeft(root); else root = DoubleRotateWithLeft(root); } } else if(x > root->val){ root->right = Insert(x, root->right); if(Height(root->right)-Height(root->left) == 2){ if(x>root->right->val) root = SingleRotateWithRight(root); else root = DoubleRotateWithRight(root); } } root->height = Max(Height(root->left), Height(root->right)) + 1; return root; }
AvlTree Insert(ElementType X, AvlTree T) { if (T == NULL) { T = malloc(sizeof(struct AvlNode)); if (T == NULL) { Error("No additional memory!!"); exit(1); } else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } else if (X < T->Element){ T->Left = Insert(X, T->Left); if (Height(T->Left) - Height(T->Right) == 2) { if(X < T->Left->Element) { T = SingleRotateWithLeft(T); } else { T = DoubleRotateWithLeft(T); } } } else if (X > T->Element) { T->Right = Insert(X, T->Right); // printf("%d%s%d\n", Height(T->Right) - Height(T->Left), "Right", X); if (Height(T->Right) - Height(T->Left) == 2) { // printf("%d|%d\n", X, T->Right->Element); if (X > T->Right->Element) { T = SingleRotateWithRight(T); } else { T = DoubleRotateWithRight(T); } } } T->Height = Max(Height(T->Left), Height(T->Right)) + 1; return T; }
struct AvlTreeNode *CAvlTree::InsertNode(const int key , struct AvlTreeNode *node) { if (node == NULL) { node = new struct AvlTreeNode; if (node == NULL) { std::cerr << "out of memory" << std::endl; } else { node->key = key; node->height = 0; node->left = node->right = NULL; } } else if (key < node->key) { node->left = this->InsertNode(key, node->left); if (this->Height(node->left) - this->Height(node->right) >= 2) { if (key < node->left->key) { node = SingleRotateWithLeft(node); } else { node = DoubleRotateWithLeft(node); } } } else if (key > node->key) { node->right = this->InsertNode(key, node->right); if (this->Height(node->right) - this->Height(node->left) >= 2) { if (key > node->right->key) node = SingleRotateWithRight(node); else node = DoubleRotateWithRight(node); } } /// \note else key is in the tree already, we will do nothing. node->height = std::max(this->Height(node->left) , this->Height(node->right)) + 1; return node; }
void insert_tree(int data,AVLTree &node) { if (node == NULL) { node = (AVL*)malloc(sizeof(AVL)); node->data = data; node->height = 0; node->lchild = node->rchild = NULL; printf("test01\n"); return; } else if (data < node->data) { insert_tree(data, node->lchild); } else if (data > node->data) { insert_tree(data, node->rchild); } node->height = Max(Height(node->lchild), Height(node->rchild)) + 1; if (Height(node->lchild) - Height(node->rchild) == 2) { if (node->lchild->data > data) { SingleRotateWithLeft(node); } else { DoubleRotateWithLeft(node); } } else if (Height(node->rchild) - Height(node->lchild) == 2) { if (node->rchild->data < data) { SingleRotateWithRight(node); } else { DoubleRotateWithRight(node); } } }
AvlTree Insert( ElementType X, AvlTree T ) { if( T == NULL ) { /* Create and return a one-node tree */ T = malloc( sizeof( struct AvlNode ) ); T->Element = malloc(sizeof(int)*RULE_SIZE); if( T == NULL ) FatalError( "Out of space!!!" ); else { memcpy(T->Element,X,RULE_SIZE*sizeof(int)); T->Height = 0; T->Left = T->Right = NULL; } } else if(compareLeaf(X, T->Element)==-1 ) { T->Left = Insert( X, T->Left ); if( Height( T->Left ) - Height( T->Right ) == 2 ) if( compareLeaf(X,T->Left->Element)==-1 ) T = SingleRotateWithLeft( T ); else T = DoubleRotateWithLeft( T ); } else if( compareLeaf(X,T->Element)==1 ) { T->Right = Insert( X, T->Right ); if( Height( T->Right ) - Height( T->Left ) == 2 ) if( compareLeaf(X,T->Right->Element)==1 ) T = SingleRotateWithRight( T ); else T = DoubleRotateWithRight( T ); } /* Else X is in the tree already; we'll do nothing */ T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1; return T; }
//先把书上的avl插入函数手打一遍 AvlTree Insert(ElementType X, AvlTree T) { if(T == NULL) { T = (AvlTree*)malloc(sizeof(struct AvlTree)); if(T == NULL) printf("Out of space!\n"); else { T->Height = 0; T->Right = NULL; T->Left = NULL; T->Element = X; } } else if(X < T->Element) { T->Left = Insert(X, T->Left); if(Height(T->Left) - Height(T->Right) == 2) if(X < T->Left->Element) SingleRotateWithLeft(T->Left); else DoubleRotateWithLeft(T->Left); } else if(X > T->Element) { T->Right = Insert(X, T->Right); if(Height(T->Right) - Height(T->Left) == 2) if(X > T->Right->Element) SingleRotateWithRight(T->Right); else DoubleRotateWithRight(T->Right); } T->Height = Max(Height(T->Left), Height(T->Right)) + 1; return T; }
AvlTree Insert(ElementType X, AvlTree T) { if(T == NULL) { T = malloc(sizeof(struct AvlNode)); if(T == NULL) FatalError("Out of space!!!"); else { T->Element = X; T->Height = 0; T->Left = T->Right = NULL; } } else if(X < T->Element) { T->Left = Insert(X, T->Left); if(Height(T->Left) - Height(T->Right) == 2) if(X < T->Left->Element) T = SingleRotateWithLeft(T); else T = DoubleRotateWithLeft(T); } else if(X > T->Element) { T->Right = Insert(X, T->Right); if(Height(T->Right) - Height(T->Left) == 2) if(X > T->Right->Element) T = SingleRotateWithRight(T); else T = DoubleRotateWithRight(T); } T->Height = Max(Height(T->Left), Height(T->Right)) + 1; return T; }
void delete_node(AVLTree& root, int data) { if (root == NULL) { printf("NULL to delete\n"); return; } if (root->data == data) { AVLTree node = root; printf("begin to delete\n"); if (root->lchild == NULL && root->rchild == NULL) { root = NULL; } else if(root->rchild != NULL && root->lchild != NULL) { AVLTree loop = root->rchild; while (loop->lchild != NULL) { loop = loop->lchild; } root->data = loop->data; delete_node(root->rchild, loop->data); } else { if (root->lchild == NULL) { root = root->rchild; } else { root = root->lchild; } } free(node); } else if (root->data < data) { delete_node(root->rchild, data); } else { delete_node(root->lchild, data); } if (root == NULL) { return; } root->height = Max(Height(root->lchild), Height(root->rchild)) + 1; if (Height(root->lchild) - Height(root->rchild) == 2) { if (Height(root->lchild->lchild) > Height(root->lchild->rchild)) { SingleRotateWithLeft(root); } else { DoubleRotateWithLeft(root); } } if (Height(root->rchild) - Height(root->lchild) == 2) { if (Height(root->rchild->rchild) > Height(root->rchild->lchild)) { SingleRotateWithRight(root); } else { DoubleRotateWithRight(root); } } }