Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added Binary Tree #96

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
ashokdey merged 4 commits into master from problems
Oct 17, 2019
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
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)

- [Trees](src/_DataStructures_/Trees)
- [Binary Tree (creation using level order)](src/_DataStructures_/Trees/BinaryTree)
- [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree)
- [Find k<sup>th</sup> maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max)
- [Find k<sup>th</sup> minimum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-min)
Expand Down
7 changes: 7 additions & 0 deletions src/_DataStructures_/Trees/BinaryTree/Node.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = class Node {
constructor(value) {
this.value = value;
this.leftChild = null; // will be a node
this.rightChild = null; // will be a node
}
};
51 changes: 51 additions & 0 deletions src/_DataStructures_/Trees/BinaryTree/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const Node = require('./Node');

class BinaryTree {
constructor(arr) {
if (!Array.isArray(arr) || !arr.length) {
throw new Error('Invalid argument to create a Binary Tre');
}
this.root = this.createBinaryTree((this.root = null), arr, 0);
}

// eslint-disable-next-line class-methods-use-this
createBinaryTree(root, arr, i) {
if (i < arr.length) {
// eslint-disable-next-line no-param-reassign
root = new Node(arr[i]);
// eslint-disable-next-line no-param-reassign
root.leftChild = this.createBinaryTree(root.leftChild, arr, 2 * i + 1);
// eslint-disable-next-line no-param-reassign
root.rightChild = this.createBinaryTree(root.rightChild, arr, 2 * i + 2);
}
return root;
}

traversePreorder(root) {
let arr = [];

if (root === null) return arr;
// push node to arr
arr.push(root.value);

// push left node
const left = this.traversePreorder(root.leftChild);
arr = [...arr, ...left];

// push right node
const right = this.traversePreorder(root.rightChild);
arr = [...arr, ...right];

return arr;
}

preOrder() {
return this.traversePreorder(this.root);
}
}

// const bt = new BinaryTree([1, 2, 3, 4, 5, 6]);
// console.log(bt.root);
// console.log(bt.preOrder());

module.exports = BinaryTree;

AltStyle によって変換されたページ (->オリジナル) /