-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeStructure.java
More file actions
173 lines (128 loc) · 4.46 KB
/
Copy pathTreeStructure.java
File metadata and controls
173 lines (128 loc) · 4.46 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class TreeStructure {
public static void inOrder(Node root) {
if (root == null)
return;
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
static void preOrder(Node root) {
if (root == null)
return;
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
static void postOrder(Node root) {
if (root == null)
return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
static int sum(Node root) {
if (root == null)
return 0;
return root.data + sum(root.left) + sum(root.right);
}
static int countNodes(Node root) {
if (root == null)
return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
static int height(Node root) {
if (root == null)
return 0;
return 1 + Math.max(height(root.left), height(root.right));
}
static boolean search(Node root, int target) {
if (root == null)
return false;
if (root.data == target)
return true;
return search(root.left, target) || search(root.right, target);
}
static int countLeaves(Node root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 1;
return countLeaves(root.left) + countLeaves(root.right);
}
static int internalNode(Node root) {
if (root == null)
return 0;
if (root.left == null && root.right == null)
return 0;
return 1 + internalNode(root.left) + internalNode(root.right);
}
static int maxdata(Node root) {
if (root == null) {
return Integer.MIN_VALUE;
}
int leftMax = maxdata(root.left);
int rightMax = maxdata(root.right);
return Math.max(root.data, Math.max(leftMax, rightMax));
}
static int minData(Node root) {
if (root == null)
return Integer.MAX_VALUE;
int leftMin = minData(root.left);
int rightMin = minData(root.right);
return Math.min(root.data, Math.min(leftMin, rightMin));
}
static boolean isLeaf(Node root) {
if (root == null)
return false;
if (root.left == null && root.right == null)
return true;
return false;
}
public static void main(String args[]) {
Node root = new Node(5);
root.left = new Node(10);
root.left.left = new Node(15);
root.left.right = new Node(20);
root.left.left.left = new Node(43);
root.left.left.right = new Node(71);
root.right = new Node(30);
root.right.left = new Node(22);
root.right.right = new Node(32);
root.right.right.left = new Node(63);
root.right.right.right = new Node(21);
System.out.print("InOrder : ");
inOrder(root);
System.out.println();
System.out.print("PreOrder : ");
preOrder(root);
System.out.println();
System.out.print("PostOrder : ");
postOrder(root);
System.out.println();
System.out.println("Sum : " + sum(root));
System.out.println("No of Nodes : " + countNodes(root));
System.out.println("Height of Tree : " + height(root));
System.out.println("Target is present : " + search(root, 45));
System.out.println("Target is present : " + search(root, 30));
System.out.println("Target is present : " + search(root, 65));
System.out.println("Target is present : " + search(root, 43));
System.out.println("NO of leaves : " + countLeaves(root));
System.out.println("NO. of Internal Node : " + internalNode(root));
System.out.println("Max data is : " + maxdata(root));
System.out.println("Minimum element is the tree is : " + minData(root));
System.out.println("Solution of is leaf : " + isLeaf(root));
System.out.println("Solution of is leaf : " + isLeaf(root.left.left));
System.out.println("Solution of is leaf : " + isLeaf(root.left.left.left));
System.out.println("Solution of is leaf : " + isLeaf(root.left.right.right));
}
}