mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Fix formatting (#4259)
Co-authored-by: Ranjeet Kumar Jena <ranjeetjena06@gmai.com> Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:

committed by
GitHub

parent
de50fc0294
commit
cc9afea036
@ -10,10 +10,10 @@ import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
|||||||
* <p>
|
* <p>
|
||||||
* An implementation of BST iteratively. Binary Search Tree is a binary tree
|
* An implementation of BST iteratively. Binary Search Tree is a binary tree
|
||||||
* which satisfies three properties: left child is less than root node, right
|
* which satisfies three properties: left child is less than root node, right
|
||||||
* child is grater than root node, both left and right childs must themselves be
|
* child is grater than root node, both left and right child must themselves be
|
||||||
* a BST.
|
* a BST.
|
||||||
*
|
*
|
||||||
* @author [Lakhan Nad](https://github.com/Lakhan-Nad)
|
* @author [Lakhan Nad](<a href="https://github.com/Lakhan-Nad">git-Lakhan Nad</a>)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class BSTIterative {
|
public class BSTIterative {
|
||||||
|
@ -17,7 +17,7 @@ import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
|||||||
* I have made public functions as methods and to actually implement recursive
|
* I have made public functions as methods and to actually implement recursive
|
||||||
* approach I have used private methods
|
* approach I have used private methods
|
||||||
*
|
*
|
||||||
* @author [Lakhan Nad](https://github.com/Lakhan-Nad)
|
* @author [Lakhan Nad](<a href="https://github.com/Lakhan-Nad">git-Lakhan Nad</a>)
|
||||||
*/
|
*/
|
||||||
public class BSTRecursive {
|
public class BSTRecursive {
|
||||||
|
|
||||||
|
@ -9,10 +9,10 @@ import java.util.List;
|
|||||||
* <p>
|
* <p>
|
||||||
* A recursive implementation of generic type BST.
|
* A recursive implementation of generic type BST.
|
||||||
*
|
*
|
||||||
* Reference: https://en.wikipedia.org/wiki/Binary_search_tree
|
* Reference: <a href="https://en.wikipedia.org/wiki/Binary_search_tree">Wiki links for BST</a>
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author [Madhur Panwar](https://github.com/mdrpanwar)
|
* @author [Madhur Panwar](<a href="https://github.com/mdrpanwar">git-Madhur Panwar</a>)
|
||||||
*/
|
*/
|
||||||
public class BSTRecursiveGeneric<T extends Comparable<T>> {
|
public class BSTRecursiveGeneric<T extends Comparable<T>> {
|
||||||
|
|
||||||
@ -219,7 +219,7 @@ public class BSTRecursiveGeneric<T extends Comparable<T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serach recursively if the given value is present in BST or not.
|
* Search recursively if the given value is present in BST or not.
|
||||||
*
|
*
|
||||||
* @param node the node under which to check
|
* @param node the node under which to check
|
||||||
* @param data the value to be checked
|
* @param data the value to be checked
|
||||||
|
@ -39,7 +39,7 @@ import com.thealgorithms.datastructures.trees.BinaryTree.Node;
|
|||||||
* ii) If key is lesser than root value than ceil will be in right subtree so
|
* ii) If key is lesser than root value than ceil will be in right subtree so
|
||||||
* call recursively on right subtree iii) if key is greater than current root,
|
* call recursively on right subtree iii) if key is greater than current root,
|
||||||
* then either a) the root is ceil b) ceil is in left subtree: call for left
|
* then either a) the root is ceil b) ceil is in left subtree: call for left
|
||||||
* subtree. If left subtree returns a non null value then that will be ceil
|
* subtree. If left subtree returns a non-null value then that will be ceil
|
||||||
* otherwise the root is ceil
|
* otherwise the root is ceil
|
||||||
*/
|
*/
|
||||||
public class CeilInBinarySearchTree {
|
public class CeilInBinarySearchTree {
|
||||||
|
@ -12,7 +12,7 @@ import java.util.Stack;
|
|||||||
* `isBalancedRecursive()` is implemented in a recursive fashion, and
|
* `isBalancedRecursive()` is implemented in a recursive fashion, and
|
||||||
* `isBalancedIterative()` is implemented in an iterative fashion.
|
* `isBalancedIterative()` is implemented in an iterative fashion.
|
||||||
*
|
*
|
||||||
* @author [Ian Cowan](https://github.com/iccowan)
|
* @author [Ian Cowan](<a href="https://github.com/iccowan">Git-Ian Cowan</a>)
|
||||||
*/
|
*/
|
||||||
public class CheckIfBinaryTreeBalanced {
|
public class CheckIfBinaryTreeBalanced {
|
||||||
/**
|
/**
|
||||||
|
@ -39,16 +39,16 @@ public class CheckTreeIsSymmetric {
|
|||||||
return isSymmetric(root.left, root.right);
|
return isSymmetric(root.left, root.right);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSymmetric(Node leftSubtreeRoot, Node rightSubtreRoot) {
|
private static boolean isSymmetric(Node leftSubtreeRoot, Node rightSubtreeRoot) {
|
||||||
if (leftSubtreeRoot == null && rightSubtreRoot == null) {
|
if (leftSubtreeRoot == null && rightSubtreeRoot == null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isInvalidSubtree(leftSubtreeRoot, rightSubtreRoot)) {
|
if (isInvalidSubtree(leftSubtreeRoot, rightSubtreeRoot)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
|
return isSymmetric(leftSubtreeRoot.right, rightSubtreeRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreeRoot.right);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isInvalidSubtree(Node leftSubtreeRoot, Node rightSubtreeRoot) {
|
private static boolean isInvalidSubtree(Node leftSubtreeRoot, Node rightSubtreeRoot) {
|
||||||
|
@ -16,32 +16,32 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
public class GenericTree {
|
public class GenericTree {
|
||||||
|
|
||||||
private class Node {
|
private static class Node {
|
||||||
|
|
||||||
int data;
|
int data;
|
||||||
ArrayList<Node> child = new ArrayList<>();
|
ArrayList<Node> child = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node root;
|
private final Node root;
|
||||||
public GenericTree() { // Constructor
|
public GenericTree() { // Constructor
|
||||||
Scanner scn = new Scanner(System.in);
|
Scanner scn = new Scanner(System.in);
|
||||||
root = create_treeG(null, 0, scn);
|
root = create_treeG(null, 0, scn);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node create_treeG(Node node, int childindx, Scanner scn) {
|
private Node create_treeG(Node node, int childIndex, Scanner scanner) {
|
||||||
// display
|
// display
|
||||||
if (node == null) {
|
if (node == null) {
|
||||||
System.out.println("Enter root's data");
|
System.out.println("Enter root's data");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Enter data of parent of index " + node.data + " " + childindx);
|
System.out.println("Enter data of parent of index " + node.data + " " + childIndex);
|
||||||
}
|
}
|
||||||
// input
|
// input
|
||||||
node = new Node();
|
node = new Node();
|
||||||
node.data = scn.nextInt();
|
node.data = scanner.nextInt();
|
||||||
System.out.println("number of children");
|
System.out.println("number of children");
|
||||||
int number = scn.nextInt();
|
int number = scanner.nextInt();
|
||||||
for (int i = 0; i < number; i++) {
|
for (int i = 0; i < number; i++) {
|
||||||
Node child = create_treeG(node, i, scn);
|
Node child = create_treeG(node, i, scanner);
|
||||||
node.child.add(child);
|
node.child.add(child);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
|
@ -5,7 +5,7 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class LCA {
|
public class LCA {
|
||||||
|
|
||||||
private static Scanner scanner = new Scanner(System.in);
|
private static final Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// The adjacency list representation of a tree:
|
// The adjacency list representation of a tree:
|
||||||
@ -91,7 +91,7 @@ public class LCA {
|
|||||||
return v1;
|
return v1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/*
|
||||||
* Input:
|
* Input:
|
||||||
* 10
|
* 10
|
||||||
* 0 1
|
* 0 1
|
||||||
|
Reference in New Issue
Block a user