Skip to content

Commit

Permalink
Merge pull request #68 from junnengsoo/branch-binarySearchTree
Browse files Browse the repository at this point in the history
style: Resolve stylecheck
  • Loading branch information
4ndrelim authored Jan 23, 2024
2 parents 2a0bfb0 + 30dedb9 commit 8a0d54a
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 114 deletions.
227 changes: 114 additions & 113 deletions src/main/java/dataStructures/binarySearchTree/BinarySearchTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ private void insert(Node<T, V> node, T key, V value) {
}
}

/**
* Inserts a key into the tree
*
* @param key to be inserted
*/
public void insert(T key, V value) {
if (root == null) {
root = new Node<>(key, value);
} else {
insert(root, key, value);
}
}

/**
* Delete a key from the binary search tree rooted at a specified node.
* Find the node that holds the key and remove the node from the tree.
Expand Down Expand Up @@ -102,6 +115,15 @@ private Node<T, V> delete(Node<T, V> node, T key) {
return node;
}

/**
* Removes a key from the tree, if it exists
*
* @param key to be removed
*/
public void delete(T key) {
root = delete(root, key);
}

/**
* Find the node with the minimum key in the tree rooted at a specified node.
*
Expand All @@ -117,6 +139,19 @@ private Node<T, V> searchMin(Node<T, V> n) {
}
}

/**
* Search for the minimum key in the tree.
*
* @return node with the minimum key; null if tree is empty
*/
public Node<T, V> searchMin() {
if (root == null) {
return null;
} else {
return searchMin(root);
}
}

/**
* Find the node with the maximum key in the tree rooted at a specified node.
*
Expand All @@ -132,6 +167,19 @@ private Node<T, V> searchMax(Node<T, V> n) {
}
}

/**
* Search for the maximum key in the tree.
*
* @return node with the maximum key; null if tree is empty
*/
public Node<T, V> searchMax() {
if (root == null) {
return null;
} else {
return searchMax(root);
}
}

/**
* Find the key of the predecessor of a specified node that exists in the tree
* NOTE: the input node is assumed to be in the tree
Expand All @@ -154,6 +202,27 @@ private T predecessor(Node<T, V> node) {
return null;
}

/**
* Search for the predecessor of a given key.
*
* @param key find predecessor of this key
* @return generic type value; null if key has no predecessor
*/
public T predecessor(T key) {
Node<T, V> curr = root;
while (curr != null) {
if (curr.getKey().compareTo(key) == 0) {
break;
} else if (curr.getKey().compareTo(key) < 0) {
curr = curr.getRight();
} else {
curr = curr.getLeft();
}
}

return predecessor(curr);
}

/**
* Find the key of the successor of a specified node that exists in the tree
* NOTE: the input node is assumed to be in the tree
Expand All @@ -176,6 +245,27 @@ private T successor(Node<T, V> node) {
return null;
}

/**
* Search for the successor of a given key.
*
* @param key find successor of this key
* @return generic type value; null if key has no successor
*/
public T successor(T key) {
Node<T, V> curr = root;
while (curr != null) {
if (curr.getKey().compareTo(key) == 0) {
break;
} else if (curr.getKey().compareTo(key) < 0) {
curr = curr.getRight();
} else {
curr = curr.getLeft();
}
}

return successor(curr);
}

/**
* Stores in-order traversal of tree rooted at node into a list
*
Expand All @@ -197,6 +287,12 @@ private void getInorder(Node<T, V> node, List<String> result) {
}
}

public List<String> getInorder() {
List<String> result = new ArrayList<>();
getInorder(root, result);
return result;
}

/**
* Stores in-order traversal of tree rooted at node into a list
*
Expand All @@ -218,6 +314,12 @@ private void getPreorder(Node<T, V> node, List<String> result) {
}
}

public List<String> getPreorder() {
List<String> result = new ArrayList<>();
getPreorder(root, result);
return result;
}

/**
* Stores post-order traversal of tree rooted at node into a list
*
Expand All @@ -239,6 +341,12 @@ private void getPostorder(Node<T, V> node, List<String> result) {
result.add(node.toString());
}

public List<String> getPostorder() {
List<String> result = new ArrayList<>();
getPostorder(root, result);
return result;
}

/**
* Stores level-order traversal of tree rooted at node into a list
*
Expand All @@ -264,6 +372,12 @@ private void getLevelorder(Node<T, V> node, List<String> result) {
}
}

public List<String> getLevelorder() {
List<String> result = new ArrayList<>();
getLevelorder(root, result);
return result;
}

/**
* Get root of tree.
*
Expand All @@ -273,28 +387,6 @@ public Node<T, V> root() {
return root;
}

/**
* Inserts a key into the tree
*
* @param key to be inserted
*/
public void insert(T key, V value) {
if (root == null) {
root = new Node<>(key, value);
} else {
insert(root, key, value);
}
}

/**
* Removes a key from the tree, if it exists
*
* @param key to be removed
*/
public void delete(T key) {
root = delete(root, key);
}

/**
* Search for a node with the specified key.
*
Expand All @@ -315,95 +407,4 @@ public Node<T, V> search(T key) {
return null;
}

/**
* Search for the predecessor of a given key.
*
* @param key find predecessor of this key
* @return generic type value; null if key has no predecessor
*/
public T predecessor(T key) {
Node<T, V> curr = root;
while (curr != null) {
if (curr.getKey().compareTo(key) == 0) {
break;
} else if (curr.getKey().compareTo(key) < 0) {
curr = curr.getRight();
} else {
curr = curr.getLeft();
}
}

return predecessor(curr);
}

/**
* Search for the successor of a given key.
*
* @param key find successor of this key
* @return generic type value; null if key has no successor
*/
public T successor(T key) {
Node<T, V> curr = root;
while (curr != null) {
if (curr.getKey().compareTo(key) == 0) {
break;
} else if (curr.getKey().compareTo(key) < 0) {
curr = curr.getRight();
} else {
curr = curr.getLeft();
}
}

return successor(curr);
}

/**
* Search for the minimum key in the tree.
*
* @return node with the minimum key; null if tree is empty
*/
public Node<T, V> searchMin() {
if (root == null) {
return null;
} else {
return searchMin(root);
}
}

/**
* Search for the maximum key in the tree.
*
* @return node with the maximum key; null if tree is empty
*/
public Node<T, V> searchMax() {
if (root == null) {
return null;
} else {
return searchMax(root);
}
}

public List<String> getInorder() {
List<String> result = new ArrayList<>();
getInorder(root, result);
return result;
}

public List<String> getPreorder() {
List<String> result = new ArrayList<>();
getPreorder(root, result);
return result;
}

public List<String> getPostorder() {
List<String> result = new ArrayList<>();
getPostorder(root, result);
return result;
}

public List<String> getLevelorder() {
List<String> result = new ArrayList<>();
getLevelorder(root, result);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dataStructures.disjointSet.weightedUnion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
* Implementation of weighted-union structure;
Expand Down

0 comments on commit 8a0d54a

Please sign in to comment.