Advertisement
ashpatel

AVL Tree Java

May 8th, 2021
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. public class Node {
  2.     public int val = 0;
  3.     public Node left = null;
  4.     public Node right = null;
  5.     public int leftHeight = 0;
  6.     public int rightHeight = 0;
  7. }
  8.  
  9. public class AVLTree {
  10.     public int outBalance = 3;
  11.     public Node root = null;
  12.     public Node badBalance = null;
  13.  
  14.     public void insert (Node current = root) {
  15.         if (null != current) {
  16.             if (current.val < this.val) {
  17.                 current.right += 1;
  18.                 if (Math.abs(current.right - current.left) == outBalance) {
  19.                     badBalance = current;
  20.                 }
  21.                 if (null != current.right) {
  22.                     insert(current.right);
  23.                 } else {
  24.                     current.right = this;
  25.                     if (null != outBalance) {
  26.                         outBalance = null:
  27.      
  28.  
  29.                 current.left += 1;
  30.                 if (Math.abs(current.left - current.right) == outBalance) {
  31.                     badBalance = current;
  32.                 }
  33.                 if (null != current.left) {
  34.                     insert(current.left);
  35.                 } else {
  36.                     current.left = this;
  37.                     if (null != outBalance) {
  38.                         outBalance = null:
  39.                         //rotate current
  40.                     }
  41.                 }
  42.             }
  43.         } else {
  44.             root = this;          
  45.         }
  46.     }
  47.  
  48.    
  49. public class AVL {
  50.     public static void main(String[] args) {
  51.        
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement