Advertisement
Guest User

wowwwwwwwesers

a guest
Apr 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class BinaryTree {
  4. static class Node {
  5. Node left, right;
  6. int data;
  7. public Node(int data) {
  8. this.data = data;
  9. }
  10.  
  11. public void insert(int value) {
  12. if(value <= data) {
  13. if(left == null) {
  14. left = new Node(value);
  15. } else {
  16. left.insert(value);
  17. }
  18. } else {
  19. if(right == null) {
  20. right = new Node(value);
  21. } else {
  22. right.insert(value);
  23. }
  24. }
  25. }
  26.  
  27. public boolean contains(int value) {
  28. if(value == data) return true;
  29. else if(value < data) {
  30. if(left == null) {
  31. return false;
  32. } else {
  33. return left.contains(value);
  34. }
  35. } else {
  36. if(right == null) {
  37. return false;
  38. } else {
  39. return right.contains(value);
  40. }
  41. }
  42. }
  43.  
  44. public void printInOrder() {
  45. if(left != null) {
  46. left.printInOrder();
  47. }
  48. System.out.println(data);
  49. if(right != null) {
  50. right.printInOrder();
  51. }
  52. }
  53. }
  54.  
  55. public static void main(String[] args) {
  56.  
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement