MCPcopy Create free account
hub / github.com/careercup/ctci / TreeNode

Class TreeNode

java/CtCILibrary/CtCILibrary/TreeNode.java:6–104  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4 * character.
5 */
6public class TreeNode {
7 public int data;
8 public TreeNode left;
9 public TreeNode right;
10 public TreeNode parent;
11 private int size = 0;
12
13 public TreeNode(int d) {
14 data = d;
15 size = 1;
16 }
17
18 public void setLeftChild(TreeNode left) {
19 this.left = left;
20 if (left != null) {
21 left.parent = this;
22 }
23 }
24
25 public void setRightChild(TreeNode right) {
26 this.right = right;
27 if (right != null) {
28 right.parent = this;
29 }
30 }
31
32 public void insertInOrder(int d) {
33 if (d <= data) {
34 if (left == null) {
35 setLeftChild(new TreeNode(d));
36 } else {
37 left.insertInOrder(d);
38 }
39 } else {
40 if (right == null) {
41 setRightChild(new TreeNode(d));
42 } else {
43 right.insertInOrder(d);
44 }
45 }
46 size++;
47 }
48
49 public int size() {
50 return size;
51 }
52
53 public boolean isBST() {
54 if (left != null) {
55 if (data < left.data || !left.isBST()) {
56 return false;
57 }
58 }
59
60 if (right != null) {
61 if (data >= right.data || !right.isBST()) {
62 return false;
63 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected