Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5aa8bfa

Browse files
adding JSDOCS for BreadthFirstTreeTraversal.js and formatting the new added code
1 parent 96985a6 commit 5aa8bfa

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

‎Trees/BreadthFirstTreeTraversal.js‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
/*
2-
Breadth First Tree Traversal or level order traversal implementation in javascript
3-
Author: @GerardUbuntu
4-
*/
5-
1+
/**
2+
* Represents a node in a binary tree.
3+
*/
64
class Node {
5+
/**
6+
* Creates a new node with the specified data.
7+
* @param {*} data The data to be stored in the node.
8+
*/
79
constructor(data) {
810
this.data = data
911
this.left = null
1012
this.right = null
1113
}
1214
}
1315

16+
/**
17+
* Represents a binary tree data structure.
18+
*/
1419
class BinaryTree {
20+
/**
21+
* Creates a new binary tree with an empty root.
22+
*/
1523
constructor() {
1624
this.root = null
1725
}
1826

27+
/**
28+
* Performs breadth-first traversal of the binary tree iteratively.
29+
* @returns {Array} An array containing the data of nodes visited in breadth-first order.
30+
*/
1931
breadthFirstIterative() {
2032
const traversal = []
2133
if (this.root) {
@@ -34,6 +46,10 @@ class BinaryTree {
3446
return traversal
3547
}
3648

49+
/**
50+
* Performs breadth-first traversal of the binary tree recursively.
51+
* @returns {Array} An array containing the data of nodes visited in breadth-first order.
52+
*/
3753
breadthFirstRecursive() {
3854
const traversal = []
3955
const h = this.getHeight(this.root)
@@ -43,7 +59,11 @@ class BinaryTree {
4359
return traversal
4460
}
4561

46-
// Computing the height of the tree
62+
/**
63+
* Computes the height of the tree starting from the specified node.
64+
* @param {Node} node The node from which to compute the height.
65+
* @returns {number} The height of the tree.
66+
*/
4767
getHeight(node) {
4868
if (node === null) {
4969
return 0
@@ -53,6 +73,12 @@ class BinaryTree {
5373
return lheight > rheight ? lheight + 1 : rheight + 1
5474
}
5575

76+
/**
77+
* Traverses the specified level of the tree and adds nodes' data to the traversal array.
78+
* @param {Node} node The current node being traversed.
79+
* @param {number} levelRemaining The remaining level to traverse.
80+
* @param {Array} traversal The array to store the traversal result.
81+
*/
5682
traverseLevel(node, levelRemaining, traversal) {
5783
if (node === null) {
5884
return

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /