AVL TREE JAVA to store string

public AVLNode search(String key)
   {
      // Tree is empty
      if (root == null)
      {
            return null;
        }

      // Tree is not empty
      AVLNode p = root;
      try
      {while (true)
      {
            if(p == null && p.getData().equals(key))
            {
                break;
            }
            else if (key.compareTo(p.getData()) < 0)
            {
                p = p.getLeftChild();
            }
            else
            {
                p = p.getRightChild();
            }
      }
      }

      catch(NullPointerException e)
      {
          System.out.println(e);
      }
      return p;
   }

    // INSERT A NODE INTO AVL TRRE
    public void insert(String data) 
    {
        // Case: tree is empty  <------------------------------
        if (root == null)
        {
            root = new AVLNode(data);
            return;
        }

        // Case: tree is not empty
        AVLNode p = root;

        // move p down as far as we can
        while(true)
        {
            if (data.equals(p.getData()) && p.getLeftChild() != null)
            {
                p = p.getLeftChild();
            }

            else if(data.compareTo(p.getData()) < 0 && p.getLeftChild() != null)
            {
                p = p.getLeftChild();
            }
            else if (data.compareTo(p.getData()) > 0 && p.getRightChild() != null)
            {
                p = p.getRightChild();
            }
            else
            {
                break;
            }
        }

        // insert new node to the tree as a child of node p

        AVLNode newNode= new AVLNode(data);

        if(data.equals(p.getData()))
        {
            p.setLeftChild(newNode);
        }

        else if (data.compareTo(p.getData()) < 0)
        {
            p.setLeftChild(newNode);
        }
        else
        {
            p.setRightChild(newNode);
        }

        rebalanceInsertionPath(newNode);
    }

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source