BonsaiTree is PAINJNNNNNNNNN

This commit is contained in:
linly
2022-05-17 16:20:56 +02:00
parent 2b4d48fb93
commit a6368c8cdf
3 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package ScuffedLinkedList;
public class BonsaiLeaf {
BonsaiLeaf left;
BonsaiLeaf right;
int value;
public BonsaiLeaf(int value) {
this.value = value;
}
}

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

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