What's a "<ref *1>"?
Asked Answered
I

3

11

As per the topic, I couldn't find anything specific about this on the internet.

What is <ref *1> in the output I get before the class name even though I call the property "tree"? Reference... to what, and why if I call property? And how to fix it?

CLI Output:

> $ node binary-search-tree.js                                                                                                                                  
<ref *1> BinarySearchTree { tree: [Circular *1] }

This is my code (learning algorithms):

const addNode = (base, num) => {
  // base = {number, left?, right?}
  if (num <= base.number) {
    if (base.left) {
      base.left = addNode(base.left, num);
    } else base.left = { number: num };
  }
  if (num > base.number) {
    if (base.right) {
      base.right = addNode(base.right, num);
    } else base.right = { number: num };
  }

  return base;
};

class BinarySearchTree {
  constructor(baseValue) {
    this.tree = { number: baseValue };
  }

  get data() {
    return this.tree;
  }

  get right() {
    throw new Error("Remove this statement and implement this function");
  }

  get left() {
    throw new Error("Remove this statement and implement this function");
  }

  insert(nums) {
    if (typeof nums === "number") {
      this.tree = addNode(this, nums);
      return;
    }
    for (let number of nums) {
      this.tree = addNode(this, number);
    }
  }

  each() {
    throw new Error("Remove this statement and implement this function");
  }
}

const lolTree = new BinarySearchTree(5);
lolTree.insert(58);
lolTree.insert([2, 7, 4, 100]);
lolTree.insert(55);

console.log(lolTree.tree);
Inglebert answered 1/10, 2021 at 12:56 Comment(0)
D
10

This is reference index for showing a circular reference.

Meaning that, there's some circular structure in your object.

You can also see it's circular by running:

JSON.stringify(lolTree.tree)

which will result in:

VM829:1 Uncaught TypeError: Converting circular structure to JSON --> starting at object with constructor 'BinarySearchTree' --- property 'tree' closes the circle at JSON.stringify ()

This happens, when object's property refers to the object itself. Consider the code:

// define some object
const a = { foo: 1 };

// add a property referring to the object
a.bar = a;

console.log(JSON.stringify(a)); // TypeError
Drudge answered 1/10, 2021 at 13:9 Comment(3)
it looks like the problem is in addNode. where you assign base.left the return value of addNode whcih is base itself. I am not certain that addNode sould return anything or that assignment needs to take placeVikiviking
weird, because before when I used a log to console it didn't show like that... but I had proper output (proper tree object/string of JSON if used stringify) - but I'll fight with addNode then... but output was good thoInglebert
yeah, in the addNode, the base comes as undefinedDrudge
I
1

I feel dumb... I left in insert() two this, not this.tree. Thanks, @Aziza for explaining this <ref *1> to me!

Inglebert answered 1/10, 2021 at 13:29 Comment(0)
V
0

addNode should probably be:

const addNode = (base, num) => {
  // base = {number, left?, right?}
  if (num <= base.number) {
    if (base.left) {
       addNode(base.left, num);
    } else {
       base.left = { number: num };
    }
  }
  else {
    if (base.right) {
       addNode(base.right, num);
    } else {
       base.right = { number: num };
    }
  }
};

to avoid the circular reference mentioned by @aziza; edit: changed second if to an else - it is either left or right, so if it isn't left...

Vikiviking answered 1/10, 2021 at 13:15 Comment(2)
thanks for that "else" good catch, but returning base is necessary for this recursion and adding "addNode" to "base.left/right" too because how else I can get nested into "base.left/right"?Inglebert
i don't think it is if you pass hte parent downVikiviking

© 2022 - 2024 — McMap. All rights reserved.