0

I am trying to solve an extra credit problem using recursion. Basically there is a "tree" that has matching "leaves" and I need to use recursion to check those matching leaves and return true if they match and return false if they do not.

I have no idea how to do this and no materials I can find are helping me understand recursion any better. This is for an online program to learn how to program.

Any help would be appreciated!

Psuedo:

    // initialize some value
      // initialize some flag.. boolean
      // initialize some helper function and pass obj...leaf checker recursive function
        // check all the keys ...for loop/forEach
          // if a key is an object && value is undefined
            // assign value
            // return
          // if a value is an object ==> recurse
          // if a value is found and it doesn't match our initial value
          // trip our flag to false
          // return
      // return true or false

 const checkMatchingLeaves = (obj) => {

};

My attempt:

const checkMatchingLeaves = (obj) => {
  // return true if every property on `obj` is the same
  // otherwise return false
  let  checker = Object.values(obj);
  if (Object.values(obj).length === 1) return true; 
    if (checker.map(checker[i] === checker[i + 1])) {
      i > checker.length; i++;
    }
};
3
  • 1
    The absolute basic idea behind recursion is that a function calls itself. Look up the fibonacci function. It's a very common example of a recursive function. Commented Dec 8, 2017 at 17:40
  • @FaizaHusain a recursive function calls itself. Assuming that you have an array full of sub-arrays or primitives you want something like this: const searchTree = (val, tree) => tree.some(node => Array.isArray(node) ? searchTree(node) : val === node); For example searchTree(3, [[1, 2], 4, [[3]]]); returns true and searchTree('abc', ['ab', 'bc', ['a'], [['b']]]); returns false. Commented Dec 8, 2017 at 17:51
  • NOTE: should be searchTree(val, node) in the inner call. Commented Dec 8, 2017 at 17:59

1 Answer 1

1

This isn't exactly what (I think) you're asking for, but you should be able to use it as a template to figure out what to do:

// searchTree takes a value to try to match and an array/primitive 
// value.
function searchTree(val, node) {
  // Check if it's a value or an array. If it's a value we can
  // do the test and return, otherwise we recursively call
  // searchTree on all the children.
  // Array.some returns true if any of the function calls return
  // true. This is a shorthand for your boolean flag: it lets us
  // return early as soon as we find a match.
  return Array.isArray(node) ? 
    node.some(child => searchTree(val, child)) : // recursive call
    val === node;
}

searchTree(3, [1, 2, [8], [[[3]]]]);      // true
searchTree('abc', 'a');                   // false
searchTree('abc', ['ab', 'bc', ['abc']]); // true 

This is a DFS search implementation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.