Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Programmers use AVL Tree concepts to balance binary trees. It is quite simple. More information can be found online. Quick wiki <a href="http://en.wikipedia.org/wiki/AVL_tree" rel="nofollow">link</a> </p> <p>Below is the sample code which does tree balance using AVL algorithm.</p> <pre><code>Node *BinarySearchTree::leftRotation(Node *root) { if(NULL == root) { return NULL; } Node *rightOfTheRoot = root-&gt;mRight; root-&gt;mRight = rightOfTheRoot-&gt;mLeft; rightOfTheRoot-&gt;mLeft = root; return rightOfTheRoot; } Node *BinarySearchTree::rightRotation(Node *root) { if(NULL == root) { return NULL; } Node *leftOfTheRoot = root-&gt;mLeft; root-&gt;mLeft = leftOfTheRoot-&gt;mRight; leftOfTheRoot-&gt;mRight = root; return leftOfTheRoot; } Node *BinarySearchTree::doAVLBalance(Node *root) { if(NULL == root) { return NULL; } else if(root-&gt;isBalanced()) { return root; } root-&gt;mLeft = doAVLBalance(root-&gt;mLeft); root-&gt;mRight = doAVLBalance(root-&gt;mRight); getDepth(root); if(root-&gt;isRightHeavy()) { if(root-&gt;mRight-&gt;isLeftHeavy()) { root-&gt;mRight = rightRotation(root-&gt;mRight); } root = leftRotation(root); } else if(root-&gt;isLeftHeavy()) { if(root-&gt;mLeft-&gt;isRightHeavy()) { root-&gt;mLeft = leftRotation(root-&gt;mLeft); } root = rightRotation(root); } return root; } </code></pre> <p>Class Definition</p> <pre><code>class BinarySearchTree { public: // .. lots of methods Node *getRoot(); int getDepth(Node *root); bool isAVLBalanced(); int calculateAndGetAVLBalanceFactor(Node *root); void doAVLBalance(); private: Node *mRoot; }; class Node { public: int mData; Node *mLeft; Node *mRight; bool mHasVisited; int mDepth; public: Node(int data) : mData(data), mLeft(NULL), mRight(NULL), mHasVisited(false), mDepth(0) { } int getData() { return mData; } void setData(int data) { mData = data; } void setRight(Node *right) { mRight = right;} void setLeft(Node *left) { mLeft = left; } Node * getRight() { return mRight; } Node * getLeft() { return mLeft; } bool hasLeft() { return (mLeft != NULL); } bool hasRight() { return (mRight != NULL); } bool isVisited() { return (mHasVisited == true); } int getAVLBalanceFactor() { int leftTreeDepth = (mLeft != NULL) ? mLeft-&gt;mDepth : -1; int rightTreeDepth = (mRight != NULL) ? mRight-&gt;mDepth : -1; return(leftTreeDepth - rightTreeDepth); } bool isRightHeavy() { return (getAVLBalanceFactor() &lt;= -2); } bool isLeftHeavy() { return (getAVLBalanceFactor() &gt;= 2); } bool isBalanced() { int balanceFactor = getAVLBalanceFactor(); return (balanceFactor &gt;= -1 &amp;&amp; balanceFactor &lt;= 1); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload