Você está na página 1de 5

//DIVISON HASHING import java.io.

*; class HashEntry { private int key; private int value; HashEntry(int key, int value) { this.key = key; this.value = value; } public int getKey() { return key; } public int getValue() { return value; } } class HashMap { private final static int TABLE_SIZE = 128; HashEntry[] table; HashMap() { table = new HashEntry[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = null; } public int get(int key) { int hash = (key % TABLE_SIZE); while (table[hash] != null && table[hash].getKey() != key) hash = (hash + 1) % TABLE_SIZE; if (table[hash] == null) return -1; else return table[hash].getValue(); } public void put(int key, int value) { int hash = (key % TABLE_SIZE); while (table[hash] != null && table[hash].getKey() != key) hash = (hash + 1) % TABLE_SIZE; table[hash] = new HashEntry(key, value); System.out.println(key + " Element is inserted at " + hash + " location"); } } class DivisionHashing { public static void main(String args[]) throws IOException { HashMap hm = new HashMap(); DataInputStream din = new DataInputStream(System.in); int k, v, ch; do { System.out.println("1.Insert\n2.Search\n3.Exit\nEnter Ur Choice"); ch = Integer.parseInt(din.readLine()); switch (ch) { case 1: System.out.println("Enter the key"); k = Integer.parseInt(din.readLine()); System.out.println("Enter the value"); v = Integer.parseInt(din.readLine()); hm.put(k, v); break; case 2: System.out.println("Enter the search key:"); k = Integer.parseInt(din.readLine()); v = hm.get(k); if (v != -1) System.out.println("Key=" + k + " value=" + v); else System.out.println("Element is not found"); break; case 3: System.exit(0); } } while (ch != 3); } }

//MULTIPLICATION HASHING import java.io.*; class HashEntry { private int key; private int value; HashEntry(int key, int value) { this.key = key; this.value = value; } public int getKey() { return key; } public int getValue() { return value; } } class HashMap { private final static int TABLE_SIZE = 128; HashEntry[] table; HashMap() { table = new HashEntry[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = null; } public int get(int key) { int hash = (int) Math.floor(TABLE_SIZE* (key * 0.629 - Math.floor(key*0.629))); while (table[hash] != null && table[hash].getKey() != key) hash = (hash + 1) % TABLE_SIZE; if (table[hash] == null) return -1; else return table[hash].getValue(); } public void put(int key, int value) { int hash = (int) Math.floor(TABLE_SIZE* (key * 0.629 - Math.floor(key*0.629))); while (table[hash] != null && table[hash].getKey() != key) hash = (hash + 1) % TABLE_SIZE; table[hash] = new HashEntry(key, value); System.out.println(key + " Element is inserted at " + hash + " location"); } } class MultiplicationHashing { public static void main(String args[]) throws IOException { HashMap hm = new HashMap(); DataInputStream din = new DataInputStream(System.in); int k, v = 0, ch; do { System.out.println("1.Insert\n2.Search\n3.Exit\nEnter Ur Choice:"); ch = Integer.parseInt(din.readLine()); switch (ch) { case 1: System.out.println("Enter the key"); k = Integer.parseInt(din.readLine()); System.out.println("Enter the value"); v = Integer.parseInt(din.readLine()); hm.put(k, v); break; case 2: System.out.println("Enter the search key:"); k = Integer.parseInt(din.readLine()); int node = hm.get(k); if (node != -1) System.out.println("Key=" + k + " value=" + v); else System.out.println("Element is not found"); break; case 3: System.exit(0); } } while (ch != 3); } }

// UNIVERSAL HASHING import java.util.*; class HashMap { private final static int TABLE_SIZE = 11; int[] table; HashMap() { table = new int[TABLE_SIZE]; for (int i = 0; i < TABLE_SIZE; i++) table[i] = 0; } public void put(int key) { int a, b, _h; Random random = new Random(); a = getRandomInteger(1, 12, random); b = getRandomInteger(0, 12, random); _h = (a * key + b) % 13; int hash = (_h % TABLE_SIZE); while (table[hash] != 0 && table[hash] != key) hash = (hash + 1) % TABLE_SIZE; table[hash] = key; } int getRandomInteger(int aStart, int aEnd, Random aRandom) { long range = (long) aEnd - (long) aStart + 1; long fraction = (long) (range * aRandom.nextDouble()); int randomNumber = (int) (fraction + aStart); } return randomNumber;

public void printKeys() { for (int i = 0; i < TABLE_SIZE; i++) { if (table[i] != 0) System.out.println(i + ": " + table[i]); } } } class UniversalHash public static HashMap int k = { void main(String ar[]) { hs = new HashMap(); 0;

while (true) { System.out.println("Enter the elements of below 13. 999 for exit"); Scanner input = new Scanner(System.in); k = input.nextInt(); if (k == 999) break; hs.put(k); } hs.printKeys(); } }

// AVL TREE OPERATIONS import java.io.*; class AvlNode { int element; AvlNode left; AvlNode right; int height; AvlNode(int theElement) { this(theElement, null, null); } AvlNode(int theElement, AvlNode lt, AvlNode rt) { this.element = theElement; this.left = lt; this.right = rt; this.height = 0; } } class AvlTree { AvlNode root; public AvlTree() { this.root = null; } public AvlNode doubleWithLeftChild(AvlNode k3) { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); } public AvlNode doubleWithRightChild(AvlNode k1) { k1.right = rotateWithLeftChild(k1.right); return rotateWithRightChild(k1); } public int height(AvlNode t) { return t == null ? -1 : t.height; } private static int max(int lhs, int rhs) { return lhs > rhs ? lhs : rhs; } public AvlNode rotateWithLeftChild(AvlNode k2) { AvlNode k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = max(height(k2.left), height(k2.right)) + 1; k1.height = max(height(k1.left), k2.height) + 1; return k1; } public AvlNode rotateWithRightChild(AvlNode k1) { AvlNode k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = max(height(k1.left), height(k1.right)) + 1; k2.height = max(height(k2.right), k1.height) + 1; return k2; } public void insert(int x) { this.root = insert(x, this.root); } public AvlNode insert(int x, AvlNode t) { if (t == null) { t = new AvlNode(x, null, null); } else if (x < t.element) { t.left = insert(x, t.left); if (height(t.left) - height(t.right) == 2) { if (x < t.left.element) { t = rotateWithLeftChild(t); } else { t = doubleWithLeftChild(t);

} else if (x > t.element) { t.right = insert(x, t.right); if (height(t.right) - height(t.left) == 2) { if (x > t.right.element) { t = rotateWithRightChild(t); } else { t = doubleWithRightChild(t); } } } else { System.out.println("Element already existed"); } t.height = max(height(t.left), height(t.right)) + 1; return t; } public boolean isEmpty() { return root == null; } public void printTree() { if (isEmpty()) { System.out.println("Empty tree"); } else { printTree(this.root); } } public void printTree(AvlNode t) { if (t != null) { printTree(t.left); System.out.println(t.element); printTree(t.right); } } } class AVLTreeApp { public static void main(String ar[]) throws IOException { AvlTree tree = new AvlTree(); DataInputStream din = new DataInputStream(System.in); while (true) { System.out.println("1.Insertion\n2.Print Tree\n3.Exit\n"); System.out.println("Enter Ur choice"); int ch = Integer.parseInt(din.readLine()); switch (ch) { case 1: System.out.println("Enter the element:"); int ele = Integer.parseInt(din.readLine()); tree.insert(ele); break; case 2: tree.printTree(); break; case 3: System.exit(0); default: System.out.println("Invalid choice"); } } } }

Você também pode gostar