diff --git a/Java/data-structure-with-java/tree/avl/AVLNode.java b/Java/data-structure-with-java/tree/avl/AVLNode.java deleted file mode 100644 index 472fdeb..0000000 --- a/Java/data-structure-with-java/tree/avl/AVLNode.java +++ /dev/null @@ -1,22 +0,0 @@ -package tree.avl; - -class AVLNode{ - - AVLNode left, right; - int data; - int height; - - public AVLNode(){ - left = null; - right = null; - data = 0; - height = 0; - } - - public AVLNode(int n){ - left = null; - right = null; - data = n; - height = 0; - } -} diff --git a/Java/data-structure-with-java/tree/avl/AVLTree.java b/Java/data-structure-with-java/tree/avl/AVLTree.java deleted file mode 100644 index 25ae229..0000000 --- a/Java/data-structure-with-java/tree/avl/AVLTree.java +++ /dev/null @@ -1,195 +0,0 @@ -package tree.avl; - -class AVLTree{ - private AVLNode root; - public AVLTree(){ - root = null; - } - - - /** - * - * @param avlNode - * @return - */ - private int height(AVLNode avlNode ){ - return avlNode == null ? -1 : avlNode.height; - } - - /** - * - * @param lHeight - * @param rHeight - * @return - */ - private int max(int lHeight, int rHeight){ - return lHeight > rHeight ? lHeight : rHeight; - } - - /** - * - * @param data - */ - public void insert(int data){ - root = insert(data, root); - } - - - /** - * - * @param data - * @param avlNode - * @return - */ - private AVLNode insert(int data, AVLNode avlNode) - { - if (avlNode == null) - avlNode = new AVLNode(data); - else if (data < avlNode.data){ - avlNode.left = insert( data, avlNode.left ); - if( height( avlNode.left ) - height( avlNode.right ) == 2 ) - if( data < avlNode.left.data ) - avlNode = leftRotation( avlNode ); - else - avlNode = leftRightRotation( avlNode ); - } - else if( data > avlNode.data ){ - avlNode.right = insert( data, avlNode.right ); - if( height( avlNode.right ) - height( avlNode.left ) == 2 ) - if( data > avlNode.right.data) - avlNode = rightRotation( avlNode ); - else - avlNode = rightLeftRotation( avlNode ); - } - else - ; // Duplicate; do nothing - avlNode.height = max( height( avlNode.left ), height( avlNode.right ) ) + 1; - return avlNode; - } - - /** - * - * @param avlNode - * @return - */ - private AVLNode leftRotation(AVLNode avlNode){ - AVLNode k1 = avlNode.left; - avlNode.left = k1.right; - k1.right = avlNode; - avlNode.height = max( height( avlNode.left ), height( avlNode.right ) ) + 1; - k1.height = max( height( k1.left ), avlNode.height ) + 1; - return k1; - } - - - /** - * - * @param avlNode - * @return - */ - private AVLNode rightRotation(AVLNode avlNode){ - AVLNode node = avlNode.right; - avlNode.right = node.left; - node.left = avlNode; - avlNode.height = max( height( avlNode.left ), height( avlNode.right ) ) + 1; - node.height = max( height( node.right ), avlNode.height ) + 1; - return node; - } - /** - * left-right rotation - * @param avlNode - * @return - */ - private AVLNode leftRightRotation(AVLNode avlNode){ - avlNode.left = rightRotation( avlNode.left ); - return leftRotation( avlNode ); - } - - /** - * right-left rotation - * @param avlNode - * @return - */ - private AVLNode rightLeftRotation(AVLNode avlNode) - { - avlNode.right = leftRotation( avlNode.right ); - return rightRotation( avlNode ); - } - - /** - * - * @return - */ - public int countNodes(){ - return countNodes(root); - } - - /** - * - * @param avlNode - * @return - */ - private int countNodes(AVLNode avlNode){ - if (avlNode == null) - return 0; - else{ - int l = 1; - l += countNodes(avlNode.left); - l += countNodes(avlNode.right); - return l; - } - } - - /** - * - * @param data - * @return - */ - public boolean search(int data){ - return search(root, data); - } - - /** - * - * @param avlNode - * @param data - * @return - */ - private boolean search(AVLNode avlNode, int data){ - boolean found = false; - while ((avlNode != null) && !found) - { - int rval = avlNode.data; - if (data < rval) - avlNode = avlNode.left; - else if (data > rval) - avlNode = avlNode.right; - else - { - found = true; - break; - } - found = search(avlNode, data); - } - return found; - } - - /** - * - */ - public void inorder(){ - inorder(root); - } - - /** - * - * @param avlNode - */ - private void inorder(AVLNode avlNode){ - if (avlNode != null){ - inorder(avlNode.left); - System.out.print(avlNode.data +" "); - inorder(avlNode.right); - } - } -} diff --git a/Java/data-structure-with-java/tree/avl/AVLTreeHelper.java b/Java/data-structure-with-java/tree/avl/AVLTreeHelper.java deleted file mode 100644 index 5e194c3..0000000 --- a/Java/data-structure-with-java/tree/avl/AVLTreeHelper.java +++ /dev/null @@ -1,42 +0,0 @@ -package tree.avl; - -import java.util.Scanner; - -public class AVLTreeHelper{ - public static void main(String[] args){ - Scanner scanner = new Scanner(System.in); - AVLTree avlTree = new AVLTree(); - - char ch; - do{ - System.out.println("\nAVLTree Operations\n"); - System.out.println("1. insert "); - System.out.println("2. search"); - System.out.println("3. count nodes"); - int choice = scanner.nextInt(); - switch (choice) - { - case 1 : - System.out.println("Enter integer element to insert"); - avlTree.insert( scanner.nextInt() ); - break; - case 2 : - System.out.println("Enter integer element to search"); - System.out.println("Search result : "+ avlTree.search( scanner.nextInt())); - break; - case 3 : - System.out.println("Nodes = "+ avlTree.countNodes()); - break; - default : - System.out.println("Wrong Entry \n "); - break; - } - - System.out.print("\nIn order : "); - avlTree.inorder(); - - System.out.println("\nDo you want to continue (Type y or n) \n"); - ch = scanner.next().charAt(0); - } while (ch == 'Y'|| ch == 'y'); - } -}