-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpract.java
More file actions
executable file
·99 lines (97 loc) · 2.52 KB
/
pract.java
File metadata and controls
executable file
·99 lines (97 loc) · 2.52 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
import java.util.LinkedList;
import java.util.Queue;
public class pract {
static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
}
}
Node root;
public void insertlevel(int data){
Node newNode = new Node(data);
if(root==null) {
root = newNode;
return;
}
Queue<Node> qu = new LinkedList<>();
qu.add(newNode);
while(!qu.isEmpty()){
Node temp = qu.poll();//retrive and return the head first ele of que
if(temp.left==null){
temp.left = newNode;
break;
}else{
qu.add(temp.left);
}
if(temp.right == null){
temp.right = newNode;
break;
}else{
qu.add(temp.right);
}
}
}
public static void inorder(Node root){
if(root==null){
return;
}
inorder(root.left);
System.out.println(root.data+" ");
inorder(root.right);
}
public static int countNode(Node root){
if(root==null || (root.left==null && root.right==null)){
return 0;
}
return 1+countNode(root.left)+countNode(root.right);
}
public static boolean search(Node root,int key){
if(root==null){
return false;
}
if(root.data>key)
return search(root.left, key);
else
return search(root.right, key);
}
public static int minu(Node root){
while (root.left!=null) {
root = root.left;
}
return root.data;
}
public static int maxx(Node root){
while(root.right!=null){
root = root.right;
}
return root.data;
}
public static int height(Node root){
if(root==null){
return 0;
}
int left = height(root.left);
int right = height(root.right);
return Math.max(left, right)+1;
}
public static boolean isfull(Node root){
if(root==null){
return true;
}
if(root.left==null && root.right==null){
return true;
}
if(root.left!=null && root.right!=null){
return isfull(root.left) && isfull(root.right);
}
return false;
}
public static void main(String[] args) {
pract tree = new pract();
tree.insertlevel(2);
tree.insertlevel(2);
}
}