Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions algorithm/tree/binary_search_tree/bst_insert/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function bst_insert ( root, element, parent ) { // node = current node , parent = previous node
tracer._visit ( root, parent )._wait ();
if ( element < root ) {
if ( T[root][0] === -1 ) { // insert as left child of root
T[root][0] = element;
G[root][element] = 1; // update in graph
tracer._visit ( element, root )._wait ();
logger._print( element + ' Inserted ');
} else {
//tracer._clear ();
bst_insert ( T[root][0], element, root );
}
} else if ( element > root ) { // insert as right child of root
if( T[root][1] === -1 ) {
T[root][1] = element;
G[root][element] = 1; // update in graph
tracer._visit ( element, root )._wait ();
logger._print ( element + ' Inserted ');
} else {
//tracer._clear ();
bst_insert ( T[root][1], element, root );
}
}
}

var Root = elements[0]; // take first element as root
logger._print ( Root + ' Inserted as root of tree ');
tracer._setTreeData ( G, Root );

for (var i = 1; i < elements.length; i++) {
tracer2._select ( i )._wait();
bst_insert ( Root, elements[i] ); // insert ith element
tracer2._deselect( i );
}
34 changes: 34 additions & 0 deletions algorithm/tree/binary_search_tree/bst_insert/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var G = [ // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];


var T = [ // mapping to G as a binary tree , [i][0] indicates left child, [i][1] indicates right child
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1],
[-1,-1]
];

var elements = [5,8,10,3,1,6,9,7,2,0,4]; // item to be searched
var tracer = new DirectedGraphTracer( " BST - Elements marked red indicates the current status of tree ");
var tracer2 = new Array1DTracer ( " Elements ")._setData ( elements );
var logger = new LogTracer ( " Log ");
tracer.attach ( logger );
5 changes: 3 additions & 2 deletions algorithm/tree/binary_search_tree/desc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"Search applications where data is constantly entering/leaving such as map and set objects in many languages' library."
],
"Complexity": {
"time": "Best : O(1) Average : O(logN) Worst : O(N)",
"time": " Best : O(1) Average : O(logN) Worst : O(N) ",
"space": "O(n)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Binary_search_tree'>Wikipedia</a>"
],
"files": {
"bst_search": "Search in Binary Search Tree"
"bst_search": "Search in Binary Search Tree",
"bst_insert": "Insert in Binary Search Tree"
}
}