Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```javascript
- // Root node does not have a parent
- // Nodes have only one parent
- // Three does no have cicle
- // A
- // / \
- // B C
- // / \ / \
- // D E F G
- const root = new TreeNode({value: 'A'});
- const nodeB = root.setLeftNode('B');
- nodeB.setLeftNode('D');
- nodeB.setRightNode('E');
- const nodeC = root.setRightNode('C');
- nodeC.setLeftNode('F');
- nodeC.setRightNode('G');
- const binaryTree = new BinaryTree(root);
- // Deep First Search (DFS)
- binaryTree.preOrder();
- binaryTree.print(); // A B D E C F G
- // Deep First Search (DFS)
- binaryTree.postOrder();
- binaryTree.print(); // D E B F G C A
- // Deep First Search (DFS)
- binaryTree.inOrder();
- binaryTree.print(); // D B E A F C G
- // Breadth First Search (BFS)
- binaryTree.levelOrder();
- binaryTree.print(); // A B C D E F G
- function TreeNode({value, parentNode}) {
- this.value = value;
- this.parentNode = parentNode;
- this.leftNode = null;
- this.rightNode = null;
- this.visited = false;
- this.visit = () => {
- this.visited = true;
- return this;
- };
- this.getLeftChildren = () => this.leftNode;
- this.setLeftNode = leftNode => {
- this.leftNode = new TreeNode({value: leftNode, parentNode: this});
- return this.leftNode;
- };
- this.getRightChildren = () => this.rightNode;
- this.setRightNode = rightNode => {
- this.rightNode = new TreeNode({value: rightNode, parentNode: this});
- return this.rightNode;
- };
- }
- function BinaryTree(root) {
- this.root = root;
- this.nodesVisited = [];
- this.preOrder = () => {
- this.nodesVisited = [];
- this._preOrder(root);
- };
- this._preOrder = node => {
- if (node != null) {
- const nodeVisited = node.visit();
- this.nodesVisited.push(nodeVisited);
- this._preOrder(node.getLeftChildren());
- this._preOrder(node.getRightChildren());
- }
- };
- this.postOrder = () => {
- this.nodesVisited = [];
- this._postOrder(root);
- };
- this._postOrder = node => {
- if (node != null) {
- this._postOrder(node.getLeftChildren());
- this._postOrder(node.getRightChildren());
- const nodeVisited = node.visit();
- this.nodesVisited.push(nodeVisited);
- }
- };
- this.inOrder = () => {
- this.nodesVisited = [];
- this._inOrder(root);
- };
- this._inOrder = node => {
- if (node != null) {
- this._inOrder(node.getLeftChildren());
- const nodeVisited = node.visit();
- this.nodesVisited.push(nodeVisited);
- this._inOrder(node.getRightChildren());
- }
- };
- this.levelOrder = () => {
- this.nodesVisited = [];
- this._levelOrder(root);
- };
- this._levelOrder = node => {
- let current = null;
- this.tempNodesVisited = [];
- this.tempNodesVisited.push(node);
- while (this.tempNodesVisited.length !== 0) {
- current = this.tempNodesVisited.shift();
- this.nodesVisited.push(current);
- if (current.getLeftChildren() != null) {
- this.tempNodesVisited.push(current.getLeftChildren());
- }
- if (current.getRightChildren() != null) {
- this.tempNodesVisited.push(current.getRightChildren());
- }
- }
- };
- this.print = () => {
- console.log(this.nodesVisited.map(n => n.value));
- };
- }
- ```
Add Comment
Please, Sign In to add comment