Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package adt.splaytree;
- import java.util.Arrays;
- import adt.avltree.AVLTreeImpl;
- import adt.bst.BSTNode;
- public class SplayTreeImpl<K extends Comparable<? super K>, V> extends AVLTreeImpl<K, V> implements
- SplayTree<K, V> {
- private void splay(BSTNode<K,V> node){
- if (node != null && node.getParent() != null) {
- if (node.getParent().getKey() == root.getKey()) {
- if (node.getParent().getLeft().getKey() == node.getKey())
- rightRotation(node.getParent());
- else if (node.getParent().getRight().getKey() == node.getKey())
- leftRotation(node.getParent());
- } else if (node.getKey() == node.getParent().getLeft().getKey()) {
- if (node.getParent().getKey() == node.getParent().getParent().getLeft().getKey()) {
- rightRotation(node.getParent().getParent());
- rightRotation(node.getParent());
- } else {
- rightRotation(node.getParent());
- leftRotation(node.getParent());
- }
- } else if (node.getKey() == node.getParent().getRight().getKey()) {
- if (node.getParent().getKey() == node.getParent().getParent().getRight().getKey()) {
- leftRotation(node.getParent().getParent());
- leftRotation(node.getParent());
- } else {
- leftRotation(node.getParent());
- rightRotation(node.getParent());
- }
- }
- }
- }
- public BSTNode<K, V> splaySearch(K key){
- BSTNode<K, V> node = search(key);
- if(!node.isEmpty()){
- splay(node);
- }else{
- splay(node.getParent());
- }
- return node;
- }
- @Override
- public void insert(K key, V value) {
- recursiveInsertion(root, key, value);
- }
- private void recursiveInsertion(BSTNode<K, V> node, K key, V value) {
- if (node.isEmpty()) {
- node.setKey(key);
- node.setValue(value);
- node.setLeft(new BSTNode<K, V>());
- node.getLeft().setParent(node);
- node.setRight(new BSTNode<K, V>());
- node.getRight().setParent(node);
- splay(node);
- } else {
- if (key.compareTo(node.getKey()) < 0) {
- recursiveInsertion(node.getLeft(), key, value);
- } else if (key.compareTo(node.getKey()) > 0) {
- recursiveInsertion(node.getRight(), key, value);
- }
- }
- }
- @Override
- public void remove(K key) {
- BSTNode<K, V> node = search(key);
- removeAux(node);
- }
- private void removeAux(BSTNode<K,V> node){
- if (!node.isEmpty()){
- if (node.getLeft().isEmpty() && node.getRight().isEmpty()){
- node.setKey(null);
- node.setValue(null);
- } else{
- if (!node.getLeft().isEmpty() || !node.getRight().isEmpty()){
- if (!node.equals(root)){
- if (node.getParent().getLeft().equals(node)){
- if (!node.getLeft().isEmpty()){
- node.getParent().setLeft(node.getLeft());
- } else{
- node.getParent().setLeft(node.getRight());
- }
- } else {
- if (!node.getLeft().isEmpty()){
- node.getParent().setRight(node.getLeft());
- } else{
- node.getParent().setRight(node.getRight());
- }
- }
- } else{
- if (!root.getLeft().isEmpty() && root.getRight().isEmpty()){
- root = root.getLeft();
- root.setParent(null);
- } else{
- if (root.getLeft().isEmpty() && !root.getRight().isEmpty()) {
- root = root.getRight();
- root.setParent(null);
- } else{
- BSTNode<K, V> sucessor = sucessor(node);
- node.setKey(sucessor.getKey());
- node.setValue(sucessor.getValue());
- removeAux(sucessor);
- }
- }
- }
- } else{
- if (node.getParent() == null){
- node.setKey(null);
- node.setValue(null);
- }
- BSTNode<K, V> sucessor = sucessor(node);
- node.setKey(sucessor.getKey());
- node.setValue(sucessor.getValue());
- removeAux(sucessor);
- }
- }
- }
- }
- public void splayRemove(K key){
- BSTNode<K, V> node = search(key);
- if(!node.isEmpty()){
- splay(node.getParent());
- }else{
- this.remove(node.getKey());
- splay(node.getParent());
- }
- }
- public Comparable findMin() {
- BSTNode aux = root;
- if(root == null) return null;
- while(aux.left != null) aux = aux.left;
- splay(aux);
- return aux.key;
- }
- /**
- * Find the largest item in the tree.
- */
- public Comparable findMax() {
- BSTNode aux = root;
- if(root == null) return null;
- while(aux.right != null) aux = aux.right;
- splay(aux);
- return aux.key;
- }
- public static void main(String[] args) {
- // SplayTreeImpl<Integer, Integer> arvore = new SplayTreeImpl<Integer, Integer>();
- // arvore.insert(0, 0);
- // arvore.insert(1, 1);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // System.out.println(arvore.search(0));
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(4, 4);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(2, 2);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(3, 3);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // System.out.println(arvore.search(1));
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // System.out.println(arvore.search(0));
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(10, 10);
- // arvore.insert(5, 5);
- // arvore.insert(6, 6);
- // arvore.insert(9, 9);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- SplayTreeImpl<Integer,Integer> node= new SplayTreeImpl<Integer,Integer>();
- // for(int i = 0;i<11;i++){
- // node.insert(i, i);
- // }
- // System.out.println(Arrays.toString(node.preOrder()));
- // SplayTreeImpl<Integer, Integer> arvore = new SplayTreeImpl<Integer, Integer>();
- // arvore.insert(0, 0);
- // arvore.insert(1, 1);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // System.out.println(arvore.search(0));
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(4, 4);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(2, 2);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(3, 3);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // System.out.println(arvore.search(1));
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // System.out.println(arvore.search(0));
- // System.out.println(Arrays.toString(arvore.preOrder()));
- // arvore.insert(10, 10);
- // arvore.insert(5, 5);
- // arvore.insert(6, 6);
- // arvore.insert(9, 9);
- // System.out.println(Arrays.toString(arvore.preOrder()));
- node.insert(10, 10);
- node.insert(0, 0);
- node.insert(1, 1);
- node.insert(2, 2);
- node.insert(3, 3);
- node.insert(4, 4);
- node.insert(5, 5);
- node.insert(6,6);
- node.insert(7, 7);
- node.insert(8,8);
- node.insert(9, 9);
- }
- }
Add Comment
Please, Sign In to add comment