Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit e89aa36

Browse files
committed
Merge pull request #149 from Widea/Widea-1
Widea - Added solution to treelevelorderprint
2 parents d3144db + 3575aa1 commit e89aa36

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

solutions/java/RemoveDuplicatesFromString.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
// Program to remove duplicates from a given string
2+
// 1. getUniqueString(String) method uses a boolean array.
3+
// 2. removeDuplicates(String) method uses a hash map.
4+
// Both the methods have O(n) space and time complexity. (n being the string length)
5+
6+
import java.util.HashMap;
7+
18
public class RemoveDuplicatesFromString {
29
public static void main(String[] args) {
310
RemoveDuplicatesFromString rsd = new RemoveDuplicatesFromString();
411
String input = "Tree Traversal";
5-
System.out.println(rsd.getUniqueString(input));
12+
System.out.println("Method 1: " +rsd.getUniqueString(input));
13+
System.out.println("Method 2: " +rsd.removeDuplicates(input));
614
}
715

816
public String getUniqueString(String input) {
@@ -17,4 +25,17 @@ public String getUniqueString(String input) {
1725
}
1826
return sb.toString();
1927
}
20-
}
28+
29+
public String removeDuplicates(String input) {
30+
HashMap<Character, Integer> map = new HashMap<>();
31+
StringBuffer sb = new StringBuffer("");
32+
for (int i = 0; i < input.length(); i++) {
33+
char c = input.charAt(i);
34+
if (!map.containsKey(c)) {
35+
sb.append(c);
36+
map.put(c, 1);
37+
}
38+
}
39+
return sb.toString();
40+
}
41+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Program to create a Binary Search Tree and implement level order traversal.
2+
// Author: Viveka Aggarwal
3+
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
7+
public class TreeLevelOrderPrint {
8+
node root;
9+
class node {
10+
Integer value;
11+
node left;
12+
node right;
13+
14+
node() {
15+
}
16+
17+
node (Integer value) {
18+
this.value = value;
19+
}
20+
}
21+
22+
public TreeLevelOrderPrint() {
23+
root = new node();
24+
}
25+
26+
public treelevelorderprint(Integer value) {
27+
root = new node(value);
28+
}
29+
30+
public void addToTree(Integer value) {
31+
if (root.value == null) {
32+
root = new node(value);
33+
} else {
34+
addToTree(value, root);
35+
}
36+
}
37+
38+
public void addToTree(Integer value, node curr) {
39+
if (value <= curr.value) {
40+
if (curr.left == null)
41+
curr.left = new node(value);
42+
else
43+
addToTree(value, curr.left);
44+
} else {
45+
if (curr.right == null)
46+
curr.right = new node(value);
47+
else
48+
addToTree(value, curr.right);
49+
}
50+
}
51+
52+
public void levelOrder() {
53+
if (root == null || root.value == null) {
54+
System.out.println();
55+
System.out.println("Empty tree!!!");
56+
return;
57+
}
58+
59+
Queue<node> q = new LinkedList<node>();
60+
q.add(root);
61+
62+
while(!q.isEmpty()) {
63+
node curr = q.poll();
64+
System.out.print(curr.value + " ");
65+
66+
if(curr.left != null)
67+
q.add(curr.left);
68+
69+
if(curr.right != null)
70+
q.add(curr.right);
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
TreeLevelOrderPrint tree = new TreeLevelOrderPrint();
76+
for (int i = 0; i <= 10; i++) {
77+
tree.addToTree(i);
78+
}
79+
tree.addToTree(5);
80+
tree.levelOrder();
81+
}
82+
}

0 commit comments

Comments
 (0)