HOME

TheInfoList



OR:

In
discrete mathematics Discrete mathematics is the study of mathematical structures that can be considered "discrete" (in a way analogous to discrete variables, having a bijection with the set of natural numbers) rather than "continuous" (analogously to continuous f ...
, tree rotation is an operation on a
binary tree In computer science, a binary tree is a k-ary k = 2 tree data structure in which each node has at most two children, which are referred to as the ' and the '. A recursive definition using just set theory notions is that a (non-empty) binary t ...
that changes the structure without interfering with the order of the elements. A tree rotation moves one node up in the tree and one node down. It is used to change the shape of the tree, and in particular to decrease its height by moving smaller subtrees down and larger subtrees up, resulting in improved performance of many tree operations. There exists an inconsistency in different descriptions as to the definition of the direction of rotations. Some say that the direction of rotation reflects the direction that a node is moving upon rotation (a left child rotating into its parent's location is a right rotation) while others say that the direction of rotation reflects which subtree is rotating (a left subtree rotating into its parent's location is a left rotation, the opposite of the former). This article takes the approach of the directional movement of the rotating node.


Illustration

The right rotation operation as shown in the adjacent image is performed with ''Q'' as the root and hence is a right rotation on, or rooted at, ''Q''. This operation results in a rotation of the tree in the clockwise direction. The inverse operation is the left rotation, which results in a movement in a counter-clockwise direction (the left rotation shown above is rooted at ''P''). The key to understanding how a rotation functions is to understand its constraints. In particular the order of the leaves of the tree (when read left to right for example) cannot change (another way to think of it is that the order that the leaves would be visited in an in-order traversal must be the same after the operation as before). Another constraint is the main property of a binary search tree, namely that the right child is greater than the parent and the left child is less than the parent. Notice that the right child of a left child of the root of a sub-tree (for example node B in the diagram for the tree rooted at Q) can become the left child of the root, that itself becomes the right child of the "new" root in the rotated sub-tree, without violating either of those constraints. As you can see in the diagram, the order of the leaves doesn't change. The opposite operation also preserves the order and is the second kind of rotation. Assuming this is a
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
, as stated above, the elements must be interpreted as variables that can be compared to each other. The alphabetic characters to the left are used as placeholders for these variables. In the animation to the right, capital alphabetic characters are used as variable placeholders while lowercase Greek letters are placeholders for an entire set of variables. The circles represent individual nodes and the triangles represent subtrees. Each subtree could be empty, consist of a single node, or consist of any number of nodes.


Detailed illustration

When a subtree is rotated, the subtree side upon which it is rotated increases its height by one node while the other subtree decreases its height. This makes tree rotations useful for rebalancing a tree. Consider the terminology of Root for the parent node of the subtrees to rotate, Pivot for the node which will become the new parent node, RS for the side of rotation and OS for the opposite side of rotation. For the root Q in the diagram above, RS is C and OS is P. Using these terms, the pseudo code for the rotation is: Pivot = Root.OS Root.OS = Pivot.RS Pivot.RS = Root Root = Pivot This is a constant time operation. The programmer must also make sure that the root's parent points to the pivot after the rotation. Also, the programmer should note that this operation may result in a new root for the entire tree and take care to update pointers accordingly.


Inorder invariance

The tree rotation renders the inorder traversal of the binary tree
invariant Invariant and invariance may refer to: Computer science * Invariant (computer science), an expression whose value doesn't change during program execution ** Loop invariant, a property of a program loop that is true before (and after) each iteratio ...
. This implies the order of the elements are not affected when a rotation is performed in any part of the tree. Here are the inorder traversals of the trees shown above:
Left tree: ((A, P, B), Q, C)        Right tree: (A, P, (B, Q, C))
Computing one from the other is very simple. The following is example
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
code that performs that computation: def right_rotation(treenode): """Rotate the specified tree to the right.""" left, Q, C = treenode A, P, B = left return (A, P, (B, Q, C)) Another way of looking at it is: Right rotation of node Q:
Let P be Q's left child.
Set Q's left child to be P's right child.
 et P's right-child's parent to QSet P's right child to be Q.
 et Q's parent to P
Left rotation of node P:
Let Q be P's right child.
Set P's right child to be Q's left child.
 et Q's left-child's parent to PSet Q's left child to be P.
 et P's parent to Q
All other connections are left as-is. There are also ''double rotations'', which are combinations of left and right rotations. A ''double left'' rotation at X can be defined to be a right rotation at the right child of X followed by a left rotation at X; similarly, a ''double right'' rotation at X can be defined to be a left rotation at the left child of X followed by a right rotation at X. Tree rotations are used in a number of tree
data structures In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
such as
AVL tree In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any nod ...
s,
red–black tree In computer science, a red–black tree is a kind of self-balancing binary search tree. Each node stores an extra bit representing "color" ("red" or "black"), used to ensure that the tree remains balanced during insertions and deletions. When the ...
s, WAVL trees, splay trees, and
treap In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of in ...
s. They require only constant time because they are ''local'' transformations: they only operate on 5 nodes, and need not examine the rest of the tree.


Rotations for rebalancing

A tree can be rebalanced using rotations. After a rotation, the side of the rotation increases its height by 1 whilst the side opposite the rotation decreases its height similarly. Therefore, one can strategically apply rotations to nodes whose left child and right child differ in height by more than 1. Self-balancing binary search trees apply this operation automatically. A type of tree which uses this rebalancing technique is the
AVL tree In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any nod ...
.


Rotation distance

The
rotation distance In discrete mathematics and theoretical computer science, the rotation distance between two binary trees with the same number of nodes is the minimum number of tree rotations needed to reconfigure one tree into another. Because of a combinatorial e ...
between any two binary trees with the same number of nodes is the minimum number of rotations needed to transform one into the other. With this distance, the set of ''n''-node binary trees becomes a
metric space In mathematics, a metric space is a set together with a notion of ''distance'' between its elements, usually called points. The distance is measured by a function called a metric or distance function. Metric spaces are the most general settin ...
: the distance is symmetric, positive when given two different trees, and satisfies the
triangle inequality In mathematics, the triangle inequality states that for any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side. This statement permits the inclusion of degenerate triangles, but ...
. It is an
open problem In science and mathematics, an open problem or an open question is a known problem which can be accurately stated, and which is assumed to have an objective and verifiable solution, but which has not yet been solved (i.e., no solution for it is know ...
whether there exists a
polynomial time In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
for calculating rotation distance. Yet, Fordham's algorithm computes a distance in linear time, but only allows 2 kind of rotations : (ab)c = a(bc) and a((bc)d) = a(b(cd)). Fordham's algorithm relies on a classification of nodes into 7 types, and a lookup table is used to find the number of rotations required to transform a node of one type into an other.
Daniel Sleator Daniel Dominic Kaplan Sleator (born 10 December 1953) is a Professor of Computer Science at Carnegie Mellon University, Pittsburgh, United States. In 1999, he won the ACM Paris Kanellakis Award (jointly with Robert Tarjan) for the splay tree da ...
,
Robert Tarjan Robert Endre Tarjan (born April 30, 1948) is an American computer scientist and mathematician. He is the discoverer of several graph algorithms, including Tarjan's off-line lowest common ancestors algorithm, and co-inventor of both splay trees a ...
and
William Thurston William Paul Thurston (October 30, 1946August 21, 2012) was an American mathematician. He was a pioneer in the field of low-dimensional topology and was awarded the Fields Medal in 1982 for his contributions to the study of 3-manifolds. Thurston ...
showed that the rotation distance between any two ''n''-node trees (for ''n'' ≥ 11) is at most 2''n'' − 6, and that some pairs of trees are this far apart as soon as ''n'' is sufficiently large. Lionel Pournin showed that, in fact, such pairs exist whenever ''n'' ≥ 11..


See also

*
AVL tree In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any nod ...
,
red–black tree In computer science, a red–black tree is a kind of self-balancing binary search tree. Each node stores an extra bit representing "color" ("red" or "black"), used to ensure that the tree remains balanced during insertions and deletions. When the ...
, and splay tree, kinds of
binary search tree In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left subtree and ...
data structures that use rotations to maintain balance. *
Associativity In mathematics, the associative property is a property of some binary operations, which means that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a valid rule of replacement f ...
of a binary operation means that performing a tree rotation on it does not change the final result. * The
Day–Stout–Warren algorithm The Day–Stout–Warren (DSW) algorithm is a method for efficiently balancing binary search trees that is, decreasing their height to O(log ''n'') nodes, where ''n'' is the total number of nodes. Unlike a self-balancing binary search tree, it doe ...
balances an unbalanced BST. *
Tamari lattice In mathematics, a Tamari lattice, introduced by , is a partially ordered set in which the elements consist of different ways of grouping a sequence of objects into pairs using parentheses; for instance, for a sequence of four objects ''abcd'', the ...
, a partially ordered set in which the elements can be defined as binary trees and the ordering between elements is defined by tree rotation.


References

{{reflist


External links


The AVL Tree Rotations Tutorial
(RTF) by John Hargrove Binary trees Search trees de:Binärbaum#Rotation