Skip to content

Commit 71504ec

Browse files
committed
Changes added.
1 parent 36e5f21 commit 71504ec

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/cs/ds/lists/LinkedList.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,28 @@ public static void main(String[] args) {
8686

8787
for(int i=0; i < nodeList.getLength(); i++) {
8888
System.out.println("data in position " + i +" is : " + nodeList.getData(i));
89-
}
89+
}
90+
91+
Node head = swapNodes(nodeList.getHead());
92+
93+
while(head !=null){
94+
System.out.print(head.getData()+" ");
95+
head = head.getNextNode();
96+
}
9097
}
98+
99+
private static Node swapNodes(Node head){
100+
if(head == null || head.getNextNode() == null) {
101+
return head;
102+
}
103+
Node temp = head;
104+
Node second = head.getNextNode();
105+
Node newHead = second.getNextNode();
106+
head = second;
107+
head.setNextNode(temp);
108+
temp.setNextNode(swapNodes(newHead));
109+
return head;
110+
}
91111
}
92112

93113

src/cs/ds/trees/BinarySearchTree.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private void printRootToLeaf(Node rootNode, String previousValue){
219219
public static void main(String[] args) {
220220

221221
BinarySearchTree binaryTree = new BinarySearchTree();
222-
binaryTree.add(7);
222+
/*binaryTree.add(7);
223223
binaryTree.add(6);
224224
binaryTree.add(8);
225225
binaryTree.add(3);
@@ -235,7 +235,15 @@ public static void main(String[] args) {
235235
binaryTree.add(15);
236236
binaryTree.add(1);
237237
binaryTree.add(4);
238-
238+
*/
239+
240+
binaryTree.add(5);
241+
binaryTree.add(3);
242+
binaryTree.add(2);
243+
binaryTree.add(4);
244+
binaryTree.add(9);
245+
binaryTree.add(6);
246+
binaryTree.add(11);
239247
System.out.println("Size of this node is : "+binaryTree.size());
240248
System.out.println("Max depth is : " + binaryTree.maxDepth());
241249
System.out.println("Min Value is : " + binaryTree.minValue());
@@ -248,6 +256,27 @@ public static void main(String[] args) {
248256
System.out.print(binaryTree.valueExists(0));
249257
System.out.println();
250258
binaryTree.printRootToLeaf();
259+
260+
BinarySearchTree bst = new BinarySearchTree();
261+
createNew(binaryTree.rootNode, 4,7, bst);
262+
System.out.println("new tree data is : ");
263+
bst.printData();
251264
}
252265

266+
267+
private static BinarySearchTree createNew(Node root, int min, int max, BinarySearchTree bst){
268+
if(root!=null) {
269+
if (root.data >= min && root.data <= max) {
270+
bst.add(root.data);
271+
}
272+
if (root.data >= min) {
273+
createNew(root.getLeftNode(), min, max, bst);
274+
}
275+
276+
if (root.data < max) {
277+
createNew(root.getRightNode(), min, max, bst);
278+
}
279+
}
280+
return bst;
281+
}
253282
}

0 commit comments

Comments
 (0)