Skip to content

Commit fad3e2a

Browse files
authored
Merge branch 'master' into master
2 parents e58f70e + 351e85d commit fad3e2a

File tree

9 files changed

+551
-221
lines changed

9 files changed

+551
-221
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
/**
7+
* Given 2 binary trees.
8+
* This code checks whether they are the same (structurally identical and have the same values) or not.
9+
* <p>
10+
* Example:
11+
* 1. Binary trees:
12+
* 1 1
13+
* / \ / \
14+
* 2 3 2 3
15+
* /\ /\ /\ /\
16+
* 4 5 6 7 4 5 6 7
17+
* These trees are the same, so the code returns 'true'.
18+
* <p>
19+
* 2. Binary trees:
20+
* 1 1
21+
* / \
22+
* 2 2
23+
* These trees are NOT the same (the structure differs), so the code returns 'false'.
24+
* <p>
25+
* This solution implements the breadth-first search (BFS) algorithm.
26+
* For each tree we create a queue and iterate the trees using these queues.
27+
* On each step we check the nodes for equality, and if the nodes are not the same, return false.
28+
* Otherwise, add children nodes to the queues and continue traversing the trees.
29+
* <p>
30+
* Complexities:
31+
* O(N) - time, where N is the number of nodes in a binary tree,
32+
* O(N) - space, where N is the number of nodes in a binary tree.
33+
*
34+
* @author Albina Gimaletdinova on 13/01/2023
35+
*/
36+
public class SameTreesCheck {
37+
public static boolean check(BinaryTree.Node p, BinaryTree.Node q) {
38+
if (p == null && q == null) {
39+
return true;
40+
}
41+
if (p == null || q == null) {
42+
return false;
43+
}
44+
45+
Deque<BinaryTree.Node> q1 = new ArrayDeque<>();
46+
Deque<BinaryTree.Node> q2 = new ArrayDeque<>();
47+
q1.add(p);
48+
q2.add(q);
49+
while (!q1.isEmpty() && !q2.isEmpty()) {
50+
BinaryTree.Node first = q1.poll();
51+
BinaryTree.Node second = q2.poll();
52+
// check that some node can be null
53+
// if the check is true: both nodes are null or both nodes are not null
54+
if (!equalNodes(first, second)) return false;
55+
56+
if (first != null) {
57+
if (!equalNodes(first.left, second.left)) return false;
58+
if (first.left != null) {
59+
q1.add(first.left);
60+
q2.add(second.left);
61+
}
62+
63+
if (!equalNodes(first.right, second.right)) return false;
64+
if (first.right != null) {
65+
q1.add(first.right);
66+
q2.add(second.right);
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
73+
private static boolean equalNodes(BinaryTree.Node p, BinaryTree.Node q) {
74+
if (p == null && q == null) {
75+
return true;
76+
}
77+
if (p == null || q == null) {
78+
return false;
79+
}
80+
return p.data == q.data;
81+
}
82+
}

src/main/java/com/thealgorithms/datastructures/trees/VerticalOrderTraversal.java

+10-22
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,13 @@
2222
*/
2323
public class VerticalOrderTraversal {
2424

25-
public static void main(String[] args) {
26-
BinaryTree tree = new BinaryTree();
27-
tree.put(5);
28-
tree.put(6);
29-
tree.put(3);
30-
tree.put(1);
31-
tree.put(4);
32-
BinaryTree.Node root = tree.getRoot();
33-
ArrayList<Integer> ans = verticalTraversal(root);
34-
for (int i : ans) {
35-
System.out.print(i + " ");
25+
/*Function that receives a root Node and prints the tree
26+
in Vertical Order.*/
27+
public static ArrayList<Integer> verticalTraversal(BinaryTree.Node root) {
28+
if (root == null) {
29+
return new ArrayList<>();
3630
}
37-
}
3831

39-
/*Function that receives a root Node and prints the tree
40-
in Vertical Order.*/
41-
private static ArrayList<Integer> verticalTraversal(BinaryTree.Node root) {
4232
/*Queue to store the Nodes.*/
4333
Queue<BinaryTree.Node> queue = new LinkedList<>();
4434

@@ -84,21 +74,19 @@ private static ArrayList<Integer> verticalTraversal(BinaryTree.Node root) {
8474
to the respective ArrayList present at that
8575
index. */
8676
map.get(index.peek()).add(queue.peek().data);
87-
max = (int) Math.max(max, index.peek());
88-
min = (int) Math.min(min, index.peek());
89-
/*The Node and its index are removed
77+
max = Math.max(max, index.peek());
78+
min = Math.min(min, index.peek());
79+
/*The Node and its index are removed
9080
from their respective queues.*/
9181
index.poll();
9282
queue.poll();
9383
}
9484
/*Finally map data is printed here which has keys
95-
from min to max. Each ArrayList represents a
85+
from min to max. Each ArrayList represents a
9686
vertical column that is added in ans ArrayList.*/
9787
ArrayList<Integer> ans = new ArrayList<>();
9888
for (int i = min; i <= max; i++) {
99-
for (int j = 0; j < map.get(i).size(); j++) {
100-
ans.add(map.get(i).get(j));
101-
}
89+
ans.addAll(map.get(i));
10290
}
10391
return ans;
10492
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Given a binary tree.
7+
* This code returns the zigzag level order traversal of its nodes' values.
8+
* Binary tree:
9+
* 7
10+
* / \
11+
* 6 3
12+
* / \ / \
13+
* 2 4 10 19
14+
* Zigzag traversal:
15+
* [[7], [3, 6], [2, 4, 10, 19]]
16+
* <p>
17+
* This solution implements the breadth-first search (BFS) algorithm using a queue.
18+
* 1. The algorithm starts with a root node. This node is added to a queue.
19+
* 2. While the queue is not empty:
20+
* - each time we enter the while-loop we get queue size. Queue size refers to the number of nodes at the current level.
21+
* - we traverse all the level nodes in 2 ways: from left to right OR from right to left
22+
* (this state is stored on `prevLevelFromLeftToRight` variable)
23+
* - if the current node has children we add them to a queue
24+
* - add level with nodes to a result.
25+
* <p>
26+
* Complexities:
27+
* O(N) - time, where N is the number of nodes in a binary tree
28+
* O(N) - space, where N is the number of nodes in a binary tree
29+
*
30+
* @author Albina Gimaletdinova on 11/01/2023
31+
*/
32+
public class ZigzagTraversal {
33+
public static List<List<Integer>> traverse(BinaryTree.Node root) {
34+
if (root == null) {
35+
return new ArrayList<>();
36+
}
37+
38+
List<List<Integer>> result = new ArrayList<>();
39+
40+
// create a queue
41+
Deque<BinaryTree.Node> q = new ArrayDeque<>();
42+
q.offer(root);
43+
// start with writing nodes from left to right
44+
boolean prevLevelFromLeftToRight = false;
45+
46+
while (!q.isEmpty()) {
47+
int nodesOnLevel = q.size();
48+
List<Integer> level = new LinkedList<>();
49+
// traverse all the level nodes
50+
for (int i = 0; i < nodesOnLevel; i++) {
51+
BinaryTree.Node node = q.poll();
52+
if (prevLevelFromLeftToRight) {
53+
level.add(0, node.data);
54+
} else {
55+
level.add(node.data);
56+
}
57+
if (node.left != null) {
58+
q.offer(node.left);
59+
}
60+
if (node.right != null) {
61+
q.offer(node.right);
62+
}
63+
}
64+
// the next level node traversal will be from the other side
65+
prevLevelFromLeftToRight = !prevLevelFromLeftToRight;
66+
result.add(level);
67+
}
68+
return result;
69+
}
70+
}

0 commit comments

Comments
 (0)