|
1 | 1 | package com.company;
|
2 | 2 |
|
3 |
| -import java.util.LinkedList; |
4 |
| -import java.util.Queue; |
5 |
| -import java.util.Stack; |
6 |
| - |
7 | 3 | class BST {
|
8 | 4 | class Node{
|
9 | 5 | int data;
|
@@ -70,131 +66,4 @@ private void postorderRec(Node root){
|
70 | 66 | }
|
71 | 67 | }
|
72 | 68 |
|
73 |
| - public int sum(){ |
74 |
| - return sumRec(root); |
75 |
| - } |
76 |
| - private int sumRec(Node root){ |
77 |
| - if(root==null) |
78 |
| - return 0; |
79 |
| - return root.data+sumRec(root.left)+sumRec(root.right); |
80 |
| - } |
81 |
| - |
82 |
| - public int getDiffEvenOddNodes(){ |
83 |
| - return getDiffEvenOddNodesRec(root); |
84 |
| - } |
85 |
| - private int getDiffEvenOddNodesRec(Node root){ |
86 |
| - if(root==null) |
87 |
| - return 0; |
88 |
| - return root.data-getDiffEvenOddNodesRec(root.left)-getDiffEvenOddNodesRec(root.right); |
89 |
| - } |
90 |
| - |
91 |
| - public int noOfNodes(){ |
92 |
| - return noofnodes(root); |
93 |
| - } |
94 |
| - private int noofnodes(Node root){ |
95 |
| - if(root==null) |
96 |
| - return 0; |
97 |
| - return 1+noofnodes(root.left)+noofnodes(root.right); |
98 |
| - } |
99 |
| - |
100 |
| - public int noOfLeadNodes(){ |
101 |
| - return noofleafnodes(root); |
102 |
| - } |
103 |
| - private int noofleafnodes(Node root){ |
104 |
| - if(root==null) |
105 |
| - return 0; |
106 |
| - if(root.left==null&&root.right==null) |
107 |
| - return 1; |
108 |
| - return noofleafnodes(root.left)+noofleafnodes(root.right); |
109 |
| - } |
110 |
| - |
111 |
| - public int height(){ |
112 |
| - return heightRec(root); |
113 |
| - } |
114 |
| - private int heightRec(Node root){ |
115 |
| - if(root==null) |
116 |
| - return -1; |
117 |
| - return max(heightRec(root.left),heightRec(root.right))+1; |
118 |
| - } |
119 |
| - private int max(int a,int b){ |
120 |
| - return (a>b)?a:b; |
121 |
| - } |
122 |
| - public void levelOrderTraversal() { |
123 |
| - levelOrderTraversalRec(root); |
124 |
| - } |
125 |
| - private void levelOrderTraversalRec(Node root){ |
126 |
| - if(root==null) |
127 |
| - return; |
128 |
| - Queue<Node> q = new LinkedList<>(); |
129 |
| - q.add(root); |
130 |
| - while(!q.isEmpty()){ |
131 |
| - Node node=q.remove(); |
132 |
| - System.out.print(node.data+" "); |
133 |
| - if(node.left!=null) |
134 |
| - q.add(node.left); |
135 |
| - if(node.right!=null) |
136 |
| - q.add(node.right); |
137 |
| - } |
138 |
| - System.out.println(); |
139 |
| - } |
140 |
| - public void printAtGivenLevel(int level){ |
141 |
| - printAtGivenLevelRec(root,level); |
142 |
| - } |
143 |
| - private void printAtGivenLevelRec(Node root,int level){ |
144 |
| - if(root==null) |
145 |
| - return; |
146 |
| - if(level==1) { |
147 |
| - System.out.print(root.data+" "); |
148 |
| - return; |
149 |
| - } |
150 |
| - printAtGivenLevelRec(root.left,level-1); |
151 |
| - printAtGivenLevelRec(root.right,level-1); |
152 |
| - } |
153 |
| - public void reverseLevelOrderTraversal() { |
154 |
| - reverseLevelOrderTraversalRec(root); |
155 |
| - } |
156 |
| - private void reverseLevelOrderTraversalRec(Node root){ |
157 |
| - if(root==null) |
158 |
| - return; |
159 |
| - Queue<Node> q = new LinkedList<>(); |
160 |
| - Stack<Integer> s=new Stack<>(); |
161 |
| - q.add(root); |
162 |
| - while(!q.isEmpty()){ |
163 |
| - Node node=q.remove(); |
164 |
| - s.push(node.data); |
165 |
| - if(node.left!=null) |
166 |
| - q.add(node.left); |
167 |
| - if(node.right!=null) |
168 |
| - q.add(node.right); |
169 |
| - } |
170 |
| - while(!s.isEmpty()){ |
171 |
| - System.out.print(s.pop()+" "); |
172 |
| - } |
173 |
| - System.out.println(); |
174 |
| - } |
175 |
| - public void levelOrderTraversallinebyline() { |
176 |
| - levelOrderTraversallinebylineRec(root); |
177 |
| - } |
178 |
| - private void levelOrderTraversallinebylineRec(Node root) { |
179 |
| - if (root == null) |
180 |
| - return; |
181 |
| - Queue<Node> q = new LinkedList<>(); |
182 |
| - q.add(root); |
183 |
| - while (true) { |
184 |
| - int count=q.size(); |
185 |
| - if(count==0) |
186 |
| - break; |
187 |
| - while (count>0) { |
188 |
| - Node node = q.remove(); |
189 |
| - System.out.print(node.data + " "); |
190 |
| - if (node.left != null) |
191 |
| - q.add(node.left); |
192 |
| - if (node.right != null) |
193 |
| - q.add(node.right); |
194 |
| - count--; |
195 |
| - } |
196 |
| - System.out.println(); |
197 |
| - } |
198 |
| - } |
199 | 69 | }
|
200 |
| - |
0 commit comments