Zodan & Right Tan & double spin in a balanced search tree

Source: Internet
Author: User

This article highlights:

Balanced search tree with Zodan, right Tan, left and right double rotation


When inserting a node in a balanced search tree, it is possible to break the balance of the entire tree. In order to ensure that the balance is not destroyed, some nodes should be rotated to reduce the height of the tree, so as to ensure the balance of the tree.

First, Zodan:


(The node in the above image may be null, or it may not be empty ...) Same

As you can see from the diagram, when you do left Tan, you only change the right pointer of parent and the left pointer of SUBR. The Subr Zuozi (SUBRL) is used as the right subtree of parent, and parent is used as the left subtree of SUBR. Obviously, doing so lowers the height of the tree.

Two points to be aware of when rotating:

1. To change the point of subrl->_parent, you need to determine whether the SUBRL is null and, if it is empty, you cannot dereference it.

2.parent is the root node. If parent is the root node, then only the SUBR can be assigned to the root node after the rotation is completed, but if parent is not the root node, that is, parent is the subtree of a node ppnode, it is necessary to determine whether parent is in Ppnode's left or right, so as to determine SUBR position.

void Rotateleft (node* parent)	//Zodan
	{		
		node* subr = parent->_right;
		node* SUBRL = subr->_left;

		Parent->_right = SUBRL;	First change parent's right pointer
		if (SUBRL)	//SUBRL may be null
		{
			subrl->_parent = parent;
		}

		node* Ppnode = parent->_parent;
		Subr->_left = parent;
		Parent->_parent = SUBR;

		if (Ppnode = = NULL)
		{
			_root = SUBR;
			Subr->_parent = NULL;
		}
		else
		{
			//Judge Subr should be linked to the Ppnode Zuozi or right subtree
			if (ppnode->_left = = parent)
				ppnode->_left = SUBR;
			else
				ppnode->_right = SUBR;

			Subr->_parent = Ppnode;
		}
	

Second, right Tan:


As with Zodan, the right single rotation is to assign the right subtree node of SUBL to Parent's left pointer, and let parent itself act as the right subtree of SUBL.

	void Rotateright (node* parent)	//Right Tan
	{
		node* subl = parent->_left;
		node* SUBLR = subl->_right;

		Parent->_left = SUBLR;
		if (SUBLR)
		{
			sublr->_parent = parent;
		}

		node* Ppnode = parent->_parent;
		Subl->_right = parent;
		Parent->_parent = Subl;

		if (Ppnode = = NULL)	//Description The parent node is root (
		{
			_root = subl);
			Subl->_parent = NULL;
		}
		else
		{
			//If parent is not a root node, determine whether it is on the right or left if
			(ppnode->_left = = Subl) of the previous node
				ppnode->_left =;
			else
				ppnode->_right = Subl;

			Subl->_parent = Ppnode;
		}
	

three, left and right double rotation:

After understanding the Tan, the double spins are simpler, only two-step spin.


void Rotatelr (node* parent)		//left-right double-
		rotateleft (parent->_left);
		Rotateright (parent);

	}
four, right and left double rotation:


	void Rotaterl (node* parent)		//Right left double rotation
	{
		
		rotateright (parent->_right);
		Rotateleft (parent);

	}


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.