Last active
April 20, 2020 08:41
-
-
Save so77id/9eca581f0f6116e62bb955651bc44ba5 to your computer and use it in GitHub Desktop.
Generic Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Vector; | |
class Btree<T> { | |
private Node<T> root; | |
public Btree() { | |
this.root = null; | |
} | |
public Btree(Node<T> root) { | |
this.root = root; | |
} | |
public Node<T> getRoot() { return this.root;} | |
public void setRoot(Node<T> root) {this.root = root;} | |
public boolean search(T value) { | |
return this.getSearch(value, this.root); | |
} | |
public Vector<T> inOrder() { | |
Vector<T> inOrderVector = new Vector<T>(); | |
this.getInOrder(this.root, inOrderVector); | |
return inOrderVector; | |
} | |
public Vector<T> preOrder() { | |
Vector<T> preOrderVector = new Vector<T>(); | |
this.getPreOrder(this.root, preOrderVector); | |
return preOrderVector; | |
} | |
public Vector<T> postOrder() { | |
Vector<T> postOrderVector = new Vector<T>(); | |
this.getPostOrder(this.root, postOrderVector); | |
return postOrderVector; | |
} | |
private boolean getSearch(T value, Node<T> root) { | |
if(root == null) return false; | |
if(root.getValue() == value) return true; | |
return (this.getSearch(value, root.getLeft()) || this.getSearch(value, root.getRight())); | |
} | |
private void getInOrder(Node<T> root, Vector<T> inOrderVector) { | |
if(root == null) return; | |
this.getInOrder(root.getLeft(), inOrderVector); | |
inOrderVector.add(root.getValue()); | |
this.getInOrder(root.getRight(), inOrderVector); | |
} | |
private void getPreOrder(Node<T> root, Vector<T> preOrderVector) { | |
if(root == null) return; | |
preOrderVector.add(root.getValue()); | |
this.getPreOrder(root.getLeft(), preOrderVector); | |
this.getPreOrder(root.getRight(), preOrderVector); | |
} | |
private void getPostOrder(Node<T> root, Vector<T> postOrderVector) { | |
if(root == null) return; | |
this.getPostOrder(root.getLeft(), postOrderVector); | |
this.getPostOrder(root.getRight(), postOrderVector); | |
postOrderVector.add(root.getValue()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
This program builds a tree from its inorder and postorder representation | |
**/ | |
import java.util.Scanner; | |
import java.util.Vector; | |
public class Main { | |
public static <T> Node<T> create_tree(Vector<T> inOrder, Vector<T> preOrder, int startIn, int endIn, int startPre, int endPre) { | |
if(startIn > endIn) return null; | |
if(startPre > endPre) return null; | |
if(endIn - startIn != endPre - startPre) return null; | |
Node<T> root = new Node<T>(preOrder.get(startPre)); | |
int inIndex = inOrder.indexOf(preOrder.get(startPre)); | |
root.setLeft(create_tree(inOrder, preOrder, startIn, inIndex - 1, startPre + 1, startPre+(inIndex - startIn))); | |
root.setRight(create_tree(inOrder, preOrder, inIndex + 1, endIn, startPre+(inIndex - startIn)+1, endPre)); | |
return root; | |
} | |
public static void main(String[] args){ | |
int n, buff; | |
Vector<Integer> inOrder = new Vector<Integer>(); | |
Vector<Integer> preOrder = new Vector<Integer>(); | |
Btree<Integer> tree; | |
Scanner scanner = new Scanner(System.in); // Create a Scanner object | |
n = scanner.nextInt(); | |
for(int i=0;i<n;i++){ | |
buff = scanner.nextInt(); | |
inOrder.add(buff); | |
} | |
for(int i=0;i<n;i++){ | |
buff = scanner.nextInt(); | |
preOrder.add(buff); | |
} | |
tree = new Btree<Integer>(create_tree(inOrder, preOrder, 0, inOrder.size()-1, 0, preOrder.size()-1)); | |
System.out.println(tree.inOrder()); | |
System.out.println(tree.preOrder()); | |
System.out.println(tree.postOrder()); | |
buff = scanner.nextInt(); | |
System.out.println(tree.search(buff)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node<T>{ | |
private T value; | |
Node<T> left; | |
Node<T> right; | |
public Node(T value){ | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
} | |
public Node(T value, Node<T> left, Node<T> right){ | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
public T getValue() {return this.value;} | |
public Node<T> getLeft() {return this.left;} | |
public Node<T> getRight() {return this.right;} | |
public void setValue(T value) {this.value = value;} | |
public void setLeft(Node<T> left) {this.left = left;} | |
public void setRight(Node<T> right) {this.right = right;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment