Tango Tree is an online algorithm. It is a type of binary search tree. It is better than the offline weight balanced binary search tree as it achieves a competitive ratio of as compared to competitive ratio of of offline weight balanced binary search tree. It takes only of additional bits of memory at every node of the tree to increase the performance over weight balanced binary search tree.

Tango tree is conceptually a tree of trees as it partitions a binary search tree into ** preferred paths** and

**and these**

*non preferred paths**preferred paths*are themselves stored in the form of auxiliary trees.

**Preferred child and Preferred Paths**

Preferred child of any node of Tango tree is defined as the most recently touched child by a normal binary search tree lookup technique. Elaborating more on preferred child let us assume a subtree T rooted at node n with right child as p and left child as q. Call p as the preferred child of n if most recently accessed node of subtree rooted at n is present in the subtree rooted at p.

A **Preferred path** is defined as the path starting from root node and following all the preferred children along a path to reach a leaf node.

**Auxiliary Trees for Preferred Paths**

A preferred path is represented by storing the nodes of the preferred path in a balanced binary search tree specifically red black tree. Then, for every non-leaf node of path P there is a non-preferred child . This non preferred child acts as the root of the new auxiliary tree and then attach this new auxiliary tree rooted at to its parent and in this way, it is easy to link the auxiliary trees together. There is also option to store some extra information in each node of the auxiliary tree as per our own needs like minimum depth of the nodes in the subtree below it, etc.

**Operations on Tango Tree**

The two most common operations on Tango Trees are *Searching* and *Updating*

**Updating in a Tango Tree**It needs to maintain the structure of the Tango Tree because of constant updates as the preferred child of the nodes changes after every search. Whenever the preferred changes the preferred path is divided into two parts at the particular node that has changed and the top part of the preferred path needs to be attached to the end of another preferred path. For this purpose, divide the update into two parts:

**Cut process**In this operation, divide the preferred path into two paths at the a given node – top part and bottom part. Top part is the auxiliary tree containing the nodes above a certain node’ level and the Bottom part becomes an auxiliary tree containing node below the given node’s level.

**Join process**In this operation we combine the two auxiliary trees together. But they can only be combined if the bottom node of one of the combining trees is the parent of the top node of the other combining tree. This join operation uses the concatenation operation of

*red black trees*. There exists two nodes in the top path such that a node is said to be in the bottom path if the its key value exists between these two nodes. To concatenate, we simply split the top path between these two nodes and then join the two resulting auxiliary trees to the bottom path and our resulting auxiliary tree is ready.

**Searching in a tango tree**To search in a Tango tree we simply carry out the search in the conceptual binary search tree. First search the preferred path by searching its auxiliary tree. If the node cannot be found in the given auxiliary tree then, search the auxiliary tree of the next path and so on until search of the entire tree or could not find the desired node.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

**DSA Self Paced Course**at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer**Complete Interview Preparation Course****.**In case you wish to attend live classes with industry experts, please refer

**Geeks Classes Live**and**Geeks Classes Live USA**