Skip to content

Commit ee55c3c

Browse files
committed
WIP BST Kth Element
1 parent aa64386 commit ee55c3c

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

Diff for: src/main/java/com/liangsworld/bst/BSTFunctions.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.liangsworld.bst;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: liliang
6+
* Date: 12/19/14
7+
* Time: 11:31 AM
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public class BSTFunctions {
11+
12+
13+
int counter = 0;
14+
15+
public void resetCounter(){
16+
counter = 0;
17+
}
18+
19+
private BSTNode targetNode = null;
20+
21+
public void resetTargetNode(){
22+
targetNode = null;
23+
}
24+
25+
//print kth smallest
26+
void printKthSmallest(BSTNode node, int k){
27+
if (node == null)
28+
return;
29+
30+
printKthSmallest(node.getLeft(), k);
31+
32+
if(++ counter == k)
33+
System.out.println(String.format("%d -th smallest is %d", k, node.getValue()));
34+
35+
printKthSmallest(node.getRight(), k);
36+
}
37+
38+
//print kth smallest
39+
BSTNode getKthSmallest(BSTNode node, int k){
40+
if (node == null)
41+
return null;
42+
else{
43+
getKthSmallest(node.getLeft(), k);
44+
45+
// if(targetNode == null && ++counter == k)
46+
if(++counter == k)
47+
return node;
48+
49+
// return getKthSmallest(node.getRight(), k);
50+
return getKthSmallest(node.getRight(), k);
51+
}
52+
//
53+
// if(++ counter == k)
54+
// targetNode = node ;
55+
//
56+
// getKthSmallest(node.getRight(), k);
57+
//
58+
// return targetNode;
59+
}
60+
61+
62+
//print kth largest
63+
64+
65+
}

Diff for: src/main/java/com/liangsworld/bst/BSTNode.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.liangsworld.bst;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
* User: liliang
6+
* Date: 12/19/14
7+
* Time: 11:32 AM
8+
* To change this template use File | Settings | File Templates.
9+
*/
10+
public class BSTNode {
11+
12+
int value;
13+
BSTNode left, right;
14+
15+
public BSTNode(int value){
16+
this.value = value;
17+
}
18+
19+
public BSTNode(int value, BSTNode left, BSTNode right) {
20+
this.value = value;
21+
this.left = left;
22+
this.right = right;
23+
}
24+
25+
public int getValue() {
26+
return value;
27+
}
28+
29+
public void setValue(int value) {
30+
this.value = value;
31+
}
32+
33+
public BSTNode getLeft() {
34+
return left;
35+
}
36+
37+
public void setLeft(BSTNode left) {
38+
this.left = left;
39+
}
40+
41+
public BSTNode getRight() {
42+
return right;
43+
}
44+
45+
public void setRight(BSTNode right) {
46+
this.right = right;
47+
}
48+
49+
50+
51+
}

Diff for: src/test/java/com/liangsworld/bst/TestKthNode.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.liangsworld.bst;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.Random;
9+
10+
import static org.junit.Assert.assertTrue;
11+
12+
/**
13+
* Created with IntelliJ IDEA.
14+
* User: liliang
15+
* Date: 12/19/14
16+
* Time: 11:42 AM
17+
* To change this template use File | Settings | File Templates.
18+
*/
19+
public class TestKthNode {
20+
21+
BSTNode root;
22+
BSTFunctions functions;
23+
24+
@Before
25+
public void setUp(){
26+
constructBST();
27+
functions = new BSTFunctions();
28+
}
29+
30+
@Test
31+
public void testPrintKthSmallest(){
32+
functions.printKthSmallest(root,1);
33+
functions.resetCounter();
34+
functions.printKthSmallest(root,3);
35+
functions.resetCounter();
36+
functions.printKthSmallest(root,6);
37+
functions.resetCounter();
38+
}
39+
40+
@Test
41+
public void testGetKthSmallest(){
42+
// Assert.assertEquals(functions.getKthSmallest(root,1).getValue(), 2);
43+
// functions.resetTargetNode();
44+
Assert.assertEquals(functions.getKthSmallest(root,3).getValue(), 18);
45+
functions.resetTargetNode();
46+
// Assert.assertEquals(functions.getKthSmallest(root,6).getValue(), 48);
47+
// functions.resetTargetNode();
48+
// Assert.assertEquals(functions.getKthSmallest(root,8).getValue(), 65);
49+
// functions.resetTargetNode();
50+
// Assert.assertEquals(functions.getKthSmallest(root,13).getValue(), 123);
51+
// functions.resetTargetNode();
52+
}
53+
54+
private void constructBST(){
55+
BSTNode node1 = new BSTNode(2);
56+
BSTNode node2 = new BSTNode(18);
57+
BSTNode node3 = new BSTNode(12);
58+
BSTNode node4 = new BSTNode(38);
59+
BSTNode node5 = new BSTNode(43);
60+
BSTNode node6 = new BSTNode(48);
61+
BSTNode node7 = new BSTNode(52);
62+
BSTNode node8 = new BSTNode(83);
63+
BSTNode node9 = new BSTNode(71);
64+
BSTNode node10 = new BSTNode(65);
65+
BSTNode node11 = new BSTNode(79);
66+
BSTNode node12 = new BSTNode(152);
67+
BSTNode node13 = new BSTNode(99);
68+
BSTNode node14 = new BSTNode(123);
69+
BSTNode node15 = new BSTNode(291);
70+
71+
node3.setLeft(node1);
72+
node3.setRight(node2);
73+
node4.setLeft(node3);
74+
node4.setRight(node5);
75+
node5.setRight(node6);
76+
node7.setLeft(node4);
77+
node7.setRight(node8);
78+
node8.setLeft(node9);
79+
node8.setRight(node12);
80+
node9.setLeft(node10);
81+
node9.setRight(node11);
82+
node12.setLeft(node13);
83+
node12.setRight(node15);
84+
node13.setRight(node14);
85+
86+
root = node7;
87+
}
88+
}

0 commit comments

Comments
 (0)