Remove redundant tree traversals (#4161)

This commit is contained in:
Albina Gimaletdinova 2023-04-21 11:41:24 +03:00 committed by GitHub
parent 1551b8f50b
commit c01a382d94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 164 deletions

View File

@ -7,7 +7,7 @@ import java.util.Queue;
public class LevelOrderTraversal { public class LevelOrderTraversal {
static List<List<Integer>> traverse(BinaryTree.Node root) { public static List<List<Integer>> traverse(BinaryTree.Node root) {
if (root == null) { if (root == null) {
return List.of(); return List.of();
} }
@ -35,4 +35,18 @@ public class LevelOrderTraversal {
} }
return result; return result;
} }
/* 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,43 +0,0 @@
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,120 +0,0 @@
package com.thealgorithms.datastructures.trees;
import java.util.LinkedList;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
// Driver Program
public class TreeTraversal {
public static void main(String[] args) {
Node tree = new Node(5);
tree.insert(3);
tree.insert(2);
tree.insert(7);
tree.insert(4);
tree.insert(6);
tree.insert(8);
// Prints 5 3 2 4 7 6 8
System.out.println("Pre order traversal:");
tree.printPreOrder();
System.out.println();
// Prints 2 3 4 5 6 7 8
System.out.println("In order traversal:");
tree.printInOrder();
System.out.println();
// Prints 2 4 3 6 8 7 5
System.out.println("Post order traversal:");
tree.printPostOrder();
System.out.println();
// Prints 5 3 7 2 4 6 8
System.out.println("Level order traversal:");
tree.printLevelOrder();
System.out.println();
}
}
/**
* The Node class which initializes a Node of a tree Consists of all 4 traversal
* methods: printInOrder, printPostOrder printPreOrder & printLevelOrder
* printInOrder: LEFT -> ROOT -> RIGHT printPreOrder: ROOT -> LEFT -> RIGHT
* printPostOrder: LEFT -> RIGHT -> ROOT printLevelOrder: Prints by level
* (starting at root), from left to right.
*/
class Node {
Node left, right;
int data;
public Node(int data) {
this.data = data;
}
public void insert(int value) {
if (value < data) {
if (left == null) {
left = new Node(value);
} else {
left.insert(value);
}
} else {
if (right == null) {
right = new Node(value);
} else {
right.insert(value);
}
}
}
public void printInOrder() {
if (left != null) {
left.printInOrder();
}
System.out.print(data + " ");
if (right != null) {
right.printInOrder();
}
}
public void printPreOrder() {
System.out.print(data + " ");
if (left != null) {
left.printPreOrder();
}
if (right != null) {
right.printPreOrder();
}
}
public void printPostOrder() {
if (left != null) {
left.printPostOrder();
}
if (right != null) {
right.printPostOrder();
}
System.out.print(data + " ");
}
/**
* O(n) time algorithm. Uses O(n) space to store nodes in a queue to aid in
* traversal.
*/
public void printLevelOrder() {
LinkedList<Node> queue = new LinkedList<>();
queue.add(this);
while (queue.size() > 0) {
Node head = queue.remove();
System.out.print(head.data + " ");
// Add children of recently-printed node to queue, if they exist.
if (head.left != null) {
queue.add(head.left);
}
if (head.right != null) {
queue.add(head.right);
}
}
}
}