MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / BST

Class BST

Java/SearchingAlgorithms/BFS/BST.java:3–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1import java.util.LinkedList;
2import java.util.ArrayList;
3public class BST {
4 Node root;
5
6 BST(){
7 root = null;
8 }
9
10 class Node{
11 int value;
12 Node left;
13 Node right;
14
15 Node(int value){
16 this.value = value;
17 }
18 }
19
20 // insert
21 public void insert(int value){
22 if(root == null){
23 Node n = new Node(value);
24 root = n;
25 }
26 else{
27 Node tmp = root;
28 while(true){
29 if(value >= tmp.value){
30 if(tmp.right == null){
31 tmp.right = new Node(value);
32 break;
33 }
34 else{
35 tmp = tmp.right;
36 }
37 }
38 else{
39 if(tmp.left == null){
40 tmp.left = new Node(value);
41 break;
42 }
43 else{
44 tmp = tmp.left;
45 }
46 }
47 }
48 tmp = null;
49 }
50 }
51
52 // find/lookup
53 public boolean contains(int value){
54 if(root == null){
55 return false;
56 }
57 else{
58 Node tmp = root;
59 while(tmp != null){
60 if(tmp.value == value){

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected