Return the k most frequent text nodes in a DOM tree
Given a DOM tree representing a webpage and a limit (K), write a function to find the K most frequently appeared text nodes in the webpage. A text node is considered as a string of non-empty characters that are not equal to ‘↵’. Ignore other node types (e.g., element nodes) while counting the frequency. Return an array containing the K most frequent text nodes in descending order of their frequencies. TreeWalker is one way to visit every text node.
Example:
Consider the following webpage HTML:
<div id="root">
<p>This is a paragraph.</p>
<div>
<span>This is some text.</span>
<p>Another paragraph here.</p>
</div>
<p>This is another paragraph.</p>
</div>
Input:
The function findKFrequentWordsInTree(tree, limit) should analyze the text nodes within the given DOM tree and return the K (3 in this example) most frequently occurring text nodes.
The expected output for this example, assuming the function correctly identifies and counts the frequency of text nodes, might be an array like ['This', 'is', 'a'], reflecting the most common text nodes within the specified limit.
This task tests your ability to traverse and process DOM trees, count frequency of specific nodes, and return a sorted list of these nodes based on their frequency, which are essential skills for manipulating web page content dynamically.