Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class BinaryTree {
- static class Node {
- Node left, right;
- int data;
- public Node(int data) {
- this.data = data;
- }
- public void insert(int value) {
- if(value <= data) {
- if(left == null) {
- left = new Node(value);
- } else {
- left.insert(value);
- }
- } else {
- if(right == null) {
- right = new Node(value);
- } else {
- right.insert(value);
- }
- }
- }
- public boolean contains(int value) {
- if(value == data) return true;
- else if(value < data) {
- if(left == null) {
- return false;
- } else {
- return left.contains(value);
- }
- } else {
- if(right == null) {
- return false;
- } else {
- return right.contains(value);
- }
- }
- }
- public void printInOrder() {
- if(left != null) {
- left.printInOrder();
- }
- System.out.println(data);
- if(right != null) {
- right.printInOrder();
- }
- }
- }
- public static void main(String[] args) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement