forked from mr-sergi/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked list to Binary Search Tree.java
112 lines (112 loc) · 2.81 KB
/
Linked list to Binary Search Tree.java
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
import java.util.*;
class LinkedList {
static Node head;
static class Node {
int data;
Node next,prev;
Node(int d) {
data = d;
next = prev=null;
}
}
static class bnode{
int data;
bnode left,right;
bnode(int d){
data=d;
left=right=null;
}
}
public static LinkedList insert(LinkedList list, int data) {
LinkedList.Node new_node = new LinkedList.Node(data);
new_node.next = null;
if (list.head == null) {
list.head = new_node;
} else {
LinkedList.Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
return list;
}
public static void printlist(LinkedList list) {
LinkedList.Node temp = list.head;
System.out.println("Linked list:");
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
bnode convert(){
int n=count(head);
return sortedbst(n);
}
bnode sortedbst(int n){
if (n<=0)
return null;
bnode left=sortedbst(n/2);
bnode root=new bnode(head.data);
root.left=left;
head=head.next;
root.right=sortedbst(n-n/2-1);
return root;
}
void preorder(bnode n) {
if (n == null) {
return;
}
System.out.print(n.data + " ");
preorder(n.left);
preorder(n.right);
}
void inorder(bnode n) {
if (n == null) {
return;
}
inorder(n.left);
System.out.print(n.data + " ");
inorder(n.right);
}
void postorder(bnode n) {
if (n == null) {
return;
}
postorder(n.left);
postorder(n.right);
System.out.print(n.data + " ");
}
int count(Node head) {
Node temp = head;
int c = 0;
while (temp != null) {
c++;
temp = temp.next;
}
return c;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList ll = new LinkedList();
ll = insert(ll, 2);
ll = insert(ll, 5);
ll = insert(ll, 6);
ll = insert(ll, 8);
ll = insert(ll, 1);
ll = insert(ll, 3);
ll = insert(ll,9);
printlist(ll);
bnode r=ll.convert();
System.out.println("The traversal of the tree are:");
System.out.println("Preorder Traversal:");
ll.preorder(r);
System.out.println();
System.out.println("Inorder Traversal:");
ll.inorder(r);
System.out.println();
System.out.println("Postorder Traversal:");
ll.postorder(r);
}
}