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

refactor: BFS tree algorithms #1108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
raklaptudirm merged 9 commits into TheAlgorithms:master from kho-kho-kho:master
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions Trees/BreadthFirstTreeTraversal.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,33 @@ class Node {
class BinaryTree {
constructor () {
this.root = null
this.traversal = []
}

breadthFirstIterative () {
this.traversal = []
const traversal = []
if (this.root) {
this.traversal.push(this.root)
traversal.push(this.root)
}
for (let i = 0; i < this.traversal.length; i++) {
const currentNode = this.traversal[i]
for (let i = 0; i < traversal.length; i++) {
const currentNode = traversal[i]
if (currentNode.left) {
this.traversal.push(currentNode.left)
traversal.push(currentNode.left)
}
if (currentNode.right) {
this.traversal.push(currentNode.right)
traversal.push(currentNode.right)
}
this.traversal[i] = currentNode.data
traversal[i] = currentNode.data
}
return this.traversal
return traversal
}

breadthFirstRecursive () {
this.traversal = []
const traversal = []
const h = this.getHeight(this.root)
for (let i = 0; i !== h; i++) {
this.traverseLevel(this.root, i)
this.traverseLevel(this.root, i, traversal)
}
return this.traversal
return traversal
}

// Computing the height of the tree
Expand All @@ -54,15 +53,15 @@ class BinaryTree {
return lheight > rheight ? lheight + 1 : rheight + 1
}

traverseLevel (node, levelRemaining) {
traverseLevel (node, levelRemaining, traversal) {
if (node === null) {
return
}
if (levelRemaining === 0) {
this.traversal.push(node.data)
traversal.push(node.data)
} else {
this.traverseLevel(node.left, levelRemaining - 1)
this.traverseLevel(node.right, levelRemaining - 1)
this.traverseLevel(node.left, levelRemaining - 1, traversal)
this.traverseLevel(node.right, levelRemaining - 1, traversal)
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions Trees/test/BreadthFirstTreeTraversal.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@ describe('Breadth First Tree Traversal', () => {
root.right = new Node(8)
root.left.left = new Node(3)
root.left.right = new Node(6)
root.right.right = new Node(9)
root.left.right.right = new Node(9)
binaryTree.root = root

// Visualization :
//
// 7
// / \
// 5 8
// / \ \
// 3 6 9
// / \
// 3 6
// \
// 9

it('Binary tree - Empty case', () => {
const emptyTree = new BinaryTree()
expect(emptyTree.breadthFirstIterative()).toStrictEqual([])
})

it('Binary tree - Level order recursive traversal', () => {
expect(binaryTree.traversal).toStrictEqual([])
const traversal = binaryTree.breadthFirstRecursive()
expect(traversal).toStrictEqual([7, 5, 8, 3, 6, 9])
})
Expand Down

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