"Data structure" balanced binary tree [AVL tree] (ii)--delete

Source: Internet
Author: User

The insert operation of the balanced binary tree is described earlier: the interpolation of the balanced binary tree, here to introduce the deletion of the balanced binary tree, the balanced binary tree is a two-fork search tree with equilibrium condition, and its deletion is to add the balance adjustment algorithm on the basis of binary search tree.

The deletion of binary search tree See blog post: Two fork Find tree removal (7th)

First look at the schematic ()

/* Two forks the nature of the search tree makes it easy to find the minimum maximum key value *//////* Find the Minimum key value node: Direct recursive traverse left subtree leaf node */avlnode* Avltree::findmin (Avlnode *node) {if (NULL = = node) r

	Eturn NULL;

	else if (NULL = = Node->leftchild) return node;
else return findmin (node->leftchild); }/* Non-recursive implementation find maximum key node */avlnode* Avltree::findmax (Avlnode *node) {if (node! = NULL) {while (Node->rightchild) nod
	E = node->rightchild;
} return node;
	} void Avltree::D elete (int val) {if (NULL = = Root) return;
else Delete (Root, Val);
	}//node deletion is the constant exchange of data, change the deletion node, and finally locate the leaf node//void Avltree::D elete (Avlnode *&node, int val) {Avlnode *tempnode = NULL;

	if (NULL = = node) return;
	else if (Val < node->data) Delete (Node->leftchild, Val);

	else if (val > Node->data) Delete (Node->rightchild, Val);
		Find the Val else if (node->leftchild && node->rightchild) {Tempnode = Findmin (node->rightchild);
		Node->data = tempnode->data;   Delete (Node->rightchild, node->data); Understand.  Delete node key value transform}                                         The key value node to be removed thereafter is not Val else {if (node->leftchild && (NULL = = Node->r
			Ightchild)) {Tempnode = Findmax (node->leftchild);
			Node->data = tempnode->data;
		Delete (Node->leftchild, node->data);
			} else if (Node->rightchild && (NULL = = Node->leftchild)) {Tempnode = Findmin (node->rightchild);
			Node->data = tempnode->data;
		Delete (Node->rightchild, node->data);
			} else {Delete (node);
		node = NULL; }} if (node)//must add this condition, using the power of recursion, adjust the balance {//Balance to determine if (2 = = height (node->leftchild)-Height (Node->rightchil d) {//condition to determine if (Height (node->leftchild->leftchild) >= height (node->leftchild->rightchild)) Rot
			Ationleftonce (node);
				else {rotationrightonce (node->leftchild);
			Rotationleftonce (node); }} if (2 = = height (node->rightchild)-height (node->leftchild)) {if (Height (node->rightchild->righ TcHild) >= Height (node->rightchild->leftchild)) rotationrightonce (node);
				else {rotationleftonce (node->rightchild);
			Rotationrightonce (node); }
		}
	}
}

The above removal is specific to the deletion of the two-fork lookup tree, in addition to the lookup of the balanced binary tree, with the same traversal as the two-fork lookup tree.

second, empty the binary tree (destructor)

void Avltree::makeempty (Avlnode *&node)
{
	if (node)
	{
		makeempty (node->leftchild);
		Makeempty (node->rightchild);
		Delete node;
	node = NULL;
}
three, balanced binary tree time complexity analysis

The Balanced binary tree adds a rotation algorithm on the basis of the binary search tree, but the rotation operation only changes the direction of a few pointers, consumes constant time, and the balanced binary tree joins the balance mechanism, so its depth is Logn, and the lookup, insertion, and deletion are O (logn) on average and worst case. Compared to binary search tree, time stabilized a lot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.