Advertisement
Guest User

fullcode

a guest
Feb 28th, 2018
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. public class Node {
  2.     private int data;
  3.  
  4.     public int getData() {
  5.         return data;
  6.     }
  7.  
  8.     private Node left;
  9.     private Node right;
  10.  
  11.     public Node(int d, Node l, Node r) {
  12.         data = d;
  13.         left = l;
  14.         right = r;
  15.     }
  16.  
  17.     // Deep copy constructor
  18.     public Node(Node o) {
  19.         if (o == null) return;
  20.         this.data = o.data;
  21.         if (o.left != null) this.left = new Node(o.left);
  22.         if (o.right != null) this.right = new Node(o.right);
  23.     }
  24.  
  25.     public String toString() {
  26.         String l = (left == null) ? "" : (left + ", ");
  27.         String r = (right == null) ? "" : (", " + right);
  28.         return l + data + r;
  29.     }
  30.  
  31.     public void insert(int d) {
  32.         if (data == d) return;
  33.  
  34.         if (data > d) {
  35.             if (left == null)
  36.                 left = new Node(d, null, null);
  37.             else
  38.                 left.insert(d);
  39.  
  40.         } else {
  41.             if (right == null)
  42.                 right = new Node(d, null, null);
  43.             else
  44.                 right.insert(d);
  45.         }
  46.     }
  47.  
  48.     public boolean contains(int d) {
  49.         if (data == d) return true;
  50.  
  51.         if (data > d)
  52.             return (left == null) ? false : left.contains(d);
  53.  
  54.         return (right == null) ? false : right.contains(d);
  55.     }
  56.  
  57.     public boolean equals(Node o) {
  58.         boolean result = false;
  59.         if (o != null) {
  60.             if (o.left == left && o.right == right && o.data == data) {
  61.                 result = true;
  62.             }
  63.         }
  64.         return result;
  65.     }
  66.  
  67.     public List<Integer> toList() {
  68.  
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement