title: I would like not to use root as global
body: <p>Hello I am writing an avl tree and I have one issue . I am currently having a root as global since I use it in the class as well as in main , how can I write it without having it global?</p>
  
  <pre><code>struct avl_node {
      int data;
      struct avl_node *left;
      struct avl_node *right;
  }*root;
  
  class avlTree {
      public:
          int height(avl_node *temp)
          {
              int h = 0;
              if (temp != NULL)
              {
                  int l_height = height (temp-&gt;left);
                  int r_height = height (temp-&gt;right);
                  int max_height = max (l_height, r_height);
                  h = max_height + 1;
              }
              return h;
          }
  
          int diff(avl_node *temp)
          {
              int l_height = height (temp-&gt;left);
              int r_height = height (temp-&gt;right);
              int b_factor= l_height - r_height;
              return b_factor;
          }
  
          avl_node *rr_rotation(avl_node *parent)
          {
              avl_node *temp;
              temp = parent-&gt;right;
              parent-&gt;right = temp-&gt;left;
              temp-&gt;left = parent;
              return temp;
          }       
  
          avl_node *ll_rotation(avl_node *parent)
          {
              avl_node *temp;
              temp = parent-&gt;left;
              parent-&gt;left = temp-&gt;right;
              temp-&gt;right = parent;
              return temp;
          }
  
          avl_node *lr_rotation(avl_node *parent)
          {
              avl_node *temp;
              temp = parent-&gt;left;
              parent-&gt;left = rr_rotation (temp);
              return ll_rotation (parent);
          }
  
          avl_node *rl_rotation(avl_node *parent)
          {
              avl_node *temp;
              temp = parent-&gt;right;
              parent-&gt;right = ll_rotation (temp);
              return rr_rotation (parent);
          }
  
          avl_node* balance(avl_node *temp)
          {
              int bal_factor = diff (temp);
              if (bal_factor &gt; 1)
              {
                  if (diff (temp-&gt;left) &gt; 0)
                      temp = ll_rotation (temp);
                  else
                      temp = lr_rotation (temp);
              }
              else if (bal_factor &lt; -1)
              {
                  if (diff (temp-&gt;right) &gt; 0)
                      temp = rl_rotation (temp);
                  else
                      temp = rr_rotation (temp);
              }
              return temp;
          }
  
          avl_node* insert(avl_node *root, int value)
          {
              if (root == NULL)
              {
                  root = new avl_node;
                  root-&gt;data = value;
                  root-&gt;left = NULL;
                  root-&gt;right = NULL;
                  return root;
              }
              else if (value &lt; root-&gt;data)
              {
                  root-&gt;left = insert(root-&gt;left, value);
                  root = balance (root);
              }
              else if (value &gt;= root-&gt;data)
              {
                  root-&gt;right = insert(root-&gt;right, value);
                  root = balance (root);
              }
              return root;
          }
  
          void display(avl_node *ptr, int level)
          {
              int i;
              if (ptr!=NULL)
              {
                  display(ptr-&gt;right, level + 1);
                  printf("\n");
                  if (ptr == root)
                      cout&lt;&lt;"Root -&gt; ";
                  for (i = 0; i &lt; level &amp;&amp; ptr != root; i++)
                      cout&lt;&lt;"        ";
                  cout&lt;&lt;ptr-&gt;data;
                  display(ptr-&gt;left, level + 1);
              }
          }
  
          void inorder(avl_node *tree)
          {
              if (tree == NULL)
                  return;
              inorder (tree-&gt;left);
              cout&lt;&lt;tree-&gt;data&lt;&lt;"  ";
              inorder (tree-&gt;right);
          }
  
          void preorder(avl_node *tree)
          {
              if (tree == NULL)
                  return;
              cout&lt;&lt;tree-&gt;data&lt;&lt;"  ";
              preorder (tree-&gt;left);
              preorder (tree-&gt;right);
          }
  
          void postorder(avl_node *tree)
          {
              if (tree == NULL)
                  return;
              postorder ( tree -&gt;left );
              postorder ( tree -&gt;right );
              cout&lt;&lt;tree-&gt;data&lt;&lt;"  ";
          }
  
          avlTree()
          {
              root = NULL;
          }
  };
  
  
  int main()
  {
      int choice, item;
      avlTree avl;
      while (1)
      {
          cout&lt;&lt;"\n---------------------"&lt;&lt;endl;
          cout&lt;&lt;"AVL Tree Implementation"&lt;&lt;endl;
          cout&lt;&lt;"\n---------------------"&lt;&lt;endl;
          cout&lt;&lt;"1.Insert Element into the tree"&lt;&lt;endl;
          cout&lt;&lt;"2.Display Balanced AVL Tree"&lt;&lt;endl;
          cout&lt;&lt;"3.InOrder traversal"&lt;&lt;endl;
          cout&lt;&lt;"4.PreOrder traversal"&lt;&lt;endl;
          cout&lt;&lt;"5.PostOrder traversal"&lt;&lt;endl;
          cout&lt;&lt;"6.Exit"&lt;&lt;endl;
          cout&lt;&lt;"Enter your Choice: ";
          cin&gt;&gt;choice;
          switch(choice)
          {
              case 1:
                  system("cls");
                  cout&lt;&lt;"Enter value to be inserted: ";
                  cin&gt;&gt;item;
                  root = avl.insert(root, item);
                  break;
              case 2:
                  system("cls");
                  if (root == NULL)
                  {
                      cout&lt;&lt;"Tree is Empty"&lt;&lt;endl;
                      continue;
                  }
                  cout&lt;&lt;"Balanced AVL Tree:"&lt;&lt;endl;
                  avl.display(root, 1);
                  break;
              case 3:
                  system("cls");
                  cout&lt;&lt;"Inorder Traversal:"&lt;&lt;endl;
                  avl.inorder(root);
                  cout&lt;&lt;endl;
                  break;
              case 4:
                  system("cls");
                  cout&lt;&lt;"Preorder Traversal:"&lt;&lt;endl;
                  avl.preorder(root);
                  cout&lt;&lt;endl;
                  break;
              case 5:
                  system("cls");
                  cout&lt;&lt;"Postorder Traversal:"&lt;&lt;endl;
                  avl.postorder(root);    
                  cout&lt;&lt;endl;
                  break;
              case 6:
                  exit(1);    
                  break;
              default:
                  system("cls");
                  cout&lt;&lt;"Wrong Choice"&lt;&lt;endl;
          }
      }
      return 0;
  }
  </code></pre>