-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
50 lines (42 loc) · 1.16 KB
/
BinarySearchTree.java
File metadata and controls
50 lines (42 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Iterator;
public class BinarySearchTree<T extends Comparable<T>>
{
/*
Excercise 1
*/
public int countNodes (int top, int bottom){
if (top<=bottom && !isEmpty()){
if (top<=0 && bottom>=0){
return 1
+ leftChild.countNodes(top-1, bottom-1)
+ rightChild.countNodes(top-1, bottom-1);
}
if(bottom < 0){
return 0;
}
return leftChild.countNodes(top-1, bottom-1)
+ rightChild.countNodes(top-1, bottom-1);
}
else {
return 0;
}
}
/*
Excercise 2
*/
public int sortedUpTo(int needToPrint){
if (needToPrint > 0 && !isEmpty()){
int leftToPrint = leftChild.sortedUpTo(needToPrint);
if (leftToPrint!=0) {
System.out.println(content);
leftToPrint = rightChild.sortedUpTo(needToPrint-1);
return leftToPrint;
}else{
return 0;
}
}else{
return needToPrint;
}
}
}
# Modified 2025-08-11 10:24:29