AVL Tree Basic Operations
Not finished .... Cond....
AVL Tree Code
public class Avltree<key extends Comparable<? Super Key> value> {private class Node {key key;//key, equivalent to the word value value;//value in the dictionary, which is equivalent to the word interpretation in the dictionary The height of the int height;//node is node left; Node right; Public Node (key key, value value) {This.key = key; This.value = value; This.left = null; This.right = null; int height = 0; }} private Node root; Public Avltree () {root = null; } private int height (node node) {if (node! = null) {return node.height; } return 0; } public int height () {return height (root); } private int Max (int a, int b) {return a > b? a:b; } private void Preorder (node node) {if (node! = null) {System.out.println (Node.key); Preorder (Node.left); Preorder (node.right); }} public void Preorder () {preorder (root); } PRivate void Inorder (node node) {if (node! = null) {inorder (node.left); System.out.println (Node.key); Inorder (Node.right); }} public void Inorder () {inorder (root); } public void Postorder (node node) {if (node! = null) {postorder (node.left); Postorder (Node.right); System.out.println (Node.key); }} public void Postorder () {postorder (root); } private node Search (node node, key key) {if (node = = null) {return null; } else if (Key.compareto (node.key) = = 0) {return node; } else if (Key.compareto (Node.key) < 0) {return search (Node.left, key); } else {//key.compareto (Node.key) > 0 return Search (node.right, key); }} public Node search (key key) {return search (root, key); Private node Minnode (node node) {if (node = = null) {return null } else if (Node.left = = null) {return node; } else {return Minnode (node.left); }} public Node Minnode () {return minnode (root); Private node Maxnode (node node) {if (node = = null) {return null; } else if (node.right = = null) {return node; } else {return Maxnode (node.right); }} public Node Maxnode () {return maxnode (root); }//For the following LL case////K1 k2///\///K2 Z ll turn x k1///\----\////X y----/O y z//////O//// or////K1 k2///\///K2 z ll turn x K1 /\----\ \///x y----/O y z////O/Private N Ode leftleftrotation (Node K1) { Node K2 = K1.left; K2 is the left subtree of k1 k1.left = k2.right;//k2 the right subtree becomes K1 the left subtree k2.right = K1; K1 becomes k2 right subtree K1.height = max (height (k1.left), height (k1.right)) + 1;//calculates k1 height k2.height = max (height (k2.left ), K1.height) + 1;//calculates the height of the K2 return k2;//returns the new root K2}//For the following RR conditions////K1 K2 /\///x K2 RR Turn K1 K3///\----\/\ \ Y K3----/x y z////z///////// K1 k2///\///x K2 rr turn K1 K3 /\----\/\///y K3----/x y z//////Z Public node Rightrightrotation (node K1) {node K2 = k1.right; K1.right = K2.left; K2.left = K1; K1.height = max (height (k1.left), HEight (k1.right)) + 1; K2.height = max (k1.height, Height (k2.right)) + 1; return K2; }//For the following LR conditions//K1 K1 K3///\/\//// K2 z K2 l K3 z K1 right spin k2 k1///\-----\/\-----\/\///w K3-----/k 2 y-----/w x y z///\ RR single turn/\ ll single turn//x y W X//Public Node LEFTR Ightrotation (Node k1) {k1.left = Rightrightrotation (k1.left); return leftleftrotation (K1); }//to the following RL case//K1 K1 K3///\ k2 right/\ K1 L///W K2-----\ w K3-----\ K1 k2///\-----//\-----//\///K3 Z ll single turn X K2 RR paddle W x y z///\///x y y z//public Node Rightlef Trotation (Node k1) {k1.right = Leftleftrotation (k1.right); return rightrightrotation (K1); }//Insert//delete}
AVL tree (Java Implementation)