0
0
DSA Javascriptprogramming~20 mins

Why Trees Exist and What Linked Lists and Arrays Cannot Do in DSA Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tree Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use trees instead of arrays or linked lists?

Which of the following is a key reason trees are used instead of arrays or linked lists?

ATrees do not require pointers or references to connect elements.
BTrees use less memory than arrays and linked lists for storing the same data.
CTrees always store data in a linear order like arrays and linked lists.
DTrees allow faster searching in sorted data compared to arrays and linked lists.
Attempts:
2 left
💡 Hint

Think about how quickly you can find an item in a sorted list versus a tree.

Predict Output
intermediate
2:00remaining
Output of tree traversal vs array traversal

What is the output of the following JavaScript code that traverses a simple binary tree and an array?

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

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);

function inorder(node) {
  if (!node) return [];
  return [...inorder(node.left), node.value, ...inorder(node.right)];
}

const arr = [1, 2, 3];

console.log(inorder(root));
console.log(arr);
A
[2, 1, 3]
[1, 2, 3]
B
[1, 2, 3]
[1, 2, 3]
C
[2, 3, 1]
[3, 2, 1]
D
[3, 1, 2]
[1, 2, 3]
Attempts:
2 left
💡 Hint

Inorder traversal visits left child, then node, then right child.

🔧 Debug
advanced
2:00remaining
Why does this linked list fail to represent hierarchical data?

Consider this linked list code trying to represent a family tree. Why does it fail to represent the hierarchy properly?

DSA Javascript
class ListNode {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const head = new ListNode('Grandparent');
head.next = new ListNode('Parent');
head.next.next = new ListNode('Child');

// Trying to represent family tree with linked list
AThe code has a syntax error in the constructor.
BLinked lists can only represent linear sequences, not branching hierarchies.
CLinked lists require a 'parent' pointer to represent hierarchies.
DThe linked list nodes must have 'left' and 'right' properties to work.
Attempts:
2 left
💡 Hint

Think about how family trees branch out compared to a single line.

Predict Output
advanced
2:00remaining
Output of searching in array vs tree

What is the output of the following JavaScript code that searches for the number 5 in an array and a binary search tree?

DSA Javascript
const arr = [1, 3, 5, 7, 9];

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

const root = new TreeNode(7);
root.left = new TreeNode(3);
root.right = new TreeNode(9);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(5);

function searchArray(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return true;
  }
  return false;
}

function searchTree(node, target) {
  if (!node) return false;
  if (node.value === target) return true;
  if (target < node.value) return searchTree(node.left, target);
  else return searchTree(node.right, target);
}

console.log(searchArray(arr, 5));
console.log(searchTree(root, 5));
A
true
true
B
false
true
C
true
false
D
false
false
Attempts:
2 left
💡 Hint

Both array and tree contain the number 5.

🧠 Conceptual
expert
2:30remaining
Why are trees better for representing hierarchical data?

Which statement best explains why trees are preferred over arrays and linked lists for hierarchical data?

ATrees use less memory than arrays and linked lists for the same data.
BArrays and linked lists can only store numbers, not objects or nodes.
CTrees allow multiple child nodes per parent, enabling natural representation of hierarchies.
DArrays and linked lists cannot be traversed or searched.
Attempts:
2 left
💡 Hint

Think about how family trees or company structures branch out.