From 30dedb9a86a29fc60d5ea10554eeb6d97730f6c1 Mon Sep 17 00:00:00 2001 From: junnengsoo Date: Tue, 23 Jan 2024 09:35:47 +0800 Subject: [PATCH] Resolve stylecheck --- .../binarySearchTree/BinarySearchTree.java | 227 +++++++++--------- .../weightedUnion/DisjointSet.java | 2 +- 2 files changed, 115 insertions(+), 114 deletions(-) diff --git a/src/main/java/dataStructures/binarySearchTree/BinarySearchTree.java b/src/main/java/dataStructures/binarySearchTree/BinarySearchTree.java index 013a91ab..5466d0f0 100644 --- a/src/main/java/dataStructures/binarySearchTree/BinarySearchTree.java +++ b/src/main/java/dataStructures/binarySearchTree/BinarySearchTree.java @@ -53,6 +53,19 @@ private void insert(Node 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. @@ -102,6 +115,15 @@ private Node delete(Node 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. * @@ -117,6 +139,19 @@ private Node searchMin(Node n) { } } + /** + * Search for the minimum key in the tree. + * + * @return node with the minimum key; null if tree is empty + */ + public Node 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. * @@ -132,6 +167,19 @@ private Node searchMax(Node n) { } } + /** + * Search for the maximum key in the tree. + * + * @return node with the maximum key; null if tree is empty + */ + public Node 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 @@ -154,6 +202,27 @@ private T predecessor(Node 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 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 @@ -176,6 +245,27 @@ private T successor(Node 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 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 * @@ -197,6 +287,12 @@ private void getInorder(Node node, List result) { } } + public List getInorder() { + List result = new ArrayList<>(); + getInorder(root, result); + return result; + } + /** * Stores in-order traversal of tree rooted at node into a list * @@ -218,6 +314,12 @@ private void getPreorder(Node node, List result) { } } + public List getPreorder() { + List result = new ArrayList<>(); + getPreorder(root, result); + return result; + } + /** * Stores post-order traversal of tree rooted at node into a list * @@ -239,6 +341,12 @@ private void getPostorder(Node node, List result) { result.add(node.toString()); } + public List getPostorder() { + List result = new ArrayList<>(); + getPostorder(root, result); + return result; + } + /** * Stores level-order traversal of tree rooted at node into a list * @@ -264,6 +372,12 @@ private void getLevelorder(Node node, List result) { } } + public List getLevelorder() { + List result = new ArrayList<>(); + getLevelorder(root, result); + return result; + } + /** * Get root of tree. * @@ -273,28 +387,6 @@ public Node 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. * @@ -315,95 +407,4 @@ public Node 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 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 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 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 searchMax() { - if (root == null) { - return null; - } else { - return searchMax(root); - } - } - - public List getInorder() { - List result = new ArrayList<>(); - getInorder(root, result); - return result; - } - - public List getPreorder() { - List result = new ArrayList<>(); - getPreorder(root, result); - return result; - } - - public List getPostorder() { - List result = new ArrayList<>(); - getPostorder(root, result); - return result; - } - - public List getLevelorder() { - List result = new ArrayList<>(); - getLevelorder(root, result); - return result; - } } diff --git a/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java b/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java index 929028da..6a2d47ed 100644 --- a/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java +++ b/src/main/java/dataStructures/disjointSet/weightedUnion/DisjointSet.java @@ -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;