-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
126 lines (125 loc) · 4.09 KB
/
BST.java
File metadata and controls
126 lines (125 loc) · 4.09 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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BST{
static class Node{
int data;
Node left,right;
Node(int data,Node left,Node right){
this.data=data;
this.left=left;
this.right=right;
}
}
public static Node root=null;
public static Queue<Node> queue=new LinkedList<Node>();
public static Stack<Node> stack=new Stack<Node>();
public static Node insertNode(int data,Node root) {
if(root==null){
return new Node(data,null,null);
}else{
if(data<=root.data){
root.left=insertNode(data, root.left);
}else{
root.right=insertNode(data, root.right);
}
return root;
}
}
public static boolean validateTree(Node root,int minValue,int maxValue) {
if(root==null){
return true;
}else{
boolean current=false,leftTree=false,rightTree=false;
if(root.data>=minValue && root.data<maxValue){
current=true;
}
leftTree=validateTree(root.left, minValue, root.data);
rightTree=validateTree(root.right, root.data+1, maxValue);
return current && leftTree && rightTree;
}
}
public static void reverseLevelOrder(Node root) {
if(root==null){
System.out.println("Tree is Empty...");
return;
}
queue.add(root);
while(!queue.isEmpty()){
Node temp=queue.remove();
stack.push(temp);
if(temp.left!=null){
queue.add(temp.left);
}
if(temp.right!=null){
queue.add(temp.right);
}
}
while (!stack.isEmpty()) {
System.out.print(stack.pop().data+" ");
}
System.out.println();
}
public static void levelOrderTraversal(Node root){
if(root==null){
System.out.println("Tree is Empty...");
return;
}
queue.add(root);
while(!queue.isEmpty()){
Node temp=queue.remove();
System.out.print(temp.data+" ");
if(temp.left!=null){
queue.add(temp.left);
}
if(temp.right!=null){
queue.add(temp.right);
}
}
System.out.println();
}
public static void SpiralLevelOrder(Node root){
if (root==null) {
System.out.println("Tree is Empty...");
return;
}
Stack<Node> stack1=new Stack<Node>();
Stack<Node> stack2=new Stack<Node>();
stack1.push(root);
while (!stack1.isEmpty() || !stack2.isEmpty()) {
while (!stack1.isEmpty()) {
Node temp=stack1.pop();
if(temp.right!=null){
stack2.push(temp.right);
}
if(temp.left!=null){
stack2.push(temp.left);
}
System.out.print(temp.data+" ");
}
while (!stack2.isEmpty()) {
Node temp=stack2.pop();
if(temp.left!=null){
stack1.push(temp.left);
}
if(temp.right!=null){
stack1.push(temp.right);
}
System.out.print(temp.data+" ");
}
}
}
public static void main(String[] args) {
root=new Node(10,null,null);
root.left=new Node(2,null,null);
root.right=new Node(15,null,null);
root.left.left=new Node(1,null,null);
root.left.right=new Node(3,null,null);
root.right.left=new Node(12,null,null);
root.right.right=new Node(16,null,null);
System.out.println(validateTree(root, Integer.MIN_VALUE,Integer.MAX_VALUE));
reverseLevelOrder(root);
levelOrderTraversal(root);
SpiralLevelOrder(root);
}
}