Refactor Level Order Traversal (#3869)

This commit is contained in:
Albina Gimaletdinova
2023-02-14 13:33:14 +03:00
committed by GitHub
parent c0fec8dfe2
commit a584ca248c
7 changed files with 154 additions and 98 deletions

View File

@ -1,58 +1,38 @@
package com.thealgorithms.datastructures.trees;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class LevelOrderTraversal {
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
// 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);
}
}
/* 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;
}
}

View File

@ -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);
}
}
}

View File

@ -1,45 +0,0 @@
package com.thealgorithms.datastructures.trees;
import java.util.LinkedList;
import java.util.Queue;
/* Class to print Level Order Traversal */
public class LevelOrderTraversalQueue {
/* Class to represent Tree node */
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = null;
right = null;
}
}
/* Given a binary tree. Print its nodes in level order
using array for implementing queue */
void printLevelOrder(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
/* poll() removes the present head.
For more information on poll() visit
http://www.tutorialspoint.com/java/util/linkedlist_poll.htm */
Node tempNode = queue.poll();
System.out.print(tempNode.data + " ");
/*Enqueue left child */
if (tempNode.left != null) {
queue.add(tempNode.left);
}
/*Enqueue right child */
if (tempNode.right != null) {
queue.add(tempNode.right);
}
}
}
}

View File

@ -32,7 +32,7 @@ import java.util.*;
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<>();