-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST_withoutgetset
More file actions
74 lines (70 loc) · 1.49 KB
/
Copy pathBST_withoutgetset
File metadata and controls
74 lines (70 loc) · 1.49 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
package Datastr;
import java.util.Scanner;
class dem{
int data;
dem left,right;
public dem(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class TreeEndsem1{
dem root = null;
public dem insert(dem root , int data) {
if(root == null) {
return new dem(data);
}
if(data < root.data) {
root.left = insert(root.left,data);
}
else {
root.right = insert(root.right , data);
}
return root;
}
public static void inorder(dem root) {
if(root!=null) {
inorder(root.left);
System.out.println(root.data);
inorder(root.right);
}
}
public static void preorder(dem root) {
if(root!=null) {
System.out.println(root.data);
preorder(root.left);
preorder(root.right);
}
}
public static void postorder(dem root) {
if(root!=null) {
postorder(root.left);
postorder(root.right);
System.out.println(root.data);
}
}
public static int max(dem root) {
while(root.right != null) {
root = root.right;
}
return root.data;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeEndsem1 tree = new TreeEndsem1();
dem root = null;
for(int i=0;i<5;i++) {
System.out.println("Enter value :");
int tem_val = sc.nextInt();
root = tree.insert(root, tem_val);
}
/*System.out.println("Inorder :");
inorder(root);
System.out.println("Preorder :");
preorder(root);
System.out.println("Postrder :");
postorder(root);*/
System.out.println("Max element in the tree is :"+max(root));
}
}