Created
February 15, 2018 23:45
-
-
Save Jun711/792dbe47c2e294312b5ee7a13ca1f49d to your computer and use it in GitHub Desktop.
TestDome - Binary Search 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
class Node { | |
public int value; | |
public Node left, right; | |
public Node(int value, Node left, Node right) { | |
this.value = value; | |
this.left = left; | |
this.right = right; | |
} | |
} | |
public class BinarySearchTree { | |
public static boolean contains(Node root, int value) { | |
if (root == null) | |
return false; | |
if (root.value == value) | |
return true; | |
else if (root.value > value) | |
return contains(root.left, value); | |
else | |
return contains(root.right, value); | |
} | |
public static void main(String[] args) { | |
Node n1 = new Node(1, null, null); | |
Node n3 = new Node(3, null, null); | |
Node n2 = new Node(2, n1, n3); | |
System.out.println(contains(n2, 3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment