Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive function, maintain a global counter without using a global variable

I'm writing functions to serialize and deserialize a binary tree (just converting it to an array and back to a BT) and I'm having a hard time writing the deserializing function without using a global variable to keep track of the index.

my Node class looks like this:

class Node {
  constructor(value) {
    this.value = value
    this.left = null
    this.right = null
  }
}

and my serialize method looks like this:

function serialize(node = this, list = []) {
  if (!node) {
    list.push('#')
    return
  }

  list.push(node.value)
  serialize(node.left, list)
  serialize(node.right, list)
  return list
}

Here's my problem, in the deserialize function I have to keep a global variable for the 'index', is there a way to keep the global value for the index without creating a global variable?

let index = 0

function deserialize(list) {
  if (index === list.length || list[index] === '#') {
    index++
    return
  }

  const tree = new Node(list[index])
  index++

  tree.left = deserialize(list)
  tree.right = deserialize(list)

  return tree
}

I initially wrote the function like this: (but I had to refactor the index variable to a global variable)

function deserialize(list, index = 0) {
  if (index === list.length || list[index] === '#') {
    index++
    return
  }

  const tree = new Node(list[index])
  index++

  tree.left = deserialize(list, index)
  tree.right = deserialize(list, index)

  return tree
}

This ended up with a symetric tree because the tree.left and tree.right would always take the same index.

I'm just curious if there is a simple way to keep track of the index in a purely recursive function (not using a recursive subroutine) without creating a global variable.

like image 251
jmancherje Avatar asked Jul 15 '26 03:07

jmancherje


1 Answers

A general way to do that:

function wrapperFunction(args) {
  var bookkeepingStuff = whatever;
  function actualRecursiveFunction(args) {
    // code
  }

  return actualRecursiveFunction(args);
}

Just wrap the real recursive function in another function. Any local variables of the wrapper will be effectively like global variables to the real recursive function nested inside.

like image 184
Pointy Avatar answered Jul 17 '26 18:07

Pointy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!