-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompleteBinaryTree.java
More file actions
159 lines (131 loc) · 4.13 KB
/
CompleteBinaryTree.java
File metadata and controls
159 lines (131 loc) · 4.13 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
149
150
151
152
153
154
155
156
157
158
159
package datastructures;
import java.util.LinkedList;
import java.util.Queue;
public class CompleteBinaryTree {
// Definition of a binary tree node
static class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Root of the binary tree
private Node root;
// Constructor
public CompleteBinaryTree() {
root = null;
}
// Function to insert a node in the complete binary tree
public void insert(int data) {
Node newNode = new Node(data);
if (root == null) {
root = newNode;
return;
}
// Use a queue to perform level-order traversal
Queue<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Node current = queue.poll();
if (current.left == null) {
current.left = newNode;
break;
} else {
queue.add(current.left);
}
if (current.right == null) {
current.right = newNode;
break;
} else {
queue.add(current.right);
}
}
}
// Function for in-order traversal
public void inOrderTraversal() {
System.out.print("In-order Traversal: ");
inOrderTraversal(root);
System.out.println();
}
private void inOrderTraversal(Node root) {
if (root != null) {
inOrderTraversal(root.left);
System.out.print(root.data + " ");
inOrderTraversal(root.right);
}
}
// Function for pre-order traversal
public void preOrderTraversal() {
System.out.print("Pre-order Traversal: ");
preOrderTraversal(root);
System.out.println();
}
private void preOrderTraversal(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
}
// Function for post-order traversal
public void postOrderTraversal() {
System.out.print("Post-order Traversal: ");
postOrderTraversal(root);
System.out.println();
}
private void postOrderTraversal(Node root) {
if (root != null) {
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.print(root.data + " ");
}
}
// Function to check if the binary tree is complete
public boolean isCompleteBinaryTree() {
if (root == null) return true;
// Use a queue for level-order traversal
Queue<Node> queue = new LinkedList<>();
queue.add(root);
boolean flag = false;
while (!queue.isEmpty()) {
Node current = queue.poll();
if (current.left != null) {
if (flag) return false;
queue.add(current.left);
} else {
flag = true;
}
if (current.right != null) {
if (flag) return false;
queue.add(current.right);
} else {
flag = true;
}
}
return true;
}
public static void main(String[] args) {
CompleteBinaryTree tree = new CompleteBinaryTree();
// Insert nodes into the complete binary tree
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.insert(4);
tree.insert(5);
tree.insert(6);
// Print traversals
tree.inOrderTraversal();
tree.preOrderTraversal();
tree.postOrderTraversal();
// Check if the tree is complete
if (tree.isCompleteBinaryTree()) {
System.out.println("The tree is a complete binary tree.");
} else {
System.out.println("The tree is not a complete binary tree.");
}
}
}