Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Tracer to depict addition of nodes in binary search tree #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2017
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
36 changes: 17 additions & 19 deletions algorithm/tree/binary_search_tree/bst_insert/code.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
function bst_insert ( root, element, parent ) { // node = current node , parent = previous node
function bst_insert ( root, element, parent ) { // root = current node , parent = previous node
tracer._visit ( root, parent )._wait ();
var treeNode = T[root];
var propName = '';
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 ();
propName = 'left';
} else if ( element > root) {
propName = 'right';
}
if(propName !== '') {
if ( !treeNode.hasOwnProperty(propName) ) { // insert as left child of root
treeNode[propName] = element;
T[element] = {};
tracer._addNode(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 );
bst_insert ( treeNode[propName], element, root );
}
}
}

var Root = elements[0]; // take first element as root
T[Root] = {};
tracer._addRoot(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 );
tracer2._deselect( i )._wait();
tracer._clearTraversal();
}
31 changes: 2 additions & 29 deletions algorithm/tree/binary_search_tree/bst_insert/data.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
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 T = {};

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 tracer = new DirectedGraphConstructTracer( " BST - Elements marked red indicates the current status of tree ", 0);
var tracer2 = new Array1DTracer ( " Elements ")._setData ( elements );
var logger = new LogTracer ( " Log ");
tracer.attach ( logger );
Loading