docs: update AnyBaseToAnyBase and GenericTree

This commit is contained in:
yanglbme 2019-05-09 20:20:44 +08:00
parent 36dc276f29
commit b1d4be7f86
3 changed files with 205 additions and 291 deletions

View File

@ -15,15 +15,16 @@ import java.util.Scanner;
*/
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 MAXIMUM_BASE = 36;
// Driver
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String n;
int b1 = 0, b2 = 0;
int b1, b2;
while (true) {
try {
System.out.print("Enter number: ");
@ -64,7 +65,7 @@ public class AnyBaseToAnyBase {
char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base);
// 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++)
digitsList.add(digitsForBase[i]);

View File

@ -4,6 +4,16 @@ import java.util.ArrayList;
import java.util.LinkedList;
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 {
private class Node {
int data;
@ -13,17 +23,6 @@ public class GenericTree {
private Node root;
private int size;
/*
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.
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 GenericTree() { //Constructor
Scanner scn = new Scanner(System.in);
root = create_treeG(null, 0, scn);
@ -42,19 +41,18 @@ public class GenericTree {
System.out.println("number of children");
int number = scn.nextInt();
for (int i = 0; i < number; i++) {
Node childd = create_treeG(node, i, scn);
Node child = create_treeG(node, i, scn);
size++;
node.child.add(childd);
node.child.add(child);
}
return node;
}
/*
Function to display the generic tree
/**
* Function to display the generic tree
*/
public void display() { //Helper function
display_1(root);
return;
}
private void display_1(Node parent) {
@ -66,14 +64,14 @@ public class GenericTree {
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
size goes as follows
/**
* One call store the size directly but if you are asked compute size this function to calculate
* size goes as follows
*
* @return size
*/
public int size2call() {
return size2(root);
}
@ -86,8 +84,10 @@ public class GenericTree {
return sz + 1;
}
/*
Function to compute maximum value in the generic tree
/**
* Function to compute maximum value in the generic tree
*
* @return maximum value
*/
public int maxcall() {
int maxi = root.data;
@ -104,10 +104,11 @@ public class GenericTree {
return maxi;
}
/*
Function to compute HEIGHT of the generic tree
/**
* Function to compute HEIGHT of the generic tree
*
* @return height
*/
public int heightcall() {
return height(root) - 1;
}
@ -122,10 +123,12 @@ public class GenericTree {
return h + 1;
}
/*
Function to find whether a number is present in the generic tree or not
/**
* 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);
}
@ -140,8 +143,11 @@ public class GenericTree {
return false;
}
/*
Function to calculate depth of generic tree
/**
* Function to calculate depth of generic tree
*
* @param dep depth
*/
public void depthcaller(int dep) {
depth(root, dep);
@ -157,8 +163,8 @@ public class GenericTree {
return;
}
/*
Function to print generic tree in pre-order
/**
* Function to print generic tree in pre-order
*/
public void preordercall() {
preorder(root);
@ -171,8 +177,8 @@ public class GenericTree {
preorder(node.child.get(i));
}
/*
Function to print generic tree in post-order
/**
* Function to print generic tree in post-order
*/
public void postordercall() {
postorder(root);
@ -185,10 +191,9 @@ public class GenericTree {
System.out.print(node.data + " ");
}
/*
Function to print generic tree in level-order
/**
* Function to print generic tree in level-order
*/
public void levelorder() {
LinkedList<Node> q = new LinkedList<>();
q.addLast(root);
@ -204,8 +209,8 @@ public class GenericTree {
System.out.println(".");
}
/*
Function to remove all leaves of generic tree
/**
* Function to remove all leaves of generic tree
*/
public void removeleavescall() {
removeleaves(root);

View File

@ -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));
}
}