Skip to content

Commit 5e69d53

Browse files
committed
randomize the key of bst
1 parent 3a4115d commit 5e69d53

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
function bst ( item, node, parent ) { // node = current node , parent = previous node
2-
tracer._visit ( node, parent )._wait ();
3-
if ( item === node ) { // key found
4-
tracer2._select ( 0 )._wait();
5-
logger._print ( ' Match Found ' );
6-
} else if ( item < node ) { // key less than value of current node
7-
if( T[node][0] === -1 ) {
8-
tracer2._notify ( 0, item )._wait();
9-
logger._print ( ' Not Found ');
10-
} else {
11-
tracer2._notify ( 0, item )._wait();
12-
bst ( item, T[node][0], node );
13-
tracer2._denotify ( 0 );
14-
}
15-
} else { // key greater than value of current node
16-
if ( T[node][1] === -1 ) {
17-
tracer2._notify ( 0, item )._wait();
18-
logger._print ( ' Not Found ');
19-
} else {
20-
tracer2._notify ( 0, item )._wait();
21-
bst ( item, T[node][1], node );
22-
tracer2._denotify ( 0 );
23-
}
24-
}
1+
function bst(item, node, parent) { // node = current node , parent = previous node
2+
tracer._visit(node, parent)._wait();
3+
if (item === node) { // key found
4+
logger._print(' Match Found ');
5+
} else if (item < node) { // key less than value of current node
6+
if (T[node][0] === -1) {
7+
logger._print(' Not Found ');
8+
} else {
9+
bst(item, T[node][0], node);
10+
}
11+
} else { // key greater than value of current node
12+
if (T[node][1] === -1) {
13+
logger._print(' Not Found ');
14+
} else {
15+
bst(item, T[node][1], node);
16+
}
17+
}
2518
}
2619

27-
bst ( key[0], 5 ); // node with key 5 is the root
20+
logger._print('Finding number ' + key);
21+
bst(key, 5); // node with key 5 is the root
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
var G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
2-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3-
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
4-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
5-
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0],
6-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
7-
[0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
8-
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
9-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
10-
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
11-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
12-
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
2+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3+
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
4+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
5+
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0],
6+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
7+
[0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
8+
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
9+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
10+
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
11+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
12+
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
1313
];
1414

1515

1616
var T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][1] indicates right child
17-
[-1,-1],
18-
[ 0, 2],
19-
[-1,-1],
20-
[ 1, 4],
21-
[-1,-1],
22-
[ 3, 8],
23-
[-1, 7],
24-
[-1,-1],
25-
[ 6,10],
26-
[-1,-1],
27-
[ 9,-1]
17+
[-1, -1],
18+
[0, 2],
19+
[-1, -1],
20+
[1, 4],
21+
[-1, -1],
22+
[3, 8],
23+
[-1, 7],
24+
[-1, -1],
25+
[6, 10],
26+
[-1, -1],
27+
[9, -1]
2828
];
2929

30-
var key = [2]; // item to be searched
31-
var tracer = new DirectedGraphTracer( " Binary Search Tree ")._setTreeData ( G, 5 );
32-
var tracer2 = new Array1DTracer ( " Key ")._setData ( key );
33-
var logger = new LogTracer ( " Log ");
34-
tracer.attach ( logger );
30+
var key = Math.random() * G.length | 0; // item to be searched
31+
var tracer = new DirectedGraphTracer(" Binary Search Tree ")._setTreeData(G, 5);
32+
var logger = new LogTracer(" Log ");
33+
tracer.attach(logger);

0 commit comments

Comments
 (0)