-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarytrees.java
More file actions
42 lines (32 loc) · 1.24 KB
/
Binarytrees.java
File metadata and controls
42 lines (32 loc) · 1.24 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.binarytrees;
/**
*
* @author HP
*/
public class Binarytrees {
public static <T> int height(BinaryTreeNode<T> p) {
if (p == null) {
return 0;
} else {
return 1 + Math.max(height(p.llink), height(p.rlink));
}
}
public static void main(String[] args) {
Binarytreesnode<Integer> node1 = new Binarytreesnode<>(5);
Binarytreesnode<Integer> node2 = new Binarytreesnode<>(6);
Binarytreesnode<Integer> node3 = new Binarytreesnode<>(7);
Binarytreesnode<Integer> node4 = new Binarytreesnode<>(10);
// Link the nodes to form a binary tree
node1.llink = node2; // Left child of node1 is node2
node1.rlink = node3; // Right child of node1 is node3
node2.llink = node4; // Left child of node2 is node4
System.out.println("Node 1 data: " + node1.info);
System.out.println("Node 2 data: " + node2.info);
System.out.println("Node 3 data: " + node3.info);
System.out.println("Node 4 data: " + node4.info);
System.out.println("Height of the tree: " + height(node1));
}
}