Java Program to Implement AA Tree

This is a Java Program to implement AA Tree. An AA tree is a form of balanced tree used for storing and retrieving ordered data efficiently. AA trees are named for Arne Andersson, their inventor. AA trees are a variation of the red-black tree, which in turn is an enhancement to the binary search tree.

Here is the source code of the Java program to implement AA Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  *  Java Program to Implement AA Tree
  3.  */
  4.  
  5.  import java.util.Scanner;
  6.  import java.util.NoSuchElementException;
  7.  
  8.  /** Class AANode **/
  9.  class AANode
  10.  {    
  11.      AANode left, right;
  12.      int element, level;    
  13.  
  14.      /** Constructor **/    
  15.      public AANode()
  16.      {
  17.          this.element = 0;
  18.          this.left = this;
  19.          this.right = this;
  20.          this.level = 0;
  21.      }
  22.  
  23.      /** Constructor **/    
  24.      public AANode(int ele)
  25.      {
  26.          this(ele, null, null);
  27.      } 
  28.  
  29.      /** Constructor **/
  30.      public AANode(int ele, AANode left, AANode right)
  31.      {
  32.          this.element = ele;
  33.          this.left = left;
  34.          this.right = right;
  35.          this.level = 1;
  36.      }    
  37.  }
  38.  
  39.  /** Class AATree **/
  40.  class AATree
  41.  {
  42.      private AANode root;
  43.      private static AANode nil = new AANode();
  44.  
  45.      /** Constructor **/
  46.      public AATree()
  47.      {
  48.          root = nil;
  49.      }
  50.  
  51.      /** Function to check if tree is empty **/
  52.      public boolean isEmpty()
  53.      {
  54.          return root == nil;
  55.      }
  56.  
  57.      /** Make the tree empty **/
  58.      public void clear()
  59.      {
  60.          root = nil;
  61.      }
  62.  
  63.      /* Functions to insert data */
  64.      public void insert(int X)
  65.      {
  66.          root = insert(X, root);
  67.      }
  68.      private AANode insert(int X, AANode T)
  69.      {
  70.          if (T == nil)
  71.              T = new AANode(X, nil, nil);
  72.          else if ( X < T.element )
  73.              T.left = insert(X, T.left);
  74.          else if ( X > T.element)
  75.              T.right = insert(X, T.right);
  76.          else
  77.              return T;
  78.  
  79.          T = skew(T);
  80.          T = split(T);
  81.          return T;
  82.      }
  83.  
  84.      /** Function Skew **/
  85.      private AANode skew(AANode T) 
  86.      {
  87.          if (T == nil)
  88.              return nil;
  89.          else if (T.left == nil)
  90.              return T;
  91.          else if (T.left.level == T.level)
  92.          {
  93.              AANode L = T.left;
  94.              T.left = L.right;
  95.              L.right = T;
  96.              return L;
  97.          }         
  98.          else
  99.              return T;       
  100.      }
  101.  
  102.      /** Function split **/
  103.      private AANode split(AANode T) 
  104.      {
  105.          if (T == nil)
  106.              return nil;
  107.          else if (T.right == nil || T.right.right == nil)
  108.              return T;
  109.          else if (T.level == T.right.right.level)
  110.          {
  111.              AANode R = T.right;
  112.              T.right = R.left;
  113.              R.left = T;
  114.  
  115.              R.level = R.level + 1;
  116.              return R;
  117.          }
  118.          else
  119.              return T;
  120.      }    
  121.  
  122.      /** Function decrease key **/
  123.      private AANode decreaseLevel(AANode T)
  124.      {
  125.          int shouldBe = Math.min(T.left.level, T.right.level) + 1;
  126.          if (shouldBe < T.level)
  127.          {
  128.              T.level = shouldBe;
  129.              if (shouldBe < T.right.level)
  130.                  T.right.level = shouldBe;
  131.          }
  132.          return T;
  133.      }
  134.  
  135.      /** Functions to count number of nodes **/
  136.      public int countNodes()
  137.      {
  138.          return countNodes(root);
  139.      }
  140.      private int countNodes(AANode r)
  141.      {
  142.          if (r == nil)
  143.              return 0;
  144.          else
  145.          {
  146.              int l = 1;
  147.              l += countNodes(r.left);
  148.              l += countNodes(r.right);
  149.              return l;
  150.          }
  151.      }
  152.      /** Functions to search for an element **/
  153.      public boolean search(int val)
  154.      {
  155.          return search(root, val);
  156.      }
  157.      private boolean search(AANode r, int val)
  158.      {
  159.          boolean found = false;
  160.          while ((r != nil) && !found)
  161.          {
  162.              int rval = r.element;
  163.              if (val < rval)
  164.                  r = r.left;
  165.              else if (val > rval)
  166.                  r = r.right;
  167.              else
  168.              {
  169.                  found = true;
  170.                  break;
  171.              }
  172.              found = search(r, val);
  173.          }
  174.          return found;
  175.      }
  176.      /** Function for inorder traversal **/
  177.      public void inorder()
  178.      {
  179.          inorder(root);
  180.      }
  181.      private void inorder(AANode r)
  182.      {
  183.          if (r != nil)
  184.          {
  185.              inorder(r.left);
  186.              System.out.print(r.element +" ");
  187.              inorder(r.right);
  188.          }
  189.      }
  190.      /** Function for preorder traversal **/
  191.      public void preorder()
  192.      {
  193.          preorder(root);
  194.      }
  195.      private void preorder(AANode r)
  196.      {
  197.          if (r != nil)
  198.          {
  199.              System.out.print(r.element +" ");
  200.              preorder(r.left);             
  201.              preorder(r.right);
  202.          }
  203.      }
  204.      /** Function for postorder traversal **/
  205.      public void postorder()
  206.      {
  207.          postorder(root);
  208.      }
  209.      private void postorder(AANode r)
  210.      {
  211.          if (r != nil)
  212.          {
  213.              postorder(r.left);             
  214.              postorder(r.right);
  215.              System.out.print(r.element +" ");
  216.          }
  217.      }         
  218.  }
  219.  
  220.  /** Class AATree **/
  221.  public class AATreeTest
  222.  {
  223.      public static void main(String[] args)
  224.      {            
  225.         Scanner scan = new Scanner(System.in);
  226.         /** Creating object of AATree **/
  227.         AATree aat = new AATree(); 
  228.         System.out.println("AATree Tree Test\n");          
  229.         char ch;
  230.         /**  Perform tree operations  **/
  231.         do    
  232.         {
  233.             System.out.println("\nAATree Operations\n");
  234.             System.out.println("1. insert ");
  235.             System.out.println("2. search");
  236.             System.out.println("3. count nodes");
  237.             System.out.println("4. check empty");
  238.             System.out.println("5. clear");
  239.  
  240.             int choice = scan.nextInt();            
  241.             switch (choice)
  242.             {
  243.             case 1 : 
  244.                 System.out.println("Enter integer element to insert");
  245.                 aat.insert( scan.nextInt() );                     
  246.                 break;                          
  247.             case 2 : 
  248.                 System.out.println("Enter integer element to search");
  249.                 System.out.println("Search result : "+ aat.search( scan.nextInt() ));
  250.                 break;                                          
  251.             case 3 : 
  252.                 System.out.println("Nodes = "+ aat.countNodes());
  253.                 break;     
  254.             case 4 : 
  255.                 System.out.println("Empty status = "+ aat.isEmpty());
  256.                 break;
  257.             case 5 : 
  258.                 System.out.println("\nTree Cleared");
  259.                 aat.clear();
  260.                 break;            
  261.             default : 
  262.                 System.out.println("Wrong Entry \n ");
  263.                 break;   
  264.             }
  265.             /**  Display tree  **/ 
  266.             System.out.print("\nPost order : ");
  267.             aat.postorder();
  268.             System.out.print("\nPre order : "); 
  269.             aat.preorder();
  270.             System.out.print("\nIn order : ");
  271.             aat.inorder();
  272.  
  273.             System.out.println("\nDo you want to continue (Type y or n) \n");
  274.             ch = scan.next().charAt(0);                        
  275.         } while (ch == 'Y'|| ch == 'y');               
  276.      }
  277.  }

AATree Tree Test
 
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
4
Empty status = true
 
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
24
 
Post order : 24
Pre order : 24
In order : 24
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
5
 
Post order : 24 5
Pre order : 5 24
In order : 5 24
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
28
 
Post order : 5 28 24
Pre order : 24 5 28
In order : 5 24 28
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
6
 
Post order : 6 5 28 24
Pre order : 24 5 6 28
In order : 5 6 24 28
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
94
 
Post order : 6 5 94 28 24
Pre order : 24 5 6 28 94
In order : 5 6 24 28 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
63
 
Post order : 6 5 28 94 63 24
Pre order : 24 5 6 63 28 94
In order : 5 6 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
1
Enter integer element to insert
19
 
Post order : 5 19 6 28 94 63 24
Pre order : 24 6 5 19 63 28 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
2
Enter integer element to search
24
Search result : true
 
Post order : 5 19 6 28 94 63 24
Pre order : 24 6 5 19 63 28 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
2
Enter integer element to search
6
Search result : true
 
Post order : 5 19 6 28 94 63 24
Pre order : 24 6 5 19 63 28 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
2
Enter integer element to search
7
Search result : false
 
Post order : 5 19 6 28 94 63 24
Pre order : 24 6 5 19 63 28 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
3
Nodes = 7
 
Post order : 5 19 6 28 94 63 24
Pre order : 24 6 5 19 63 28 94
In order : 5 6 19 24 28 63 94
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
5
 
Tree Cleared
 
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
 
y
 
AATree Operations
 
1. insert
2. search
3. count nodes
4. check empty
5. clear
4
Empty status = true
 
Post order :
Pre order :
In order :
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.