Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A generic AVL Tree implementation.
- The AVLTree and associated classes are below the Test_AVLTree class.
- This version has been tested for edge cases, but no optimization
- effort has been made.
- Tim Bartlett, Feb 19th, 2017.
- */
- import java.util.Random;
- import java.math.BigInteger;
- /*
- Main test class
- Test of Generic AVL Tree using Strings as data and BigIntegers as keys.
- A unique number is assigned to each String by the Converter class, and
- this number is used as the key. A higher key indicates a further place in
- Alphabetical order, so the result is that the Strings remain in alphabetical
- order and search time is limited: You could use the Converter on a String
- to get a key for avltree.getData(key), which searches the tree for this key
- and returns a reference to the data.
- This could easily be extended so that the String is some other Object
- with a field that would could be used in the Converter method to generate
- unique keys.
- */
- public class Test_AVLTree{
- static final char[] ALPHABET =
- {'A','B','C','D','E','F','G','H','I','J','K','L','M',
- 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
- static final int CHARACTER_LIM = 2;
- static final int NUM_INSERTS = 25;
- static final int NUM_REMOVES = 25;
- public static void main(String args[]){
- //handle arguments
- Long seed = (new Random()).nextLong();
- if(args.length != 0){
- try{
- seed = Long.parseLong(args[0]);
- }catch(Exception ex){
- System.out.println("Bad seed arg");
- return;
- }
- }
- Random rand = new Random(seed);
- System.out.println("\nseed (use as argument to repeat): " +
- seed + "\n");
- /* class Converter
- Converts our random strings into unique BigIntegers, such that
- a greater number indicates a further place in the order
- defined by the characters in char[] order.
- in our case, this is the char[] ALPHABET above.
- */
- class Converter implements DataToKeyConverter<BigInteger,String>{
- char[] order = ALPHABET;
- int longest = CHARACTER_LIM;
- public BigInteger dataToKey(String data){
- BigInteger n = new BigInteger("0");
- BigInteger n2;
- //for every char in string
- for(int j = 0, jLim = data.length(); j < jLim; j++){
- //for every char in order[]
- for(int k = 0, kLim = order.length; k < kLim; k++){
- //if current char equals char in order
- if(data.charAt(j) == order[k]){
- //add to n according to
- // n += (k+1)(order.length^(longest - j);
- // so the first character has the most weight
- // (little endian)
- n2 = new BigInteger(order.length + "");
- n2 = n2.pow(longest - j);
- n = n.add(
- n2.multiply(new BigInteger((k + 1) + "")));
- break;
- }
- }
- }
- return n;
- }
- }
- //construct an avltree using a new Converter as the parameter
- // This Converter will now be used every insert() and remove()
- AVLTree<BigInteger, String> avltree = new AVLTree<>(new Converter());
- String[] insertedData = new String[NUM_INSERTS];
- String s = "";
- String data;
- long t = System.currentTimeMillis();
- for(int i = 0; i < NUM_INSERTS; i++){
- data = "";
- for(int j = 0; j < CHARACTER_LIM; j++){
- data += ALPHABET[rand.nextInt(ALPHABET.length)];
- }
- try{
- avltree.insert(data);
- }catch(Exception ex){
- //System.out.println(ex);
- }
- insertedData[i] = data;
- s += data += " ";
- }
- //System.out.println("Inserted in this order:\n" + s);
- System.out.println(
- NUM_INSERTS + " inserts took " +
- (System.currentTimeMillis() - t) + "ms");
- /*
- System.out.println(
- "\nInorder after insert() calls (dots indicate level):\n"+
- avltree.toString_InorderDetailed() + "\n"+
- "Inorder, displaying data:\n"+
- avltree.toString_InorderData() + "\n");
- */
- s = "";
- t = System.currentTimeMillis();
- for(int i = 0; i < NUM_REMOVES; i++){
- data = insertedData[rand.nextInt(insertedData.length)];
- try{
- avltree.remove(data);
- }catch(Exception ex){
- //System.out.println(ex);
- }
- s += data += " ";
- }
- //System.out.println("\nRemoved in this order:\n" + s);
- System.out.println(
- NUM_REMOVES + " removes took " +
- (System.currentTimeMillis() - t) + "ms");
- /*
- System.out.println(
- "\nInorder after remove() calls (dots indicate level):\n" +
- avltree.toString_InorderDetailed() + "\n"+
- "Inorder, displaying data:\n"+
- avltree.toString_InorderData());
- */
- System.out.println(avltree.toString_InorderDataDetailed());
- }
- }
- /*
- This is the interface for data to key conversions. A class
- implementing this is required in the AVLTree constructor
- */
- interface DataToKeyConverter<K extends Comparable<K>, E>{
- public K dataToKey(E data);
- }
- /*
- This node used in the AVLTree
- */
- class AVLNode<K extends Comparable<K>, E>{
- E data;
- K key;
- AVLNode<K,E> parent;
- AVLNode<K,E> left;
- AVLNode<K,E> right;
- public AVLNode(){
- data = null;
- key = null;
- parent = null;
- left = null;
- right = null;
- }
- public void setData(E data){this.data = data;}
- public void setKey(K key){this.key = key;}
- public void setParent(AVLNode<K,E> parent){this.parent = parent;}
- public void setLeft(AVLNode<K,E> left){this.left = left;}
- public void setRight(AVLNode<K,E> right){this.right = right;}
- public E getData(){return data;}
- public K getKey(){return key;}
- public AVLNode<K,E> getParent(){return parent;}
- public AVLNode<K,E> getLeft(){return left;}
- public AVLNode<K,E> getRight(){return right;}
- }
- /*
- The AVLTree
- public methods:
- insert(E data), remove(E data), getData(K key) and many toString()
- methods which are defined at the end of the class.
- */
- class AVLTree<K extends Comparable<K>,E>{
- public AVLNode<K,E> root;
- DataToKeyConverter<K,E> convert;
- public AVLTree(DataToKeyConverter<K,E> convert){
- this.convert = convert;
- root = null;
- }
- public void insert(E data) throws IndexOutOfBoundsException{
- AVLNode<K,E> node_new = new AVLNode<K,E>();
- K key = convert.dataToKey(data);
- node_new.setKey(key);
- node_new.setData(data);
- //if tree is empty
- if(root == null){
- root = node_new;
- return;
- }
- //find position and insert new node
- AVLNode<K,E> node_temp = root;
- AVLNode<K,E> node_tempParent = null;
- boolean isDeepenedLeft = false;
- while(node_temp != null){
- if(node_temp.getKey().compareTo(key) < 0){
- if(node_temp.getRight() != null){
- node_tempParent = node_temp;
- node_temp = node_temp.getRight();
- }else{
- node_new.setParent(node_temp);
- node_temp.setRight(node_new);
- isDeepenedLeft = false;
- break;
- }
- }else if(node_temp.getKey().compareTo(key) > 0){
- if(node_temp.getLeft() != null){
- node_tempParent = node_temp;
- node_temp = node_temp.getLeft();
- }else{
- node_new.setParent(node_temp);
- node_temp.setLeft(node_new);
- isDeepenedLeft = true;
- break;
- }
- }else{
- //duplicate
- throw new IndexOutOfBoundsException(
- "ERR: insert(" + data + "): duplicate value");
- }
- }
- balance(node_new);
- }
- public void remove(E data) throws IndexOutOfBoundsException{
- K key = convert.dataToKey(data);
- //search for node with matching key, node_temp
- AVLNode<K,E> node_temp = root;
- K temp_key;
- while(node_temp != null){
- temp_key = node_temp.getKey();
- if(temp_key.compareTo(key) > 0){
- node_temp = node_temp.getLeft();
- }else if(temp_key.compareTo(key) < 0){
- node_temp = node_temp.getRight();
- }else{
- //equal
- break;
- }
- }
- if(node_temp == null){
- throw new IndexOutOfBoundsException(
- "ERR: remove(" + data + "): no match");
- }
- //reserve pointers of node_temp
- AVLNode<K,E> node_tL = node_temp.getLeft();
- AVLNode<K,E> node_tR = node_temp.getRight();
- AVLNode<K,E> node_tP = node_temp.getParent();
- AVLNode<K,E> node_toBalance = node_tP;
- AVLNode<K,E> node_tC = null; //temp Closest
- //is the node_temp higher or lower than parent
- boolean isLeftOfParent = false;
- if(node_temp.getParent() != null &&
- node_temp.getParent().getKey().compareTo(node_temp.getKey())>0){
- isLeftOfParent = true;
- }
- //four main cases, depending on number of branches from node_temp
- if(node_tL == null && node_tR == null){ //if no branches
- if(node_tP == null){
- root = null;
- throw new IndexOutOfBoundsException(
- "ERR: remove(" + data + "): all nodes removed");
- }
- if(isLeftOfParent){
- node_tP.setLeft(null);
- }else{
- node_tP.setRight(null);
- }
- }else if(node_tL == null){ //if one branch on right
- node_tR.setParent(node_tP);
- if(isLeftOfParent){
- if(node_tP != null) node_tP.setLeft(node_tR);
- else root = node_tR;
- }else{
- if(node_tP != null) node_tP.setRight(node_tR);
- else root = node_tR;
- }
- }else if(node_tR == null){ //if one branch on left
- node_tL.setParent(node_tP);
- if(isLeftOfParent){
- if(node_tP != null) node_tP.setLeft(node_tL);
- else root = node_tL;
- }else{
- if(node_tP != null) node_tP.setRight(node_tL);
- else root = node_tL;
- }
- }else{ //both branches exist
- int depthL = getDepth(node_tL);
- int depthR = getDepth(node_tR);
- //two cases for if replacement is on left or right
- if(depthL > depthR){ //take replacement from deeper, left side
- //search for closest to node_temp
- AVLNode<K,E> node = node_tL;
- while(node != null){
- node_tC = node;
- node = node.getRight();
- }
- //if closest is not just one below
- if(node_tC.getParent() != node_temp){
- node_toBalance = node_tC.getParent();
- node_tC.getParent().setRight(node_tC.getLeft());
- if(node_tC.getLeft() != null){
- node_tC.getLeft().setParent(node_tC.getParent());
- }
- }else{ //closest is just one below
- if(node_tP != null){
- if(isLeftOfParent){
- node_tP.setLeft(node_tC);
- }else{
- node_tP.setRight(node_tC);
- }
- }
- if(node_tL != null) node_tL.setParent(node_tC);
- if(node_tR != null) node_tR.setParent(node_tC);
- }
- }else{ //take replacement from right
- //search for closest to node_temp
- AVLNode<K,E> node = node_tR;
- while(node != null){
- node_tC = node;
- node = node.getLeft();
- }
- //if closest is not just one below
- if(node_tC.getParent() != node_temp){
- node_toBalance = node_tC.getParent();
- node_tC.getParent().setLeft(node_tC.getRight());
- if(node_tC.getRight() != null){
- node_tC.getRight().setParent(node_tC.getParent());
- }
- }else{ //closest is just one below
- if(node_tP != null){
- if(isLeftOfParent){
- node_tP.setLeft(node_tC);
- }else{
- node_tP.setRight(node_tC);
- }
- }
- if(node_tL != null) node_tL.setParent(node_tC);
- if(node_tR != null) node_tR.setParent(node_tC);
- }
- }
- //set node_tC.left to that of removed node
- if(node_tC == node_tL){ //if they're the same
- if(node_tC.getLeft() != null){
- node_tC.getLeft().setParent(node_tC);
- }
- node_tC.setLeft(node_tC.getLeft());
- }else{
- node_tC.setLeft(node_tL);
- }
- //set parent for node left of removed node
- if(node_tL != null){
- node_tL.setParent(node_tC);
- }
- //set node_tC.right to that of removed node
- if(node_tC == node_tR){ //if they're the same
- if(node_tC.getRight() != null){
- node_tC.getRight().setParent(node_tC);
- }
- node_tC.setRight(node_tC.getRight());
- }else{
- node_tC.setRight(node_tR);
- }
- //set parent for node right of removed node
- if(node_tR != null){
- node_tR.setParent(node_tC);
- }
- node_tC.setParent(node_tP);
- //set the left or right of removed node, if it's higher or lower
- if(node_tP == null){
- root = node_tC;
- }else{
- if(isLeftOfParent){
- node_tP.setLeft(node_tC);
- }else{
- node_tP.setRight(node_tC);
- }
- }
- }
- balance(node_toBalance);
- }
- public E getData(K key){
- AVLNode<K,E> node_temp = root;
- while(node_temp != null){
- if(node_temp.getKey().compareTo(key) < 0){
- node_temp = node_temp.getRight();
- }else if(node_temp.getKey().compareTo(key) > 0){
- node_temp = node_temp.getLeft();
- }else{
- //equal
- return node_temp.getData();
- }
- }
- //no match
- return null;
- }
- int getDepth(AVLNode<K,E> avlnode){
- return getDepth(avlnode,0);
- }
- int getDepth(AVLNode<K,E> avlnode, int depthAbove){
- if(avlnode == null) return depthAbove;
- AVLNode<K,E> avlnodeR = avlnode.getRight();
- AVLNode<K,E> avlnodeL = avlnode.getLeft();
- int depthRight = getDepth(avlnodeR, depthAbove);
- int depthLeft = getDepth(avlnodeL, depthAbove);
- if(depthRight > depthLeft) return depthRight + 1;
- else return depthLeft + 1;
- }
- //This function begins at the given node_temp and
- // checks the balance at every level up to the root,
- // calling rotate functions as necessary.
- private void balance(AVLNode<K,E> node_temp){
- /*
- tp
- / \
- L R
- / \ / \
- LL LR RL RR
- */
- //check given node for balance, continue to parent if balanced
- AVLNode<K,E> node_tempL, node_tempR;
- int depthL, depthR, depthLL, depthLR, depthRL, depthRR;
- while(node_temp != null){
- //depths from branches
- depthL = getDepth(node_temp.getLeft());
- depthR = getDepth(node_temp.getRight());
- //reserve temp left and right pointers
- node_tempL = node_temp.getLeft();
- node_tempR = node_temp.getRight();
- //if left branch exists, get its depths
- if(node_tempL != null){
- depthLL = getDepth(node_tempL.getLeft());
- depthLR = getDepth(node_tempL.getRight());
- }else{
- depthLL = -1;
- depthLR = -1;
- }
- //if right branch exists, get its depths
- if(node_tempR != null){
- depthRL = getDepth(node_tempR.getLeft());
- depthRR = getDepth(node_tempR.getRight());
- }else{
- depthRL = -1;
- depthRR = -1;
- }
- //continue to parent node if balanced
- switch(depthL - depthR){
- case 0:
- case -1:
- case 1:
- node_temp = node_temp.getParent();
- continue;
- }
- //4 cases of rotations, based on depths of branches
- if(depthR > depthL && depthRR > depthRL){ //if longer R & RR
- rotationL(node_temp);
- //check blanace of lengthened side
- balance(node_temp);
- break;
- }else if(depthR > depthL && depthRL > depthRR){ //if longer R & RL
- rotationLR(node_temp);
- //check balance of both sides
- balance(node_temp);
- balance(node_tempR);
- break;
- }else if(depthL > depthR && depthLL > depthLR){ //if longer L & LL
- rotationR(node_temp);
- //check blanace of lengthened side
- balance(node_temp);
- break;
- }else if(depthL > depthR && depthLR > depthLL){ //if longer L & LR
- rotationRL(node_temp);
- //check balance of both sides
- balance(node_temp);
- balance(node_tempL);
- break;
- }
- //if codes makes it here, continue to parent node
- node_temp = node_temp.getParent();
- }//EO while(node_temp != null)
- }
- void rotationL(AVLNode<K,E> node_root){
- /*
- Pr
- |
- Rt
- / \
- l1 r1
- / \
- l2 r2
- / \
- l3 r3
- */
- AVLNode<K,E> node_parent = node_root.getParent();
- AVLNode<K,E> node_r1 = node_root.getRight();
- AVLNode<K,E> node_r2 = node_r1.getRight();
- AVLNode<K,E> node_r3 = node_r2.getRight();
- AVLNode<K,E> node_l1 = node_root.getLeft();
- AVLNode<K,E> node_l2 = node_r1.getLeft();
- AVLNode<K,E> node_l3 = node_r2.getLeft();
- if(node_root == this.root){
- root = node_r1;
- }
- /*
- Pr
- |
- r1
- / \
- Rt r2
- / \ / \
- l1 l2 l3 r3
- */
- boolean isRootLeftOfParent = false;
- if(node_parent != null && node_parent.getLeft() == node_root){
- isRootLeftOfParent = true;
- }
- if(node_parent != null){
- if(isRootLeftOfParent){
- node_parent.setLeft(node_r1);
- }else{
- node_parent.setRight(node_r1);
- }
- }
- if(node_r1 != null){
- node_r1.setParent(node_parent);
- node_r1.setLeft(node_root);
- node_r1.setRight(node_r2);
- }
- if(node_root != null){
- node_root.setParent(node_r1);
- //node_l1 already on left
- node_root.setRight(node_l2);
- }
- if(node_r2 != null){
- node_r2.setParent(node_r1);
- //node_l3 already on left
- //node_r3 already on right
- }
- if(node_l1 != null) node_l1.setParent(node_root);
- if(node_l2 != null) node_l2.setParent(node_root);
- if(node_l3 != null) node_l3.setParent(node_r2);
- if(node_r3 != null) node_r3.setParent(node_r2);
- }
- void rotationLR(AVLNode<K,E> node_root){
- /*
- Pr
- |
- Rt
- / \
- l1 r1
- / \
- l2 r2
- / \
- l3 r3
- */
- AVLNode<K,E> node_parent = node_root.getParent();
- AVLNode<K,E> node_l1 = node_root.getLeft();
- AVLNode<K,E> node_r1 = node_root.getRight();
- AVLNode<K,E> node_l2 = node_r1.getLeft();
- AVLNode<K,E> node_r2 = node_r1.getRight();
- AVLNode<K,E> node_l3 = node_l2.getLeft();
- AVLNode<K,E> node_r3 = node_l2.getRight();
- if(node_root == this.root){
- root = node_l2;
- }
- /*
- Pr
- |
- l2
- / \
- Rt r1
- / \ / \
- l1 l3 r3 r2
- */
- boolean isRootLeftOfParent = false;
- if(node_parent != null && node_parent.getLeft() == node_root){
- isRootLeftOfParent = true;
- }
- if(node_parent != null){
- if(isRootLeftOfParent){
- node_parent.setLeft(node_l2);
- }else{
- node_parent.setRight(node_l2);
- }
- }
- if(node_l2 != null){
- node_l2.setParent(node_parent);
- node_l2.setLeft(node_root);
- node_l2.setRight(node_r1);
- }
- if(node_root != null){
- node_root.setParent(node_l2);
- //node_l1 already on left
- node_root.setRight(node_l3);
- }
- if(node_r1 != null){
- node_r1.setParent(node_l2);
- node_r1.setLeft(node_r3);
- //node_r2 already on right
- }
- if(node_l1 != null) node_l1.setParent(node_root);
- if(node_l3 != null) node_l3.setParent(node_root);
- if(node_r3 != null) node_r3.setParent(node_r1);
- if(node_r2 != null) node_r2.setParent(node_r1);
- }
- void rotationR(AVLNode<K,E> node_root){
- /*
- Pr
- |
- Rt
- / \
- l1 r1
- / \
- l2 r2
- / \
- l3 r3
- */
- AVLNode<K,E> node_parent = node_root.getParent();
- AVLNode<K,E> node_l1 = node_root.getLeft();
- AVLNode<K,E> node_r1 = node_root.getRight();
- AVLNode<K,E> node_l2 = node_l1.getLeft();
- AVLNode<K,E> node_r2 = node_l1.getRight();
- AVLNode<K,E> node_l3 = node_l2.getLeft();
- AVLNode<K,E> node_r3 = node_l2.getRight();
- if(node_root == this.root){
- root = node_l1;
- }
- /*
- Pr
- |
- l1
- / \
- l2 Rt
- / \ / \
- l3 r3 r2 r1
- */
- boolean isRootLeftOfParent = false;
- if(node_parent != null && node_parent.getLeft() == node_root){
- isRootLeftOfParent = true;
- }
- if(node_parent != null){
- if(isRootLeftOfParent){
- node_parent.setLeft(node_l1);
- }else{
- node_parent.setRight(node_l1);
- }
- }
- if(node_l1 != null){
- node_l1.setParent(node_parent);
- node_l1.setLeft(node_l2);
- node_l1.setRight(node_root);
- }
- if(node_l2 != null){
- node_l2.setParent(node_l1);
- //node_l1 already on left
- //node_r3 already on right
- }
- if(node_root != null){
- node_root.setParent(node_l1);
- node_root.setLeft(node_r2);
- //node_r1 already on right
- }
- if(node_l3 != null) node_l3.setParent(node_l2);
- if(node_r3 != null) node_r3.setParent(node_l2);
- if(node_r2 != null) node_r2.setParent(node_root);
- if(node_r1 != null) node_r1.setParent(node_root);
- }
- void rotationRL(AVLNode<K,E> node_root){
- /*
- Pt
- |
- Rt
- / \
- l1 r1
- / \
- l2 r2
- / \
- l3 r3
- */
- AVLNode<K,E> node_parent = node_root.getParent();
- AVLNode<K,E> node_l1 = node_root.getLeft();
- AVLNode<K,E> node_r1 = node_root.getRight();
- AVLNode<K,E> node_l2 = node_l1.getLeft();
- AVLNode<K,E> node_r2 = node_l1.getRight();
- AVLNode<K,E> node_l3 = node_r2.getLeft();
- AVLNode<K,E> node_r3 = node_r2.getRight();
- if(node_root == this.root){
- root = node_r2;
- }
- /*
- Pt
- |
- r2
- / \
- l1 Rt
- / \ / \
- l2 l3 r3 r1
- */
- boolean isRootLeftOfParent = false;
- if(node_parent != null && node_parent.getLeft() == node_root){
- isRootLeftOfParent = true;
- }
- if(node_parent != null){
- if(isRootLeftOfParent){
- node_parent.setLeft(node_r2);
- }else{
- node_parent.setRight(node_r2);
- }
- }
- if(node_r2 != null){
- node_r2.setParent(node_parent);
- node_r2.setLeft(node_l1);
- node_r2.setRight(node_root);
- }
- if(node_l1 != null){
- node_l1.setParent(node_r2);
- //node_l2 already on left
- node_l1.setRight(node_l3);
- }
- if(node_root != null){
- node_root.setParent(node_r2);
- node_root.setLeft(node_r3);
- //node_r1 already on right
- }
- if(node_l2 != null) node_l2.setParent(node_l1);
- if(node_l3 != null) node_l3.setParent(node_l1);
- if(node_r3 != null) node_r3.setParent(node_root);
- if(node_r1 != null) node_r1.setParent(node_root);
- }
- public String toString_InorderDetailed(){
- String r_s = "[ ";
- int count = 0;
- r_s += inorderDetailed(root, count);
- r_s += "]";
- return r_s;
- }
- private String inorderDetailed(AVLNode<K,E> node_ref, int count){
- String r_s = "";
- String dots = "";
- for(int i = 0; i < count; i++){
- dots += '.';
- }
- if(node_ref != null){
- count++;
- r_s += inorderDetailed(node_ref.getLeft(), count);
- if(dots.equals("")){
- r_s += "<" + node_ref.getKey() + "> ";
- }else{
- r_s += dots + node_ref.getKey() + " ";
- }
- r_s += inorderDetailed(node_ref.getRight(), count);
- }
- return r_s;
- }
- public String toString_PreorderDetailed(){
- String r_s = "[ ";
- int count = 0;
- r_s += preorderDetailed(root, count);
- r_s += "]";
- return r_s;
- }
- private String preorderDetailed(AVLNode<K,E> node_ref, int count){
- String r_s = "";
- String dots = "";
- for(int i = 0; i < count; i++){
- dots += '.';
- }
- if(node_ref != null){
- count++;
- if(dots.equals("")){
- r_s += "<" + node_ref.getKey() + "> ";
- }else{
- r_s += dots + node_ref.getKey() + " ";
- }
- r_s += preorderDetailed(node_ref.getLeft(), count);
- r_s += preorderDetailed(node_ref.getRight(), count);
- }
- return r_s;
- }
- public String toString_PostorderDetailed(){
- String r_s = "[ ";
- int count = 0;
- r_s += postorderDetailed(root, count);
- r_s += "]";
- return r_s;
- }
- private String postorderDetailed(AVLNode<K,E> node_ref, int count){
- String r_s = "";
- String dots = "";
- for(int i = 0; i < count; i++){
- dots += '.';
- }
- if(node_ref != null){
- count++;
- r_s += postorderDetailed(node_ref.getLeft(), count);
- r_s += postorderDetailed(node_ref.getRight(), count);
- if(dots.equals("")){
- r_s += "<" + node_ref.getKey() + "> ";
- }else{
- r_s += dots + node_ref.getKey() + " ";
- }
- }
- return r_s;
- }
- public String toString_InorderData(){
- String r_s = "[ ";
- int count = 0;
- r_s += inorderData(root);
- r_s += "]";
- return r_s;
- }
- private String inorderData(AVLNode<K,E> node_ref){
- String r_s = "";
- if(node_ref != null){
- r_s += inorderData(node_ref.getLeft());
- r_s += node_ref.getData() + " ";
- r_s += inorderData(node_ref.getRight());
- }
- return r_s;
- }
- public String toString_InorderDataDetailed(){
- String r_s = "[ ";
- int count = 0;
- r_s += inorderDataDetailed(root, count);
- r_s += "]";
- return r_s;
- }
- private String inorderDataDetailed(AVLNode<K,E> node_ref, int count){
- String r_s = "";
- String dots = "";
- for(int i = 0; i < count; i++){
- dots += '.';
- }
- if(node_ref != null){
- count++;
- r_s += inorderDataDetailed(node_ref.getLeft(), count);
- if(dots.equals("")){
- r_s += "<" + node_ref.getData() + "> ";
- }else{
- r_s += dots + node_ref.getData() + " ";
- }
- r_s += inorderDataDetailed(node_ref.getRight(), count);
- }
- return r_s;
- }
- public String toString_Inorder(){
- String r_s = "[ ";
- r_s += inorder(root);
- r_s += "]";
- return r_s;
- }
- private String inorder(AVLNode<K,E> node_ref){
- String r_s = "";
- if(node_ref != null){
- r_s += inorder(node_ref.getLeft());
- r_s += node_ref.getKey() + " ";
- r_s += inorder(node_ref.getRight());
- }
- return r_s;
- }
- public String toString_Preorder(){
- String r_s = "[ ";
- r_s += preorder(root);
- r_s += "]";
- return r_s;
- }
- private String preorder(AVLNode<K,E> node_ref){
- String r_s = "";
- if(node_ref != null){
- r_s += node_ref.getKey() + " ";
- r_s += preorder(node_ref.getLeft());
- r_s += preorder(node_ref.getRight());
- }
- return r_s;
- }
- public String toString_Postorder(){
- String r_s = "[ ";
- r_s += postorder(root);
- r_s += "]";
- return r_s;
- }
- private String postorder(AVLNode<K,E> node_ref){
- String r_s = "";
- if(node_ref != null){
- r_s += postorder(node_ref.getLeft());
- r_s += postorder(node_ref.getRight());
- r_s += node_ref.getKey() + " ";
- }
- return r_s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement