BonsaiTree is PAINJNNNNNNNNN
This commit is contained in:
11
src/ScuffedLinkedList/BonsaiLeaf.java
Normal file
11
src/ScuffedLinkedList/BonsaiLeaf.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package ScuffedLinkedList;
|
||||||
|
|
||||||
|
public class BonsaiLeaf {
|
||||||
|
BonsaiLeaf left;
|
||||||
|
BonsaiLeaf right;
|
||||||
|
int value;
|
||||||
|
|
||||||
|
public BonsaiLeaf(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/ScuffedLinkedList/BonsaiTree.java
Normal file
54
src/ScuffedLinkedList/BonsaiTree.java
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package ScuffedLinkedList;
|
||||||
|
|
||||||
|
public class BonsaiTree {
|
||||||
|
public BonsaiLeaf root;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void add(int value)
|
||||||
|
{
|
||||||
|
root = add(value,this.root);
|
||||||
|
}
|
||||||
|
public BonsaiLeaf add(int value,BonsaiLeaf root)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(root == null)
|
||||||
|
|
||||||
|
root = new BonsaiLeaf(value);
|
||||||
|
|
||||||
|
else if(value > root.value)
|
||||||
|
root.right = add(value, root.right);
|
||||||
|
else
|
||||||
|
root.left = add(value,root.left);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
public void search(int value)
|
||||||
|
{
|
||||||
|
search(value,root);
|
||||||
|
}
|
||||||
|
public void search(int value,BonsaiLeaf root)
|
||||||
|
{
|
||||||
|
if(root == null)
|
||||||
|
System.out.println("NOT FOUND!!!!!!!!!!!!!");
|
||||||
|
else if(root.value == value)
|
||||||
|
System.out.println("Found!");
|
||||||
|
else if(value>root.value)
|
||||||
|
search(value,root.right);
|
||||||
|
else if(value<root.value)
|
||||||
|
search(value,root.left);
|
||||||
|
|
||||||
|
}
|
||||||
|
public boolean isLeaf(BonsaiLeaf node)
|
||||||
|
{
|
||||||
|
if(node.right == null && node.left == null)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
public boolean isRoot(BonsaiLeaf node)
|
||||||
|
{
|
||||||
|
return !isLeaf(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
25
src/ScuffedLinkedList/BonsaiTreeTest.java
Normal file
25
src/ScuffedLinkedList/BonsaiTreeTest.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package ScuffedLinkedList;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class BonsaiTreeTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void add() {
|
||||||
|
BonsaiTree T = new BonsaiTree();
|
||||||
|
T.add(5);
|
||||||
|
T.add(6);
|
||||||
|
T.add(7);
|
||||||
|
T.add(9);
|
||||||
|
T.add(8);
|
||||||
|
T.add(3);
|
||||||
|
T.add(4);
|
||||||
|
System.out.println(T.root.right.value);
|
||||||
|
T.search(9);
|
||||||
|
T.search(0);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user