Skip to content

Commit 099d702

Browse files
committed
Algorithm for searching in BST
1 parent 3ca02f9 commit 099d702

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

algorithm/category.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
"page_rank": "PageRank Algorithm"
1313
}
1414
},
15+
"tree": {
16+
"name": "Tree",
17+
"list": {
18+
"binary_search_tree": "Binary Search Tree"
19+
}
20+
},
1521
"number_theory": {
1622
"name": "Number Theory",
1723
"list": {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}
25+
}
26+
27+
bst ( key[0], 5 ); // node with key 5 is the root
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
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]
13+
];
14+
15+
16+
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]
28+
];
29+
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 );
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Binary Search Tree": "Binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type of containers: data structures that store \"items\" (such as numbers, names etc.) in memory. They allow fast lookup, addition and removal of items, and can be used to implement either dynamic sets of items, or lookup tables that allow finding an item by its key (e.g., finding the phone number of a person by name).",
3+
"Applications": [
4+
"Search applications where data is constantly entering/leaving such as map and set objects in many languages' library."
5+
],
6+
"Complexity": {
7+
"time": "Best : O(1) Average : O(logN) Worst : O(N)",
8+
"space": "O(n)"
9+
},
10+
"References": [
11+
"<a href='https://en.wikipedia.org/wiki/Binary_search_tree'>Wikipedia</a>"
12+
],
13+
"files": {
14+
"bst_search": "Search in Binary Search Tree"
15+
}
16+
}

0 commit comments

Comments
 (0)