1 public class TestRunner extends Thread {
6 Node m_tree; // The root of a BST
8 public TestRunner(int index,
13 this.m_nodenum = nodenum;
14 this.m_tree = new Node();
18 // Randomly generate new (key, value) pair and insert into the tree
19 // If have collision, simply throw away the old node
20 // The tree can hold m_size nodes at most, if it has reached the
21 // limitation of m_size, then replace the node whose key is the
22 // closest to the new key.
23 Random rand = new Random(m_index);
24 while(this.m_nodenum-- > 0) {
25 // Generate a new (key, value) pair
26 int key = Math.abs(rand.nextInt());
27 int value = Math.abs(rand.nextInt());
28 if(this.m_tree.insert(key, value, !(this.m_size > 0))) {
64 public void setParent(Node p) {
68 public void setLeftChild(int key,
70 Node n = new Node(key, value);
75 public void setRightChild(int key,
77 Node n = new Node(key, value);
82 public boolean insert(int key,
85 if(this.m_key == -1) {
90 if(this.m_key == key) {
94 } else if(this.m_key > key) {
95 if(this.m_left == null) {
98 // replace this node with the new node
102 setLeftChild(key, value);
105 // insert into the left subtree
106 return this.m_left.insert(key, value, candelete);
108 } else if(this.m_key < key) {
109 if(this.m_right == null) {
115 setRightChild(key, value);
118 // insert into the right subtree
119 return this.m_right.insert(key, value, candelete);
126 public void replace(int key,
128 Node n = new Node(key, value);
129 n.m_left = this.m_left;
130 n.m_right = this.m_right;
131 n.m_parent = this.m_parent;
132 if(this.m_parent != null) {
133 if(this.m_parent.getKey() > key) {
134 this.m_parent.m_left = n;
136 this.m_parent.m_right = n;
139 if(this.m_left != null) {
140 this.m_left.m_parent = n;
142 if(this.m_right != null) {
143 this.m_right.m_parent = n;
148 public static void main(String[] args) {
149 int threadnum = 62; // 56;
151 int nodenum = size*10;
152 for(int i = 0; i < threadnum; ++i) {
153 TestRunner tr = new TestRunner(i, size, nodenum);