0

How to create a binary tree in node.js by taking input from the console node by node and form the tree?

wanted to create something like this:

     4
    / \
   6   5
  / \  
 8   2
Enter Root Node: 4
Left Child of 4: 6
Left Child of 6: 8
Left Child of 8: *
Right Child of 6: 2
Right Child of 4: 5
Left Child of 5: *
Right Child of 5: *
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});



function binaryTree() {
    let root = null;

    function Node(element) {
        this.element = element;
        this.left = null;
        this.right = null;
    }

    function create(question) {
        rl.question(question, (answer) => {
            

            if(answer == "*") {
                return "*";
            }

            if(answer.includes("exit")) {
                answer = answer.split(" ")[0];
                rl.close();
                if(answer == "*") {
                    return "*";
                }
            }

            let newNode = new Node(answer);
            if(root === null) root = newNode;
            newNode.left = create("Left Child of " + answer + ": ");
            newNode.right = create("Right Child of " + answer + ": ");
            
            return newNode;


        
        });
    }

    function preOrder(head) {
        console.log("head: " + head);
        if( head == "*" || head == null ) {
            return;
        }

        console.log(head.element);
        preOrder(head.left);
        preOrder(head.right);
    }

    function getRoot() {
        return root;
    }

    return {  create, preOrder, getRoot };
}

let bTree = binaryTree();
let bTree_root = bTree.getRoot();

bTree.create("Root Value: ");

The above code is not working as expected. I think it is due to asynchronous and recursion.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    What is the code doing that you don't expect? – Welbog Jun 05 '21 at 18:20
  • 1
    you have a misunderstanding of nested functions and methods of instances. you can not access a nesting function by using them with a dot. it works only by calling the function and inside the function if call a function of the same scope. – Nina Scholz Jun 05 '21 at 18:20
  • I think the recursion is fine. Yes, the issue is trying to `return` from an asynchronous callback - use promises and `async`/`await to fix that. Also you're calling `bTree.getRoot()` before creating the tree… – Bergi Jun 05 '21 at 19:54
  • @NinaScholz I tried to use closure so that the linked list will be in lexical scope of create function. – akash9521 Jun 06 '21 at 06:47
  • @Bergi I think the issue is with ```rl.question``` since this will return void but I need a node to be returned from create function. Is it possible? because the execution context of create function will be removed from the call stack when the async code of input compile. – akash9521 Jun 06 '21 at 06:50
  • @akash9521 As I said, use a promise for that. See also the canonical questions – Bergi Jun 06 '21 at 09:06

0 Answers0