sol11 - Solution 1. 1) 25 2) 25 \ 35 3) 25 \ 35 \ 45 4) 35...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Solution 1. 1) 25 2) 25 \ 35 3) 25 \ 35 \ 45 4) 35 R1‐rotation / \ 25 45 5) 35 / \ 25 45 / 20 6) 35 / \ 25 45 / 20 \ 22 7) 35 L / \ 25 45 / 22 / 20 8) 35 LR‐rotation / \ 22 45 / \ 20 25 9) 35 / \ 22 45 / \ 20 25 \ 27 10) 35 L / \ 25 45 / \ 22 27 / 20 11) 25 LR‐rotation / \ 22 35 / / \ 20 27 45 Solution 2 package dataStructures; public class MyBinarySearchTree extends BinarySearchTree{ int isTrue = 0; public boolean check(){ return check(root); } public boolean check(BinaryTreeNode current){ int count = 0; if(count == 0){ if(current.leftChild != null && current.rightChild != null){ if((Integer)((Data)current.leftChild.element).element < (Integer)((Data)current.element).element){ check(current.leftChild); } if((Integer)((Data)current.leftChild.element).element >= (Integer)((Data)current.element).element){ isTrue++; } if((Integer)((Data)current.rightChild.element).element >= (Integer)((Data)current.element).element){ check(current.rightChild); } if((Integer)((Data)current.rightChild.element).element < (Integer)((Data)current.element).element){ isTrue++; } count++; } else if(current.leftChild != null){ if((Integer)((Data)current.leftChild.element).element < (Integer)((Data)current.element).element){ check(current.leftChild); } else{ isTrue++; } } else if(current.rightChild != null){ if((Integer)((Data)current.rightChild.element).element >= (Integer)((Data)current.element).element){ check(current.rightChild); } else{ isTrue++; } } else{ } } if(isTrue == 0){ return true; } else{ return false; } } public void removeLarger(int x){ removeLarger(x, root); } public void removeLarger(int x, BinaryTreeNode current){ if(current.leftChild != null && current.rightChild != null){ if((Integer)((Data)current.element).element > x){ root = current.leftChild; current = null; removeLarger(x, root); } else if((Integer)((Data)current.rightChild.element).element > x){ current.rightChild = current.rightChild.leftChild; } else{ removeLarger(x, current.rightChild); } } else if(current.leftChild != null){ if((Integer)((Data)current.element).element > x){ current = current.leftChild; removeLarger(x, current); } } else if(current.rightChild != null){ if((Integer)((Data)current.element).element > x){ current = null; } else if((Integer)((Data)current.rightChild.element).element > x){ current.rightChild = current.rightChild.leftChild; } else{ removeLarger(x, current.rightChild); } } else{ if((Integer)((Data)current.element).element > x){ current = null; } } } private boolean isLeaf(BinaryTreeNode current){ if((current.leftChild != null)||(current.rightChild != null)) return false; return true; } public void print(){ printR(root,0); } private void printR(BinaryTreeNode current,int level){ String blank = ""; for(int i = 0 ; i < level ; i++){ blank += "\t"; } if(current == null){ blank += "null"; System.out.println(blank); return; } blank += current.toString(); if(!isLeaf(current)){ printR(current.rightChild,level + 1 ); System.out.println(blank); printR(current.leftChild,level + 1 ); } else{ System.out.println(blank); } } public static void main(String args) { MyBinarySearchTree t = new MyBinarySearchTree(); int a = new int[10]; a[0] = 21; a[1] = -72; a[2] = 56; a[3] = 39; a[4] = -29; a[5] = 93; a[6] = 7; a[7] = -19; a[8] = 26; a[9] = 2; for(int i = 0 ; i < 10 ; i++){ t.put(new Integer(a[i]),new Integer(a[i])); } System.out.println("t="); t.print(); System.out.println("t.check() = " + t.check()); System.out.println(); t.root.leftChild.element = new Data(new Integer( -1) ,new Integer(-1)); System.out.println("t="); t.print(); System.out.println("t.check() = " + t.check()); System.out.println(); System.out.println("t="); t.root.leftChild.element = new Data(new Integer( -72) ,new Integer(-72)); t.removeLarger(5); t.print(); } } ...
View Full Document

Ask a homework question - tutors are online