Skip to content

Commit 39e389e

Browse files
get , min , max added.
1 parent 896d8a8 commit 39e389e

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

src/com/mehmetpekdemir/Main.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,16 @@ public static void main(String[] args) {
209209

210210
System.out.println("\n----------Pre Order----------");
211211
tree.traversePreOrder();
212-
212+
213213
System.out.println("\n----------In Order----------");
214214
tree.traverseInOrder();
215-
215+
216216
System.out.println("\n----------Post Order----------");
217217
tree.traversePostOrder();
218-
219-
218+
219+
System.out.println("\n" + tree.get(27));
220+
System.out.println(tree.min());
221+
System.out.println(tree.max());
220222

221223
}
222224

src/com/mehmetpekdemir/tree/Tree.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,28 @@ public void traversePostOrder() {
3535
}
3636
}
3737

38+
public TreeNode get(int value) {
39+
if (root != null) {
40+
return root.get(value);
41+
}
42+
43+
return null;
44+
}
45+
46+
public int min() {
47+
if(root == null) {
48+
return Integer.MIN_VALUE;
49+
} else {
50+
return root.min();
51+
}
52+
}
53+
54+
public int max() {
55+
if(root == null) {
56+
return Integer.MAX_VALUE;
57+
} else {
58+
return root.max();
59+
}
60+
}
61+
3862
}

src/com/mehmetpekdemir/tree/TreeNode.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,40 @@ public void traversePostOrder() {
6868
System.out.print(data + " ");
6969
}
7070

71+
public TreeNode get(int value) {
72+
if (value == data) {
73+
return this;
74+
}
75+
76+
if (value < data) {
77+
if (leftChild != null) {
78+
return leftChild.get(value);
79+
}
80+
} else {
81+
if (rightChild != null) {
82+
return rightChild.get(value);
83+
}
84+
}
85+
86+
return null;
87+
}
88+
89+
public int min() {
90+
if (leftChild == null) {
91+
return data;
92+
} else {
93+
return leftChild.min();
94+
}
95+
}
96+
97+
public int max() {
98+
if (rightChild == null) {
99+
return data;
100+
} else {
101+
return rightChild.max();
102+
}
103+
}
104+
71105
public int getData() {
72106
return data;
73107
}
@@ -92,4 +126,9 @@ public void setRightChild(TreeNode rightChild) {
92126
this.rightChild = rightChild;
93127
}
94128

129+
@Override
130+
public String toString() {
131+
return "[Data=" + data + "]";
132+
}
133+
95134
}

0 commit comments

Comments
 (0)