From c6a22c0f3dd4da56d359fc7ff0311f2ab6aac43f Mon Sep 17 00:00:00 2001 From: archie94 Date: Sat, 4 Jun 2016 15:14:56 +0530 Subject: [PATCH] Algorithm for insertion in BST --- .../binary_search_tree/bst_insert/code.js | 34 +++++++++++++++++++ .../binary_search_tree/bst_insert/data.js | 34 +++++++++++++++++++ algorithm/tree/binary_search_tree/desc.json | 5 +-- 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 algorithm/tree/binary_search_tree/bst_insert/code.js create mode 100644 algorithm/tree/binary_search_tree/bst_insert/data.js diff --git a/algorithm/tree/binary_search_tree/bst_insert/code.js b/algorithm/tree/binary_search_tree/bst_insert/code.js new file mode 100644 index 00000000..8dad4b60 --- /dev/null +++ b/algorithm/tree/binary_search_tree/bst_insert/code.js @@ -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 ); +} diff --git a/algorithm/tree/binary_search_tree/bst_insert/data.js b/algorithm/tree/binary_search_tree/bst_insert/data.js new file mode 100644 index 00000000..d062c779 --- /dev/null +++ b/algorithm/tree/binary_search_tree/bst_insert/data.js @@ -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 ); \ No newline at end of file diff --git a/algorithm/tree/binary_search_tree/desc.json b/algorithm/tree/binary_search_tree/desc.json index a98a2229..9feae49d 100644 --- a/algorithm/tree/binary_search_tree/desc.json +++ b/algorithm/tree/binary_search_tree/desc.json @@ -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": [ "Wikipedia" ], "files": { - "bst_search": "Search in Binary Search Tree" + "bst_search": "Search in Binary Search Tree", + "bst_insert": "Insert in Binary Search Tree" } } \ No newline at end of file