diff --git a/js/arrayqueue.js b/js/arrayqueue.js deleted file mode 100644 index 41590c1..0000000 --- a/js/arrayqueue.js +++ /dev/null @@ -1,50 +0,0 @@ -class ArrayQueue{ - - constructor(arrayLength) { - this.arrayLength = arrayLength; - this.myArray = new Array(this.arrayLength); - this.headIndex = 0; - this.tailIndex = 0; - this.size = 0; - } - - enqueue(insertObject){ - if (this.checkEnqueueCondition()) { - this.myArray[this.tailIndex] = insertObject; - this.tailIndex = (this.tailIndex + 1) % this.arrayLength; - this.size ++; - } - } - - dequeue(){ - if (this.size !== 0) { - this.myArray[this.headIndex] = null; - this.headIndex = (this.headIndex + 1) % this.arrayLength; - this.size --; - } - } - - printQueue(){ - console.log(this.myArray) - } - - checkEnqueueCondition(){ - return this.size < this.arrayLength - 1; - } -} - - -example = new ArrayQueue(4); -example.enqueue(1); -example.enqueue(2); -example.printQueue(); -example.dequeue(); -example.printQueue(); -example.enqueue(3); -example.enqueue(4); -example.dequeue(); -example.enqueue(5); -example.printQueue(); - - - diff --git a/src/AVLTree.java b/src/AVLTree.java new file mode 100644 index 0000000..0974fd9 --- /dev/null +++ b/src/AVLTree.java @@ -0,0 +1,151 @@ +import java.util.Objects; + +public class AVLTree> extends BinarySearchTree { + + public AVLTree(LinkedList initialLinkedList) { + super(initialLinkedList); + } + + public boolean isValid(){ + boolean b1 = super.isValid(); + boolean b2 = supervise() == null; + return b1 && b2; + } + + public void addData(T data){ + super.addData(data); + Node node = supervise(); + _adjustTree(node); + } + + private void _adjustTree(Node node){ + Node parent = getParent(node); + switch (calculateRotateType(node)){ + case "right": + if(parent == null){ + headNode = _rightRotate(headNode); + }else { + if(parent.leftChild.equals(node)){ + parent.leftChild = _rightRotate(node); + }else{ + parent.rightChild = _rightRotate(node); + } + } + break; + case "leftThenRight": + if(parent == null){ + headNode = _leftThenRightRotate(headNode); + }else { + if(parent.leftChild.equals(node)){ + parent.leftChild = _leftThenRightRotate(node); + }else{ + parent.rightChild = _leftThenRightRotate(node); + } + } + break; + case "left": + if(parent == null){ + headNode = _leftRotate(headNode); + }else { + if(parent.leftChild.equals(node)){ + parent.leftChild = _leftRotate(node); + }else{ + parent.rightChild = _leftRotate(node); + } + } + break; + case "rightThenLeft": + if(parent == null){ + headNode = _rightThenLeftRotate(headNode); + }else { + if(parent.leftChild.equals(node)){ + parent.leftChild = _rightThenLeftRotate(node); + }else{ + parent.rightChild = _rightThenLeftRotate(node); + } + } + break; + } + } + + public void deleteData(T data){ + super.deleteData(data); + Node node = supervise(); + _adjustTree(node); + } + + private String calculateRotateType(Node node){ + if(_getLevels(node.leftChild) > _getLevels(node.rightChild)){ + if(_getLevels(node.leftChild.leftChild) > _getLevels(node.leftChild.rightChild)){ + // If insert into leftchild's left child tree. + return "right"; + }else{ + return "leftThenRight"; + } + }else{ + if(_getLevels(node.rightChild.rightChild) > _getLevels(node.rightChild.leftChild)){ + // If insert into rightchild's right child tree. + return "left"; + }else{ + return "rightThenLeft"; + } + } + } + + private Node _rightRotate(Node head){ + Node newNode = head.leftChild; + head.leftChild = newNode.rightChild; + newNode.rightChild = head; + + return newNode; + } + private Node _leftRotate(Node head){ + Node newNode = head.rightChild; + head.rightChild = newNode.leftChild; + newNode.leftChild = head; + + return newNode; + } + private Node _rightThenLeftRotate(Node head){ + head.rightChild = _rightRotate(head.rightChild); + return _leftRotate(head); + } + private Node _leftThenRightRotate(Node head){ + head.leftChild = _leftRotate(head.leftChild); + return _rightRotate(head); + } + + public Node supervise(){ + return _supervise(headNode); + } + + private Node _supervise(Node head){ + + if(_getLevels(head) <= 2){ + return null; + } + Node result = null; + if(Math.abs(_getLevels(head.leftChild) - _getLevels(head.rightChild)) >= 2){ + result = head; + } + + Node leftResult = _supervise(head.leftChild); + Node rightResult = _supervise(head.rightChild); + if(leftResult != null){ + result = leftResult; + }else if(rightResult != null){ + result = rightResult; + } + return result; + } + + public static void main(String[] args) { + MyArray array = new MyArray<>(11); + array.buildArray(new Integer[]{5,2,-4,null,null,3,null,null,12,9,null,null,21,19,null,null,25,null,null}); + AVLTree tree = new AVLTree<>(new LinkedList<>(array)); + System.out.print(tree.printTree("breadth")); + tree.deleteData(9); + System.out.print(tree.printTree("breadth")); + System.out.print("is valid? " + tree.isValid()); + } +} diff --git a/src/ArrayBinaryTree.java b/src/ArrayBinaryTree.java index 1052101..e423f1c 100644 --- a/src/ArrayBinaryTree.java +++ b/src/ArrayBinaryTree.java @@ -1,5 +1,15 @@ public class ArrayBinaryTree { - private MyArray tree; + protected MyArray tree; + + public int getSize(){ + int count = 0; + for(T ele : tree){ + if(ele != null){ + count++; + } + } + return count; + } public ArrayBinaryTree(MyArray initialArray){ tree = initialArray; @@ -78,6 +88,10 @@ public MyArray breadthFirstTraverse() { return array; } + protected String displayTree() { + return tree.printArray(); + } + public MyArray postOrderTraverse() { MyArray orderedArray = new MyArray<>(tree.getCapacity()); _postOrderTraverse(0, orderedArray); @@ -166,9 +180,23 @@ public boolean isCompleteTree(){ return true; } + protected int getParent(int childIndex){ + if(childIndex<0 || childIndex > tree.getSize()){ + return -2; + } + if(childIndex == 0){ + return -1; + } + if(childIndex % 2 != 0){ + return (childIndex - 1) / 2; + }else { + return (childIndex - 2) / 2; + } + } + public static void main(String[] args) { MyArray array = new MyArray<>(9); - array.buildArray(new Integer[]{1,2,3,4,5,6,7}); + array.buildArray(new Integer[]{1,2,3,4,5,null,7}); ArrayBinaryTree tree = new ArrayBinaryTree<>(array); System.out.print(tree.printTree("pre")); System.out.print(tree.printTree("in")); diff --git a/src/BinaryHeap.java b/src/BinaryHeap.java new file mode 100644 index 0000000..90e563b --- /dev/null +++ b/src/BinaryHeap.java @@ -0,0 +1,140 @@ +public class BinaryHeap> extends ArrayBinaryTree { + + private final boolean minHeap; + public BinaryHeap(MyArray initialArray, boolean type) { + super(initialArray); + minHeap = type; + } + + public boolean isValid(){ + + return isCompleteTree() && _isValid(0, minHeap); + } + + private boolean _isValid(int index, boolean isMinType){ + if(index >= tree.getSize()){ + return false; + } + int leftChildIndex = index * 2 + 1; + int rightChildIndex = index * 2 + 2; + + if((leftChildIndex < tree.getSize() && tree.getData(leftChildIndex) != null) + || (rightChildIndex < tree.getSize() && tree.getData(rightChildIndex) != null)){ + if(leftChildIndex < tree.getSize() && tree.getData(leftChildIndex) != null){ + if((tree.getData(index).compareTo(tree.getData(leftChildIndex)) > 0 && isMinType) + || tree.getData(index).compareTo(tree.getData(leftChildIndex)) < 0 && !isMinType){ + return false; + } + } + if(rightChildIndex < tree.getSize() && tree.getData(rightChildIndex) != null){ + if((tree.getData(index).compareTo(tree.getData(rightChildIndex)) > 0 && isMinType) + || tree.getData(index).compareTo(tree.getData(rightChildIndex)) < 0 && !isMinType){ + return false; + } + } + return _isValid(leftChildIndex, isMinType) && _isValid(rightChildIndex, isMinType); + }else { + return true; + } + } + + private void upAdjust(int index){ + int parentIndex = getParent(index); + while (parentIndex >= 0){ + if((tree.getData(parentIndex).compareTo(tree.getData(index)) > 0 && minHeap) + || tree.getData(parentIndex).compareTo(tree.getData(index)) < 0 && !minHeap){ + + T tmp = tree.getData(index); + tree.update(index, tree.getData(parentIndex)); + tree.update(parentIndex, tmp); + + index = parentIndex; + parentIndex = getParent(index); + continue; + } + break; + } + } + + private void downAdjust(int index){ + int leftChildIndex = 2 * index + 1; + while (leftChildIndex < tree.getSize()){ + int rightChildIndex = leftChildIndex == index - 1 ? leftChildIndex + 1 : -1; + T rightData = rightChildIndex == -1 ? null : tree.getData(rightChildIndex); + T leftData = tree.getData(leftChildIndex); + int childIndex = leftData != null ? leftChildIndex : rightData != null ? rightChildIndex : -1; + if(childIndex == -1){ + break; + } + if((tree.getData(childIndex).compareTo(tree.getData(index)) < 0 && minHeap) + || (tree.getData(childIndex).compareTo(tree.getData(index)) > 0 && !minHeap)){ + T tmp = tree.getData(index); + tree.update(index, tree.getData(childIndex)); + tree.update(childIndex, tmp); + + index = childIndex; + leftChildIndex = 2 * index + 1; + continue; + } + break; + } + } + + public void addData(T data){ + int position = getSize(); + if(getSize() < tree.getSize()){ + tree.update(position, data); + }else{ + tree.insert(data); + } + upAdjust(position); + } + + public void deleteRoot(){ + tree.update(0, tree.getData(getSize() - 1)); + tree.delete(); + downAdjust(0); + } + + public void selfBuild(){ + LinkedListStack stack = new LinkedListStack(null); + for(int i=0;i 0){ + int index = stack.getData(stack.getSize() - 1); + downAdjust(index); + stack.pop(); + } + } + + public static void main(String[] args) { + MyArray array = new MyArray<>(9); + array.buildArray(new Integer[]{6,3,8,2,4,7,9,1,null,null,null,null,null,null,null}); + BinaryHeap tree = new BinaryHeap<>(array, true); + System.out.print(tree.printTree("pre")); + System.out.print(tree.printTree("in")); + System.out.print(tree.printTree("post")); + System.out.print(tree.printTree("breadth")); + System.out.println("levels: " + tree.getLevels()); + System.out.println("is complete: " + tree.isCompleteTree()); + System.out.println("is valid: " + tree.isValid()); + tree.selfBuild(); + tree.addData(0); + System.out.print(tree.displayTree()); + tree.deleteRoot(); + System.out.print(tree.displayTree()); + System.out.println("is valid: " + tree.isValid()); + //tree.downAdjust(3); + //System.out.print(tree.printTree("breadth")); + } +} diff --git a/src/BinarySearchTree.java b/src/BinarySearchTree.java index cf69346..3e048fe 100644 --- a/src/BinarySearchTree.java +++ b/src/BinarySearchTree.java @@ -53,6 +53,9 @@ private Node _searchTree(T data, Node head){ } public void addData(T data){ + if(searchTree(data)!=null){ + return; + } headNode= _addData(data, headNode); } diff --git a/src/LinkedBinaryTree.java b/src/LinkedBinaryTree.java index 9c8bef1..c790ce1 100644 --- a/src/LinkedBinaryTree.java +++ b/src/LinkedBinaryTree.java @@ -1,7 +1,17 @@ public class LinkedBinaryTree { protected Node headNode; - protected int size; + + public int getSize(){ + return _getSize(headNode); + } + + private int _getSize(Node head){ + if(head == null){ + return 0; + } + return 1 + _getSize(head.leftChild) + _getSize(head.rightChild); + } public LinkedBinaryTree(LinkedList initialLinkedList){ headNode = _createTree(initialLinkedList); @@ -19,7 +29,6 @@ private Node _createTree(LinkedList initialLinkedList){ Node node = new Node(); node.data = data; - size ++; node.leftChild = _createTree(initialLinkedList); node.rightChild = _createTree(initialLinkedList); @@ -27,7 +36,7 @@ private Node _createTree(LinkedList initialLinkedList){ } public MyArray preOrderTraverse(){ - MyArray array = new MyArray<>(size); + MyArray array = new MyArray<>(getSize()); _preOrderTraverse(headNode, array); return array; } @@ -42,7 +51,7 @@ private void _preOrderTraverse(Node head, MyArray array){ } public MyArray preOrderTraverse_stack(){ - MyArray array = new MyArray(size); + MyArray array = new MyArray(getSize()); LinkedListStack stack = new LinkedListStack<>(null); stack.push(headNode); while (stack.getSize() > 0){ @@ -82,7 +91,7 @@ public String printTree(String option){ } public MyArray breadthFirstTraverse() { - MyArray array = new MyArray<>(size); + MyArray array = new MyArray<>(getSize()); LinkedListQueue queue = new LinkedListQueue<>(null); queue.push(headNode); @@ -102,7 +111,7 @@ public MyArray breadthFirstTraverse() { } public MyArray inOrderTraverse() { - MyArray array = new MyArray<>(size); + MyArray array = new MyArray<>(getSize()); _inOrderTraverse(headNode, array); return array; } @@ -117,7 +126,7 @@ private void _inOrderTraverse(Node head, MyArray array) { } public MyArray postOrderTraverse() { - MyArray array = new MyArray<>(size); + MyArray array = new MyArray<>(getSize()); _postOrderTraverse(headNode, array); return array; } @@ -135,7 +144,7 @@ public int getLevels(){ return _getLevels(headNode); } - private int _getLevels(Node node) { + protected int _getLevels(Node node) { if(node == null){ return 0; }