Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created September 25, 2014 14:15
Show Gist options
  • Save Krewn/45adfec506b86f621710 to your computer and use it in GitHub Desktop.
Save Krewn/45adfec506b86f621710 to your computer and use it in GitHub Desktop.
Bare Bones tree w/ search. -needs to implement generics < T >-
package Trees;
import Trees.treeNode;
public class tree{
public treeNode root;
public treeNode current;
int size;
boolean first;
public tree(){
super();
root = new treeNode(0);
current=root;
System.out.print("youMadeATree");
}
public tree(int n){
root = new treeNode(n);
current=root;
System.out.print("youMadeATree");
}
public void add(treeNode n){
current.addBranch(n);
current=n;
}
public void getRoot(){
current=root;
}
public treeNode search(int n){
System.out.println("letsSearch");
return(root.search(n));
}
}
package Trees;
import java.util.Vector;
public class treeNode{
int val;
treeNode parent;
Vector<treeNode> branches;
public treeNode(){
val=0;
branches = new Vector<treeNode>();
}
public treeNode(int n){
val=n;
branches = new Vector<treeNode>();
}
public int getval(){int Val = val ; return(Val);}
public boolean isEmpty(){
boolean ret = branches.size()==0 ? true : false;//love it.
return(ret);
}
public void addBranch(treeNode newBranch ){
branches.add(newBranch);
}
public int size(){return(branches.size());}
public boolean rmVal(int n){
int k=0;
int temp;
boolean ret=false;
while(k<branches.size()){
temp = branches.elementAt(k).getval() == n ? 0 : 1;
k+=temp;
if(temp==0){branches.removeElementAt(k); ret = true;}
}
return(ret);
}
public boolean rmAt(int n){
boolean ret=false;
if(n<branches.size()){branches.removeElementAt(n);ret=true;}
return(ret);
}
public int getMaxBranches(){
return(branches.capacity());
}
public treeNode search(int n){
System.out.println("Search");
new java.util.Scanner(System.in).nextLine(); // press enter to simulate a unit of time;
if(val==n){
System.out.println("Found!");
new java.util.Scanner(System.in).nextLine();
return(this);
}else{
System.out.println("NotThisOne");
new java.util.Scanner(System.in).nextLine();
if(branches.size()==0){
return(null);
}else{
System.out.println("WhileBranch");
int k = 0;
while(k<branches.size()){
System.out.println("While: "+k);
new java.util.Scanner(System.in).nextLine();
treeNode temp=branches.elementAt(k).search(n);
if (temp!=null){
System.out.println("found2");
return(temp);
}
k++;
}
return(null);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment