docs: update AnyBaseToAnyBase and GenericTree
This commit is contained in:
parent
36dc276f29
commit
b1d4be7f86
@ -15,15 +15,16 @@ import java.util.Scanner;
|
|||||||
*/
|
*/
|
||||||
public class AnyBaseToAnyBase {
|
public class AnyBaseToAnyBase {
|
||||||
|
|
||||||
// Smallest and largest base you want to accept as valid input
|
/**
|
||||||
|
* Smallest and largest base you want to accept as valid input
|
||||||
|
*/
|
||||||
static final int MINIMUM_BASE = 2;
|
static final int MINIMUM_BASE = 2;
|
||||||
static final int MAXIMUM_BASE = 36;
|
static final int MAXIMUM_BASE = 36;
|
||||||
|
|
||||||
// Driver
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner in = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
String n;
|
String n;
|
||||||
int b1 = 0, b2 = 0;
|
int b1, b2;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
System.out.print("Enter number: ");
|
System.out.print("Enter number: ");
|
||||||
@ -64,7 +65,7 @@ public class AnyBaseToAnyBase {
|
|||||||
char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base);
|
char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base);
|
||||||
|
|
||||||
// Convert character array into set for convenience of contains() method
|
// Convert character array into set for convenience of contains() method
|
||||||
HashSet<Character> digitsList = new HashSet();
|
HashSet<Character> digitsList = new HashSet<>();
|
||||||
for (int i = 0; i < digitsForBase.length; i++)
|
for (int i = 0; i < digitsForBase.length; i++)
|
||||||
digitsList.add(digitsForBase[i]);
|
digitsList.add(digitsForBase[i]);
|
||||||
|
|
||||||
|
@ -4,226 +4,231 @@ import java.util.ArrayList;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A generic tree is a tree which can have as many children as it can be
|
||||||
|
* It might be possible that every node present is directly connected to
|
||||||
|
* root node.
|
||||||
|
* <p>
|
||||||
|
* In this code
|
||||||
|
* Every function has two copies: one function is helper function which can be called from
|
||||||
|
* main and from that function a private function is called which will do the actual work.
|
||||||
|
* I have done this, while calling from main one have to give minimum parameters.
|
||||||
|
*/
|
||||||
public class GenericTree {
|
public class GenericTree {
|
||||||
private class Node {
|
private class Node {
|
||||||
int data;
|
int data;
|
||||||
ArrayList<Node> child = new ArrayList<>();
|
ArrayList<Node> child = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node root;
|
private Node root;
|
||||||
private int size;
|
private int size;
|
||||||
|
|
||||||
/*
|
public GenericTree() { //Constructor
|
||||||
A generic tree is a tree which can have as many children as it can be
|
Scanner scn = new Scanner(System.in);
|
||||||
It might be possible that every node present is directly connected to
|
root = create_treeG(null, 0, scn);
|
||||||
root node.
|
}
|
||||||
|
|
||||||
In this code
|
private Node create_treeG(Node node, int childindx, Scanner scn) {
|
||||||
Every function has two copies: one function is helper function which can be called from
|
// display
|
||||||
main and from that function a private function is called which will do the actual work.
|
if (node == null) {
|
||||||
I have done this, while calling from main one have to give minimum parameters.
|
System.out.println("Enter root's data");
|
||||||
|
} else {
|
||||||
|
System.out.println("Enter data of parent of index " + node.data + " " + childindx);
|
||||||
|
}
|
||||||
|
// input
|
||||||
|
node = new Node();
|
||||||
|
node.data = scn.nextInt();
|
||||||
|
System.out.println("number of children");
|
||||||
|
int number = scn.nextInt();
|
||||||
|
for (int i = 0; i < number; i++) {
|
||||||
|
Node child = create_treeG(node, i, scn);
|
||||||
|
size++;
|
||||||
|
node.child.add(child);
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
*/
|
/**
|
||||||
public GenericTree() { //Constructor
|
* Function to display the generic tree
|
||||||
Scanner scn = new Scanner(System.in);
|
*/
|
||||||
root = create_treeG(null, 0, scn);
|
public void display() { //Helper function
|
||||||
}
|
display_1(root);
|
||||||
|
}
|
||||||
|
|
||||||
private Node create_treeG(Node node, int childindx, Scanner scn) {
|
private void display_1(Node parent) {
|
||||||
// display
|
System.out.print(parent.data + "=>");
|
||||||
if (node == null) {
|
for (int i = 0; i < parent.child.size(); i++) {
|
||||||
System.out.println("Enter root's data");
|
System.out.print(parent.child.get(i).data + " ");
|
||||||
} else {
|
}
|
||||||
System.out.println("Enter data of parent of index " + node.data + " " + childindx);
|
System.out.println(".");
|
||||||
}
|
for (int i = 0; i < parent.child.size(); i++) {
|
||||||
// input
|
display_1(parent.child.get(i));
|
||||||
node = new Node();
|
}
|
||||||
node.data = scn.nextInt();
|
}
|
||||||
System.out.println("number of children");
|
|
||||||
int number = scn.nextInt();
|
|
||||||
for (int i = 0; i < number; i++) {
|
|
||||||
Node childd = create_treeG(node, i, scn);
|
|
||||||
size++;
|
|
||||||
node.child.add(childd);
|
|
||||||
}
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Function to display the generic tree
|
* One call store the size directly but if you are asked compute size this function to calculate
|
||||||
*/
|
* size goes as follows
|
||||||
public void display() { //Helper function
|
*
|
||||||
display_1(root);
|
* @return size
|
||||||
return;
|
*/
|
||||||
}
|
public int size2call() {
|
||||||
|
return size2(root);
|
||||||
|
}
|
||||||
|
|
||||||
private void display_1(Node parent) {
|
public int size2(Node roott) {
|
||||||
System.out.print(parent.data + "=>");
|
int sz = 0;
|
||||||
for (int i = 0; i < parent.child.size(); i++) {
|
for (int i = 0; i < roott.child.size(); i++) {
|
||||||
System.out.print(parent.child.get(i).data + " ");
|
sz += size2(roott.child.get(i));
|
||||||
}
|
}
|
||||||
System.out.println(".");
|
return sz + 1;
|
||||||
for (int i = 0; i < parent.child.size(); i++) {
|
}
|
||||||
display_1(parent.child.get(i));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
One call store the size directly but if you are asked compute size this function to calcuate
|
* Function to compute maximum value in the generic tree
|
||||||
size goes as follows
|
*
|
||||||
*/
|
* @return maximum value
|
||||||
|
*/
|
||||||
|
public int maxcall() {
|
||||||
|
int maxi = root.data;
|
||||||
|
return max(root, maxi);
|
||||||
|
}
|
||||||
|
|
||||||
public int size2call() {
|
private int max(Node roott, int maxi) {
|
||||||
return size2(root);
|
if (maxi < roott.data)
|
||||||
}
|
maxi = roott.data;
|
||||||
|
for (int i = 0; i < roott.child.size(); i++) {
|
||||||
|
maxi = max(roott.child.get(i), maxi);
|
||||||
|
}
|
||||||
|
|
||||||
public int size2(Node roott) {
|
return maxi;
|
||||||
int sz = 0;
|
}
|
||||||
for (int i = 0; i < roott.child.size(); i++) {
|
|
||||||
sz += size2(roott.child.get(i));
|
|
||||||
}
|
|
||||||
return sz + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Function to compute maximum value in the generic tree
|
* Function to compute HEIGHT of the generic tree
|
||||||
*/
|
*
|
||||||
public int maxcall() {
|
* @return height
|
||||||
int maxi = root.data;
|
*/
|
||||||
return max(root, maxi);
|
public int heightcall() {
|
||||||
}
|
return height(root) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
private int max(Node roott, int maxi) {
|
private int height(Node node) {
|
||||||
if (maxi < roott.data)
|
int h = 0;
|
||||||
maxi = roott.data;
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
for (int i = 0; i < roott.child.size(); i++) {
|
int k = height(node.child.get(i));
|
||||||
maxi = max(roott.child.get(i), maxi);
|
if (k > h)
|
||||||
}
|
h = k;
|
||||||
|
}
|
||||||
|
return h + 1;
|
||||||
|
}
|
||||||
|
|
||||||
return maxi;
|
/**
|
||||||
}
|
* Function to find whether a number is present in the generic tree or not
|
||||||
|
*
|
||||||
|
* @param info number
|
||||||
|
* @return present or not
|
||||||
|
*/
|
||||||
|
public boolean findcall(int info) {
|
||||||
|
return find(root, info);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
private boolean find(Node node, int info) {
|
||||||
Function to compute HEIGHT of the generic tree
|
if (node.data == info)
|
||||||
*/
|
return true;
|
||||||
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
|
if (find(node.child.get(i), info))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public int heightcall() {
|
|
||||||
return height(root) - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int height(Node node) {
|
/**
|
||||||
int h = 0;
|
* Function to calculate depth of generic tree
|
||||||
for (int i = 0; i < node.child.size(); i++) {
|
*
|
||||||
int k = height(node.child.get(i));
|
* @param dep depth
|
||||||
if (k > h)
|
*/
|
||||||
h = k;
|
public void depthcaller(int dep) {
|
||||||
}
|
depth(root, dep);
|
||||||
return h + 1;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
public void depth(Node node, int dep) {
|
||||||
Function to find whether a number is present in the generic tree or not
|
if (dep == 0) {
|
||||||
*/
|
System.out.println(node.data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < node.child.size(); i++)
|
||||||
|
depth(node.child.get(i), dep - 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean findcall(int info) {
|
/**
|
||||||
return find(root, info);
|
* Function to print generic tree in pre-order
|
||||||
}
|
*/
|
||||||
|
public void preordercall() {
|
||||||
|
preorder(root);
|
||||||
|
System.out.println(".");
|
||||||
|
}
|
||||||
|
|
||||||
private boolean find(Node node, int info) {
|
private void preorder(Node node) {
|
||||||
if (node.data == info)
|
System.out.print(node.data + " ");
|
||||||
return true;
|
for (int i = 0; i < node.child.size(); i++)
|
||||||
for (int i = 0; i < node.child.size(); i++) {
|
preorder(node.child.get(i));
|
||||||
if (find(node.child.get(i), info))
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Function to calculate depth of generic tree
|
* Function to print generic tree in post-order
|
||||||
*/
|
*/
|
||||||
public void depthcaller(int dep) {
|
public void postordercall() {
|
||||||
depth(root, dep);
|
postorder(root);
|
||||||
}
|
System.out.println(".");
|
||||||
|
}
|
||||||
|
|
||||||
public void depth(Node node, int dep) {
|
private void postorder(Node node) {
|
||||||
if (dep == 0) {
|
for (int i = 0; i < node.child.size(); i++)
|
||||||
System.out.println(node.data);
|
postorder(node.child.get(i));
|
||||||
return;
|
System.out.print(node.data + " ");
|
||||||
}
|
}
|
||||||
for (int i = 0; i < node.child.size(); i++)
|
|
||||||
depth(node.child.get(i), dep - 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Function to print generic tree in pre-order
|
* Function to print generic tree in level-order
|
||||||
*/
|
*/
|
||||||
public void preordercall() {
|
public void levelorder() {
|
||||||
preorder(root);
|
LinkedList<Node> q = new LinkedList<>();
|
||||||
System.out.println(".");
|
q.addLast(root);
|
||||||
}
|
while (!q.isEmpty()) {
|
||||||
|
int k = q.getFirst().data;
|
||||||
|
System.out.print(k + " ");
|
||||||
|
|
||||||
private void preorder(Node node) {
|
for (int i = 0; i < q.getFirst().child.size(); i++) {
|
||||||
System.out.print(node.data + " ");
|
q.addLast(q.getFirst().child.get(i));
|
||||||
for (int i = 0; i < node.child.size(); i++)
|
}
|
||||||
preorder(node.child.get(i));
|
q.removeFirst();
|
||||||
}
|
}
|
||||||
|
System.out.println(".");
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Function to print generic tree in post-order
|
* Function to remove all leaves of generic tree
|
||||||
*/
|
*/
|
||||||
public void postordercall() {
|
public void removeleavescall() {
|
||||||
postorder(root);
|
removeleaves(root);
|
||||||
System.out.println(".");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void postorder(Node node) {
|
private void removeleaves(Node node) {
|
||||||
for (int i = 0; i < node.child.size(); i++)
|
ArrayList<Integer> arr = new ArrayList<>();
|
||||||
postorder(node.child.get(i));
|
for (int i = 0; i < node.child.size(); i++) {
|
||||||
System.out.print(node.data + " ");
|
if (node.child.get(i).child.size() == 0) {
|
||||||
}
|
arr.add(i);
|
||||||
|
// node.child.remove(i);
|
||||||
/*
|
// i--;
|
||||||
Function to print generic tree in level-order
|
} else
|
||||||
*/
|
removeleaves(node.child.get(i));
|
||||||
|
}
|
||||||
public void levelorder() {
|
for (int i = arr.size() - 1; i >= 0; i--) {
|
||||||
LinkedList<Node> q = new LinkedList<>();
|
node.child.remove(arr.get(i) + 0);
|
||||||
q.addLast(root);
|
}
|
||||||
while (!q.isEmpty()) {
|
}
|
||||||
int k = q.getFirst().data;
|
|
||||||
System.out.print(k + " ");
|
|
||||||
|
|
||||||
for (int i = 0; i < q.getFirst().child.size(); i++) {
|
|
||||||
q.addLast(q.getFirst().child.get(i));
|
|
||||||
}
|
|
||||||
q.removeFirst();
|
|
||||||
}
|
|
||||||
System.out.println(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function to remove all leaves of generic tree
|
|
||||||
*/
|
|
||||||
public void removeleavescall() {
|
|
||||||
removeleaves(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeleaves(Node node) {
|
|
||||||
ArrayList<Integer> arr = new ArrayList<>();
|
|
||||||
for (int i = 0; i < node.child.size(); i++) {
|
|
||||||
if (node.child.get(i).child.size() == 0) {
|
|
||||||
arr.add(i);
|
|
||||||
// node.child.remove(i);
|
|
||||||
// i--;
|
|
||||||
} else
|
|
||||||
removeleaves(node.child.get(i));
|
|
||||||
}
|
|
||||||
for (int i = arr.size() - 1; i >= 0; i--) {
|
|
||||||
node.child.remove(arr.get(i) + 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,92 +0,0 @@
|
|||||||
package Sorts;
|
|
||||||
|
|
||||||
import static Sorts.SortUtils.less;
|
|
||||||
import static Sorts.SortUtils.print;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Podshivalov Nikita (https://github.com/nikitap492)
|
|
||||||
* @see SortAlgorithm
|
|
||||||
*/
|
|
||||||
public class BinaryTreeSort implements SortAlgorithm {
|
|
||||||
|
|
||||||
interface TreeVisitor<T extends Comparable<T>> {
|
|
||||||
void visit(Node<T> node);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class SortVisitor<T extends Comparable<T>> implements TreeVisitor<T> {
|
|
||||||
|
|
||||||
private final T[] array;
|
|
||||||
private int counter;
|
|
||||||
|
|
||||||
SortVisitor(T[] array) {
|
|
||||||
this.array = array;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(Node<T> node) {
|
|
||||||
array[counter++] = node.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class Node<T extends Comparable<T>> {
|
|
||||||
private T value;
|
|
||||||
private Node<T> left;
|
|
||||||
private Node<T> right;
|
|
||||||
|
|
||||||
Node(T value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void insert(Node<T> node) {
|
|
||||||
if (less(node.value, value)) {
|
|
||||||
if (left != null) left.insert(node);
|
|
||||||
else left = node;
|
|
||||||
} else {
|
|
||||||
if (right != null) right.insert(node);
|
|
||||||
else right = node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void traverse(TreeVisitor<T> visitor) {
|
|
||||||
if (left != null)
|
|
||||||
left.traverse(visitor);
|
|
||||||
|
|
||||||
visitor.visit(this);
|
|
||||||
|
|
||||||
if (right != null)
|
|
||||||
right.traverse(visitor);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T extends Comparable<T>> T[] sort(T[] array) {
|
|
||||||
|
|
||||||
Node<T> root = new Node<>(array[0]);
|
|
||||||
for (int i = 1; i < array.length; i++) {
|
|
||||||
root.insert(new Node<>(array[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
root.traverse(new SortVisitor<>(array));
|
|
||||||
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void main(String args[]) {
|
|
||||||
|
|
||||||
Integer[] intArray = {12, 40, 9, 3, 19, 74, 7, 31, 23, 54, 26, 81, 12};
|
|
||||||
BinaryTreeSort treeSort = new BinaryTreeSort();
|
|
||||||
Integer[] sorted = treeSort.sort(intArray);
|
|
||||||
print(sorted);
|
|
||||||
|
|
||||||
Double[] decimalArray = {8.2, 1.5, 3.14159265, 9.3, 5.1, 4.8, 2.6};
|
|
||||||
print(treeSort.sort(decimalArray));
|
|
||||||
|
|
||||||
String[] stringArray = {"c", "a", "e", "b", "d", "dd", "da", "zz", "AA", "aa", "aB", "Hb", "Z"};
|
|
||||||
print(treeSort.sort(stringArray));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user