-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckTypeOfBinaryTree.java
More file actions
148 lines (116 loc) · 3.76 KB
/
Copy pathCheckTypeOfBinaryTree.java
File metadata and controls
148 lines (116 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Author : Hasnain Memon
* Date : 14/12/2024
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class CheckTypeOfBinaryTree {
private Node root;
static class Node {
int data;
Node left, right;
public Node(int data) {
this.data = data;
left = right = null;
}
}
public void printPreOrder(Node node) {
if (node == null) {
return;
}
// first print data of node
System.out.print(node.data + " ");
// then recur on left subtree
printPreOrder(node.left);
// then recur on right subtree
printPreOrder(node.right);
}
public void printInOrder(Node node) {
if (node == null) {
return;
}
// first recur on left subtree
printInOrder(node.left);
// then print data of node
System.out.print(node.data + " ");
// then recur on right subtree
printInOrder(node.right);
}
public void printPostOrder(Node node) {
if (node == null) {
return;
}
// first recur on left subtree
printPostOrder(node.left);
// then recur on right subtree
printPostOrder(node.right);
// then print data of node
System.out.print(node.data + " ");
}
// Task 2: Check whether a given tree is complete tree, full tree or both.
public void checkTypeOfTree(Node root) {
if (root == null) {
System.out.println("Given tree is empty!");
return;
}
boolean isFullTree = isFullTree(root);
boolean isCompleteTree = isCompleteTree(root);
if (isCompleteTree && isFullTree) {
System.out.println("Given tree is Full Tree as well as Complete Tree");
} else if (isFullTree) {
System.out.println("Given tree is Full Tree");
} else if (isCompleteTree) {
System.out.println("Given tree is Complete Tree");
}
}
public boolean isFullTree(Node root) {
if (root == null) {
return true;
}
if ((root.left != null && root.right == null) || (root.right != null && root.left == null)) {
return false;
}
return isFullTree(root.left) && isFullTree(root.right);
}
public boolean isCompleteTree(Node root) {
if (root == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
boolean gotNull = false;
while (!queue.isEmpty()) {
Node current = queue.poll();
if (current == null) {
gotNull = true;
} else {
if (gotNull) {
return false;
}
queue.add(current.left);
queue.add(current.right);
}
}
return true;
}
public static void main(String[] args) {
CheckTypeOfBinaryTree bt = new CheckTypeOfBinaryTree();
bt.root = new Node(5);
bt.root.left = new Node(4);
bt.root.right = new Node(11);
bt.root.left.left = new Node(2);
bt.root.left.right = new Node(9);
System.out.print("Pre-Order Traversal: ");
bt.printPreOrder(bt.root);
System.out.println();
System.out.print("In-Order Traversal: ");
bt.printInOrder(bt.root);
System.out.println();
System.out.print("Post-Order Traversal: ");
bt.printPostOrder(bt.root);
System.out.println();
// Task 2
bt.checkTypeOfTree(bt.root);
}
}