Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 890c7dd

Browse files
Add all tree operations .
1 parent 880a358 commit 890c7dd

File tree

2 files changed

+164
-10
lines changed

2 files changed

+164
-10
lines changed

‎Tree/FirstExample.java

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ public static void main(String[] args) {
1111

1212
// tree.inorder();
1313
// tree.preorder();
14-
tree.postorder();
14+
// tree.postorder();
15+
16+
// int count = tree.countNode(BinaryTree.root);
17+
// System.out.println(count);
18+
19+
int sumOfNodes = tree.sumOfNodes(BinaryTree.root);
20+
System.out.print("Sum of the nodes: " + sumOfNodes);
21+
22+
System.out.println();
23+
24+
int heightOfTheTree = tree.heightOfTheTree(BinaryTree.root);
25+
System.out.println("Height of the Tree is : " + heightOfTheTree);
26+
27+
System.out.println();
28+
29+
int diameter = tree.CalculateDiameter(BinaryTree.root);
30+
System.out.println("Diameter of the tree is : " + diameter);
31+
1532
System.out.println();
1633

1734
}
@@ -27,8 +44,9 @@ public Node(int data) {
2744
}
2845
}
2946

30-
public static class BinaryTree {
47+
public static class BinaryTree {
3148
static Node root;
49+
static int AnsOfDiamiter = 0;
3250

3351
public static void insertData(int data) {
3452
root = insertRec(root, data);
@@ -61,32 +79,90 @@ public static void inorderRec(Node root) {
6179

6280
}
6381

64-
//pre order
65-
public static void preorder(){
82+
//pre order
83+
public static void preorder(){
6684
preorderRec(root);
6785
}
68-
public static void preorderRec(Node root){
69-
if(root == null){
86+
87+
public static void preorderRec(Node root) {
88+
if (root == null) {
7089
return;
7190
}
7291
System.out.print(root.data + " ");
7392
preorderRec(root.left);
7493
preorderRec(root.right);
7594
}
7695

77-
//post order
78-
public static void postorder(){
96+
//post order
97+
public static void postorder(){
7998
postorderRec(root);
8099
}
81100

82-
public static void postorderRec(Node root){
83-
if(root == null){
101+
public static void postorderRec(Node root){
102+
if(root == null){
84103
return;
85104
}
86105
postorderRec(root.left);
87106
postorderRec(root.right);
88107
System.out.println(root.data + " ");
89108

90109
}
110+
111+
// count the nodes
112+
public static int countNode(Node root) {
113+
if (root == null) {
114+
return 0;
115+
}
116+
117+
int left = countNode(root.left);
118+
int right = countNode(root.right);
119+
120+
return 1 + left + right;
121+
}
122+
123+
// sun of nodes
124+
125+
public static int sumOfNodes(Node root) {
126+
if (root == null) {
127+
return 0;
128+
}
129+
int left = sumOfNodes(root.left);
130+
int right = sumOfNodes(root.right);
131+
132+
return left + right + root.data;
133+
}
134+
135+
// height of the tree
136+
137+
public static int heightOfTheTree(Node root) {
138+
if (root == null) {
139+
return 0;
140+
}
141+
int left = heightOfTheTree(root.left);
142+
int right = heightOfTheTree(root.right);
143+
144+
return Math.max(left, right) + 1;
145+
}
146+
147+
// diameter of a binary tree
148+
public static int CalculateDiameter(Node root){
149+
diamiter(root);
150+
return AnsOfDiamiter;
151+
}
152+
public static int diamiter(Node root){
153+
154+
if(root == null){
155+
return 0;
156+
}
157+
158+
int diam1 = diamiter(root.left);
159+
int diam2 = diamiter(root.right);
160+
161+
int finaldiam = diam1 + diam2 + 1;
162+
AnsOfDiamiter = Math.max(AnsOfDiamiter, finaldiam);
163+
return 1 + Math.max(diam1, diam2);
164+
165+
}
166+
91167
}
92168
}

‎Tree/LevelOrderTravarsal.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class LevelOrderTravarsal {
5+
6+
public static void main(String[] args) {
7+
BinaryTree tree = new BinaryTree();
8+
tree.insertData(8);
9+
tree.insertData(7);
10+
tree.insertData(12);
11+
tree.insertData(15);
12+
tree.insertData(2);
13+
tree.insertData(5);
14+
15+
// tree.levelorder(BinaryTree.root);
16+
tree.levelordertravarsal();
17+
}
18+
19+
// Node class
20+
static class Node {
21+
int data;
22+
Node left;
23+
Node right;
24+
25+
public Node(int data) {
26+
this.data = data;
27+
}
28+
}
29+
30+
// BinaryTree class
31+
public static class BinaryTree {
32+
static Node root;
33+
34+
public static void insertData(int data) {
35+
root = insertRec(root, data);
36+
}
37+
38+
private static Node insertRec(Node root, int data) {
39+
if (root == null) {
40+
return new Node(data);
41+
}
42+
if (data < root.data) {
43+
root.left = insertRec(root.left, data);
44+
} else if (data > root.data) {
45+
root.right = insertRec(root.right, data);
46+
}
47+
return root;
48+
}
49+
50+
public void levelordertravarsal() {
51+
levelorder(root);
52+
}
53+
54+
public static void levelorder(Node root) {
55+
if (root == null) return;
56+
57+
Queue<Node> q = new LinkedList<>();
58+
q.add(root);
59+
q.add(null);
60+
61+
while (!q.isEmpty()) {
62+
Node currNode = q.remove();
63+
64+
if (currNode == null) {
65+
System.out.println();
66+
if (q.isEmpty()) return;
67+
q.add(null);
68+
} else {
69+
70+
System.out.print(currNode.data + " ");
71+
72+
if (currNode.left != null) q.add(currNode.left);
73+
if (currNode.right != null) q.add(currNode.right);
74+
}
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /