Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function BinarySearch(t,A){
- var i = 0, j = A.length, k;
- while (i < j){
- k = Math.floor((i+j)/2);
- if (t <= A[k]) j = k;
- else i = k+1;
- }
- if (A[ i ] === t) return i;
- else return -1;
- }
- function quickSort(arr[], left, right) {
- let i = left, j = right;
- const pivot = arr[(left + right) / 2];
- while (i <= j) {
- while (arr[i] < pivot) i++;
- while (arr[j] > pivot) j--;
- if (i <= j) {
- swap(arr[i], arr[j]);
- i++;
- j--;
- }
- }
- if (left < j)
- quickSort(arr, left, j);
- if (i < right)
- quickSort(arr, i, right);
- }
- function bubbleSort(arr[], l) {
- for (let i = 0; i < l; i++)
- for (let j = (l - 1); j >= (i + 1); j--) {
- if (arr[j] < arr[j - 1])
- swap(arr[j], arr[j - 1]);
- }
- }
- function selectionSort(arr[], l) {
- let min;
- for (let i = 0; i < l-1; i++) {
- min = i;
- for (let j = i+1; j < l; k++)
- if (arr[min] > arr[j])
- min = j;
- swap(arr[i], arr[min]);
- }
- }
- function insertionSort(arr[], l) {
- let k = 0, i = 0;
- for (let j = 1; j < l; j++) {
- k = arr[j];
- i = j - 1;
- while (i >= 0 && arr[i] > k) {
- arr[i + 1] = arr[i];
- i -= 1;
- arr[i + 1] = k;
- }
- }
- }
- const merge = (arrFirst, arrSecond) => {
- const arrSort = [];
- let i = j = 0;
- while (i < arrFirst.length && j < arrSecond.length) {
- arrSort.push(
- (arrFirst[i] < arrSecond[j]) ?
- arrFirst[i++] : arrSecond[j++]
- );
- }
- return [
- ...arrSort,
- ...arrFirst.slice(i),
- ...arrSecond.slice(j)
- ];
- };
- const mergeSort = arr => {
- if (!arr || !arr.length) {
- return null;
- }
- if (arr.length <= 1) {
- return arr;
- }
- const middle = Math.floor(arr.length / 2);
- const arrLeft = arr.slice(0, middle);
- const arrRight = arr.slice(middle);
- return merge(mergeSort(arrLeft), mergeSort(arrRight));;
- };
- class BinarySearchTree {
- constructor(){
- this.root = null;
- }
- add(value){
- let newNode = { value, left: null, right: null};
- // set the root if we don't have one
- if(this.root == null){
- this.root = newNode;
- return;
- }
- let current = this.root;
- while(true){
- if(value > current.value){
- if(!current.right){ current.right = newNode; break; }
- current = current.right;
- } else if(value < current.value){
- if(!current.left){ current.left = newNode; break; }
- current = current.left;
- } else {
- break;
- }
- }
- }
- contains(value){
- let current = this.root;
- while(current){
- if(current.value == value){
- return true;
- } else if (current.value > value){
- current = current.left;
- } else {
- current = current.right;
- }
- }
- return false;
- }
- }
- class DepthFirstTraverser extends BinarySearchTree {
- consstructor(){
- this.traverseMethod = 'pre-order';
- }
- setTraverseMethod(traverseMethod){
- if(traverseMethod == 'pre-order' || traverseMethod == 'in-order' || traverseMethod == 'post-order'){
- this.traverseMethod = traverseMethod;
- } else {
- console.error('Not a valid search method, must be "pre-order", "in-order" or "post-order"');
- }
- }
- getTraverseMethod(){
- return this.traverseMethod;
- }
- traverse(){
- switch(this.traverseMethod){
- case 'pre-order':
- this.preOrderTraverse(value);
- break;
- case 'in-order':
- this.inOrderTraverse(value);
- break;
- case 'post-order':
- this.postOrderTraverse(value);
- break;
- default:
- console.error('invalid traverse method');
- }
- }
- preOrderTraverse(value){
- if(value){
- console.log(value.value);
- preOrderTraverse(value.left);
- preOrderTraverse(value.right);
- }
- inOrderTraverse(value){
- }
- postOrderTraverse(value){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement