-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
160 lines (146 loc) · 4.94 KB
/
BinaryTree.java
File metadata and controls
160 lines (146 loc) · 4.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree {
Node root;
public static void main(String[] args) {
int a[]={1,2,3,4,0,5,6};
BinaryTree t=new BinaryTree();
t.root=createTree(a,t.root,0);
inorder(t.root);
System.out.println("");
preorder(t.root);
System.out.println("");
postorder(t.root);
System.out.println("\nLevel order traversal:");
levelorder(t.root);
System.out.println("Inorder iterative:");
inorder_iterative(t.root);
System.out.println("Pre-order iterative");
preorder_iterative(t.root);
System.out.println("Height of binary tree:"+ (findheight(t.root)-1));
System.out.println("Total no. of leaf nodes:"+getLeafCount(t.root));
System.out.println("Maximum element:"+findmax(t.root));
// System.out.println("Minimum element in binary tree:"+findmin(t.root));
// System.out.println("Total no.of non-leaf nodes:"+getNonLeafCount(t.root));
}
public static Node createTree(int arr[],Node root,int i){
if(i<arr.length){
Node temp= new Node(arr[i]);
root=temp;
root.left= createTree(arr,root.left,2*i+1); //inserting left child
root.right=createTree(arr,root.right,2*i+2); //inserting right child
}
return root;
}
private static void inorder(Node root){//left-Root-right
if(root!=null){
inorder(root.left);
if(root.data!=0)
System.out.print(root.data+" ");
inorder(root.right);
}
}
public static void preorder(Node root){//root-left-right
if(root!=null){
if(root.data!=0)
System.out.print(root.data+" ");
preorder(root.left);
preorder(root.right);
}
}
public static void postorder(Node root){//left-right-root
if(root!=null){
postorder(root.left);
postorder(root.right);
if(root.data!=0)
System.out.print(root.data+" ");
}
}
public static int findheight(Node root){ //computes the height of the tree: longest path from root to farthest leaf node
if(root==null)
return 0;
int lheight= findheight(root.left);
int rheight=findheight(root.right);
if(lheight<= rheight)
return rheight+1;
else
return lheight+1;
}
public static int getLeafCount(Node root){
if(root==null)
return 0;
if(root.left==null && root.right==null)
return 1;
return getLeafCount(root.left)+ getLeafCount(root.right);
}
public static int findmax(Node root){
if(root==null)
return 0;
int val=root.data;
int leftval=findmax(root.left);
int rightval=findmax(root.right);
if(val<leftval)
val=leftval;
if(val<rightval)
val=rightval;
return val;
}
public static void levelorder(Node root){
Queue<Node> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
Node node=q.poll();
if(node.data!=0)
System.out.println(node.data);
if(node.left!=null)
q.add(node.left);
if(node.right!=null)
q.add(node.right);
}
}
public static void inorder_iterative(Node root){
Stack<Node> stack=new Stack();
if(root==null)
return;
Node curr=root;
while(!stack.isEmpty()||curr!=null){
while(curr!=null){
stack.push(curr);
curr=curr.left;
}
Node popped_item=stack.pop();
System.out.println(popped_item.data);
curr=popped_item.right;
}
}
public static void preorder_iterative(Node root){
Stack<Node> stack=new Stack<>();
if(root==null)
return;
Node curr=root;
while(!stack.isEmpty()||curr!=null){
while(curr!=null){
if(curr.data!=0)
System.out.println(curr.data);
stack.push(curr);
curr=curr.left;
}
Node popped_item=stack.pop();
curr=popped_item.right;
}
}
}
class Node{
Node left;
Node right;
int data;
Node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}
//write a program to compute load factor and no.of collisions required
//in long random sequence using linear probing,quadratic probing and random probing.
//Plot the graph between load factor and no. of collisions.