-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullBinaryTree.java
More file actions
135 lines (109 loc) · 3.89 KB
/
FullBinaryTree.java
File metadata and controls
135 lines (109 loc) · 3.89 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
package datastructures;
public class FullBinaryTree {
// 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 FullBinaryTree() {
root = null;
}
// Function to create a full binary tree with given elements
public void createFullBinaryTree(int[] elements) {
if (elements.length == 0) return;
root = createFullBinaryTree(elements, 0);
}
private Node createFullBinaryTree(int[] elements, int index) {
if (index >= elements.length) return null;
Node node = new Node(elements[index]);
// Full binary tree property: Each node has 0 or 2 children
// Calculate left and right child indices
int leftIndex = 2 * index + 1;
int rightIndex = 2 * index + 2;
// Recursively create left and right subtrees
if (leftIndex < elements.length && rightIndex < elements.length) {
node.left = createFullBinaryTree(elements, leftIndex);
node.right = createFullBinaryTree(elements, rightIndex);
}
return node;
}
// 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 full
public boolean isFullBinaryTree() {
return isFullBinaryTree(root);
}
private boolean isFullBinaryTree(Node node) {
if (node == null) return true; // An empty tree is considered full
// If leaf node, return true
if (node.left == null && node.right == null) return true;
// If both children are present
if (node.left != null && node.right != null) {
return isFullBinaryTree(node.left) && isFullBinaryTree(node.right);
}
// If one child is missing
return false;
}
public static void main(String[] args) {
FullBinaryTree tree = new FullBinaryTree();
// Example array to create a full binary tree
int[] elements = {1, 2, 3, 4, 5, 6, 7};
tree.createFullBinaryTree(elements);
// Print traversals
tree.inOrderTraversal();
tree.preOrderTraversal();
tree.postOrderTraversal();
// Check if the tree is full
if (tree.isFullBinaryTree()) {
System.out.println("The tree is a full binary tree.");
} else {
System.out.println("The tree is not a full binary tree.");
}
}
}