Skip to content

Level order traversal refactoring, added unit test #3869

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
merged 2 commits into from
Feb 14, 2023
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
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
package com.thealgorithms.datastructures.trees;

public class LevelOrderTraversal {

class Node {

int data;
Node left, right;

public Node(int item) {
data = item;
left = right = null;
}
}
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

// Root of the Binary Tree
Node root;

public LevelOrderTraversal(Node root) {
this.root = root;
}

/* function to print level order traversal of tree*/
void printLevelOrder() {
int h = height(root);
int i;
for (i = 1; i <= h; i++) {
printGivenLevel(root, i);
}
}
public class LevelOrderTraversal {

/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int height(Node root) {
static List<List<Integer>> traverse(BinaryTree.Node root) {
if (root == null) {
return 0;
} else {
/**
* Return the height of larger subtree
*/
return Math.max(height(root.left), height(root.right)) + 1;
return List.of();
}
}

/* Print nodes at the given level */
void printGivenLevel(Node root, int level) {
if (root == null) {
return;
}
if (level == 1) {
System.out.print(root.data + " ");
} else if (level > 1) {
printGivenLevel(root.left, level - 1);
printGivenLevel(root.right, level - 1);
List<List<Integer>> result = new ArrayList<>();

Queue<BinaryTree.Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int nodesOnLevel = q.size();
List<Integer> level = new LinkedList<>();
for (int i = 0; i < nodesOnLevel; i++) {
BinaryTree.Node tempNode = q.poll();
level.add(tempNode.data);

if (tempNode.left != null) {
q.add(tempNode.left);
}

if (tempNode.right != null) {
q.add(tempNode.right);
}
}
result.add(level);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.thealgorithms.datastructures.trees;

public class LevelOrderTraversalHelper {
/* function to print level order traversal of tree*/
public static void printLevelOrder(BinaryTree.Node root) {
if (root == null) {
System.out.println("Root node must not be null! Exiting.");
return;
}

int h = height(root);
int i;
for (i = 1; i <= h; i++) {
printGivenLevel(root, i);
}
}

/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
private static int height(BinaryTree.Node root) {
if (root == null) {
return 0;
} else {
//return the height of larger subtree
return Math.max(height(root.left), height(root.right)) + 1;
}
}

/* Print nodes at the given level */
public static void printGivenLevel(BinaryTree.Node root, int level) {
if (root == null) {
System.out.println("Root node must not be null! Exiting.");
return;
}
if (level == 1) {
System.out.print(root.data + " ");
} else if (level > 1) {
printGivenLevel(root.left, level - 1);
printGivenLevel(root.right, level - 1);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class ZigzagTraversal {
public static List<List<Integer>> traverse(BinaryTree.Node root) {
if (root == null) {
return new ArrayList<>();
return List.of();
}

List<List<Integer>> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.thealgorithms.datastructures.trees;

import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Albina Gimaletdinova on 08/02/2023
*/
public class LevelOrderTraversalTest {
@Test
public void testRootNull() {
assertEquals(Collections.emptyList(), LevelOrderTraversal.traverse(null));
}

@Test
public void testSingleNodeTree() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{50});
assertEquals(List.of(List.of(50)), LevelOrderTraversal.traverse(root));
}

/*
1
/ \
2 3
/\ /\
4 5 6 7
*/
@Test
public void testLevelOrderTraversalCompleteTree() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
assertEquals(List.of(List.of(1), List.of(2, 3), List.of(4, 5, 6, 7)), LevelOrderTraversal.traverse(root));
}

/*
1
/ \
2 3
/\ /\
4 5 6 7
/ \
8 9
*/
@Test
public void testLevelOrderTraversalDifferentHeight() {
final BinaryTree.Node root = TreeTestUtils.createTree(
new Integer[]{1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
assertEquals(List.of(List.of(1), List.of(2, 3), List.of(4, 5, 6, 7), List.of(8, 9)),
LevelOrderTraversal.traverse(root));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testVerticalTraversalCompleteTree() {
/ \
2 3
/\ /\
4 56 7
4 5 6 7
/ \
8 9
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,33 @@ public void testSingleNodeTree() {
assertEquals(List.of(List.of(50)), ZigzagTraversal.traverse(root));
}

/*
1
/ \
2 3
/\ /\
4 5 6 7
*/
@Test
public void testZigzagTraversal() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{7, 6, 14, 2, 80, 100});
assertEquals(List.of(List.of(7), List.of(14, 6), List.of(2, 80, 100)), ZigzagTraversal.traverse(root));
public void testZigzagTraversalCompleteTree() {
final BinaryTree.Node root = TreeTestUtils.createTree(new Integer[]{1, 2, 3, 4, 5, 6, 7});
assertEquals(List.of(List.of(1), List.of(3, 2), List.of(4, 5, 6, 7)), ZigzagTraversal.traverse(root));
}

/*
1
/ \
2 3
/\ /\
4 5 6 7
/ \
8 9
*/
@Test
public void testZigzagTraversalDifferentHeight() {
final BinaryTree.Node root = TreeTestUtils.createTree(
new Integer[]{1, 2, 3, 4, 5, 6, 7, null, null, 8, null, null, 9});
assertEquals(List.of(List.of(1), List.of(3, 2), List.of(4, 5, 6, 7), List.of(9, 8)),
ZigzagTraversal.traverse(root));
}
}