|
| 1 | +// Java Binary Search Tree |
| 2 | + |
| 3 | +public class Tree { |
| 4 | + Node root; |
| 5 | + |
| 6 | + public boolean insert(int val) { |
| 7 | + if (root == null) { |
| 8 | + root = new Node(val); |
| 9 | + return true; |
| 10 | + } |
| 11 | + else |
| 12 | + return root.insert(val); |
| 13 | + } |
| 14 | + |
| 15 | + public boolean find(int val) { |
| 16 | + if (root == null) |
| 17 | + return false; |
| 18 | + else |
| 19 | + return root.find(val); |
| 20 | + } |
| 21 | + |
| 22 | + public void preorder() { |
| 23 | + if (root != null) { |
| 24 | + System.out.println("Preorder:"); |
| 25 | + root.preorder(); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public void postorder() { |
| 30 | + if (root != null) { |
| 31 | + System.out.println("Postorder:"); |
| 32 | + root.postorder(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public void inorder() { |
| 37 | + if (root != null) { |
| 38 | + System.out.println("Inorder:"); |
| 39 | + root.inorder(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private class Node { |
| 44 | + private Node leftChild; |
| 45 | + private Node rightChild; |
| 46 | + private int data; |
| 47 | + |
| 48 | + private Node(int val) { |
| 49 | + data = val; |
| 50 | + } |
| 51 | + |
| 52 | + private boolean insert(int val) { |
| 53 | + boolean added = false; |
| 54 | + if (this == null) { |
| 55 | + this.data = val; |
| 56 | + return true; |
| 57 | + } |
| 58 | + else { |
| 59 | + if (val < this.data) { |
| 60 | + if (this.leftChild == null) { |
| 61 | + this.leftChild = new Node(val); |
| 62 | + return true; |
| 63 | + } |
| 64 | + else |
| 65 | + added = this.leftChild.insert(val); |
| 66 | + } |
| 67 | + else if (val > this.data) { |
| 68 | + if (this.rightChild == null) { |
| 69 | + this.rightChild = new Node(val); |
| 70 | + return true; |
| 71 | + } |
| 72 | + else |
| 73 | + added = this.rightChild.insert(val); |
| 74 | + } |
| 75 | + } |
| 76 | + return added; |
| 77 | + } |
| 78 | + |
| 79 | + private boolean find(int val) { |
| 80 | + boolean found = false; |
| 81 | + if (this == null) |
| 82 | + return false; |
| 83 | + else { |
| 84 | + if (val == this.data) |
| 85 | + return true; |
| 86 | + else if (val < this.data && this.leftChild != null) |
| 87 | + found = this.leftChild.find(val); |
| 88 | + else if (val > this.data && this.rightChild != null) |
| 89 | + found = this.rightChild.find(val); |
| 90 | + } |
| 91 | + return found; |
| 92 | + } |
| 93 | + |
| 94 | + private void preorder() { |
| 95 | + if (this != null) { |
| 96 | + System.out.println(this.data); |
| 97 | + if (this.leftChild != null) |
| 98 | + this.leftChild.preorder(); |
| 99 | + if (this.rightChild != null) |
| 100 | + this.rightChild.preorder(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void postorder() { |
| 105 | + if (this != null) { |
| 106 | + if (this.leftChild != null) |
| 107 | + this.leftChild.postorder(); |
| 108 | + if (this.rightChild != null) |
| 109 | + this.rightChild.postorder(); |
| 110 | + System.out.println(this.data); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void inorder() { |
| 115 | + if (this != null) { |
| 116 | + if (this.leftChild != null) |
| 117 | + this.leftChild.inorder(); |
| 118 | + System.out.println(this.data); |
| 119 | + if (this.rightChild != null) |
| 120 | + this.rightChild.inorder(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments