Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43b1a52

Browse files
added binary tree programs
1 parent 100347a commit 43b1a52

File tree

9 files changed

+293
-10
lines changed

9 files changed

+293
-10
lines changed

‎Algorithms/Sorting/InsertionSort.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package Algorithms.Sorting;
22
/* TODO :: visit the link for more details => https://youtu.be/f-f50FjS_jA */
33

4+
import java.util.Arrays;
5+
46
public class InsertionSort {
5-
public voidsort(int[] array) {
7+
public int[] insertionSort(int[] array) {
68
int temp, j;
79
for (int i = 0; i < array.length; i++) {
810
temp = array[i];
@@ -13,5 +15,11 @@ public void sort(int[] array) {
1315
}
1416
array[j] = temp;
1517
}
18+
return array;
19+
}
20+
21+
public static void main(String[] args) {
22+
InsertionSort insertionSort = new InsertionSort();
23+
System.out.println(Arrays.toString(insertionSort.insertionSort(new int[]{2, 8, 1, 3, 6, 7, 5, 4})));
1624
}
1725
}

‎DSA_College/CircularLinkedList.java

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package DSA_College;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class CircularLinkedList {
6+
Node tail;
7+
private int size = 0;
8+
9+
private static class Node {
10+
int data;
11+
Node next;
12+
13+
public Node(int data) {
14+
this.data = data;
15+
}
16+
}
17+
18+
// insert last
19+
public void add(int data) {
20+
Node node = new Node(data);
21+
if (tail == null) {
22+
tail = node;
23+
node.next = node;
24+
} else {
25+
node.next = tail.next;
26+
tail.next = node;
27+
tail = node;
28+
}
29+
size++;
30+
}
31+
// insert first
32+
public void addFirst(int data) {
33+
Node node = new Node(data);
34+
if (isEmpty()) {
35+
add(data);
36+
37+
} else {
38+
node.next = tail.next;
39+
tail.next = node;
40+
}
41+
size ++;
42+
}
43+
// insert at
44+
public void add(int index, int data) {
45+
Node node = new Node(data);
46+
if (isEmpty())
47+
add(data);
48+
if (index == 0)
49+
addFirst(data);
50+
else if (index == size)
51+
add(data);
52+
else {
53+
Node n = tail.next;
54+
int i = 0;
55+
while (i < index - 1) {
56+
n = n.next;
57+
i ++;
58+
}
59+
node.next = n.next;
60+
n.next = node;
61+
}
62+
size ++;
63+
}
64+
// delete last
65+
public int delete() {
66+
if (isEmpty())
67+
throw new NoSuchElementException("Empty List!!");
68+
int data = tail.data;
69+
Node n = tail;
70+
while (n.next != tail) {
71+
n = n.next;
72+
}
73+
n.next = tail.next;
74+
tail = n;
75+
size --;
76+
return data;
77+
}
78+
// delete first
79+
public int deleteFirst() {
80+
if (isEmpty())
81+
delete();
82+
Node node = tail.next;
83+
tail.next = tail.next.next;
84+
node.next = null;
85+
size --;
86+
return node.data;
87+
}
88+
public int delete(int index) {
89+
int i = 0;
90+
Node n = tail.next;
91+
while (i < index - 1) {
92+
n = n.next;
93+
i ++;
94+
}
95+
int data = n.next.data;
96+
n.next = n.next.next;
97+
size --;
98+
return data;
99+
}
100+
101+
public boolean isEmpty() {
102+
return size == 0;
103+
}
104+
105+
public void display() {
106+
Node n = tail.next;
107+
for (int i = 0; i < size; i++) {
108+
System.out.print(n.data + " -> ");
109+
n = n.next;
110+
}
111+
System.out.println(n.data);
112+
}
113+
114+
public static void main(String[] args) {
115+
CircularLinkedList cll = new CircularLinkedList();
116+
cll.add(1);
117+
cll.add(2);
118+
cll.add(3);
119+
cll.display();
120+
cll.addFirst(0);
121+
cll.display();
122+
cll.add(1, 10);
123+
cll.display();
124+
System.out.println(cll.delete());
125+
cll.display();
126+
System.out.println(cll.deleteFirst());
127+
cll.display();
128+
System.out.println(cll.delete(2));
129+
cll.display();
130+
}
131+
}

‎DSA_College/Trees/BinaryTree.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package DSA_College.Trees;
2+
3+
import java.util.Stack;
4+
5+
class TreeNode {
6+
int data;
7+
TreeNode right;
8+
TreeNode left;
9+
}
10+
11+
public class BinaryTree {
12+
// preeOrder using iteration
13+
public void preOrder(TreeNode root) {
14+
if (root == null)
15+
return;
16+
Stack<TreeNode> stack = new Stack<>();
17+
stack.push(root);
18+
while (!stack.isEmpty()) {
19+
TreeNode t = stack.pop();
20+
System.out.print(t.data + " ");
21+
if (t.right != null) {
22+
stack.push(t.right);
23+
}
24+
if (t.left != null) {
25+
stack.push(t.left);
26+
}
27+
}
28+
}
29+
}

‎DSA_College/Trees/Trees_COncepts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Trees are the non linear data structure which organizes data in the hierarchical structure and this is a recursive definition
2+
OR
3+
A tree is any connected graph without any circuits.
4+
OR
5+
If in a graph there is one and only one path between every pair of vertices, then graph is called as a tree.
6+
7+
8+
//TODO:: Properties of a tree
9+
1 => A tree with n vertices has exactly (n - 1) edges.
10+
2 => A graph is a tree if it is minimally connected.
11+
3 => Any connect with n vertices and n - 1 edges is a tree
12+
13+
// TODO:: Tree Terminologies
14+
## Root => A root is that vertex or a node where the tree originated.
15+
## Edge => An edge is an connected link between nodes or vertices, in a tree with n vertices possibly there exists (n-1) edges.
16+
## Parent => A node which has a branch from it. Theoritically a parent can have any number of child.
17+
## Child => descendent of a parent node. except root node all nodes are child nodes.
18+
## Siblings => Nodes that belongs to same parent are called siblings
19+
## Degree => Degree of a Node == total number of children of that node. Degree of a tree == Highest degree of a Node among all Nodes
20+
## Internal Node (Non-terminal Nodes) => any Node that have at-least one child Node.
21+
## Leaf Node (External Nodes) => Node that do not have any child Node.
22+
## Level of a Node => Every step from leaf to root is called level. Default level of node is 0.
23+
## Height => Longest path from any leaf node to a particular node. Height of root node == Height of the tree.
24+
## Depth => Total number of nodes from root to the particular node is called Depth of that node.
25+
## Subtree => In a tree, each child from a node forms a sub-tree recursively.
26+
27+
// TODO:: Binary Trees
28+
=> A tree that can have maximum two child nodes.
29+
Types of Binary Trees
30+
1) Labeled Trees 2) Un-labeled Trees
31+
=> unlabeled trees => none of the node is labeled. Total number of unlabeled trees == (2*n C n) / (n + 1) ex. 6C3 / 4 (n = 3)
32+
=> labeled trees => number of labeled trees == unlabeled trees * n!.
33+
Important => multiway tree, B-tree,

‎Leetcode/Leetcode_560.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Leetcode;
2+
3+
import java.util.HashMap;
4+
5+
public class Leetcode_560 {
6+
public static int subarraySum(int[] nums, int k) {
7+
int[] res = new int[nums.length];
8+
int count = 0;
9+
for (int i = 0; i < nums.length; i++) {
10+
int maxEnd = 0, maxHere = Integer.MIN_VALUE;
11+
for (int j = i; j < nums.length; j++) {
12+
maxHere += nums[i];
13+
if (maxEnd == k) {
14+
count++;
15+
break;
16+
}
17+
if (maxHere > maxEnd) {
18+
maxEnd = maxHere;
19+
maxHere = 0;
20+
} else if (maxHere < 0) {
21+
maxHere = 0;
22+
}
23+
}
24+
}
25+
return count;
26+
}
27+
28+
private static boolean subSum(int i, int k) {
29+
HashMap<Integer, Integer> h = new HashMap<>();
30+
return false;
31+
}
32+
33+
public static void main(String[] args) {
34+
System.out.println(subarraySum(new int[]{1, 1, 1}, 2));
35+
}
36+
}

‎Leetcode/Leetcode_62.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class Leetcode_62 {
6+
public int uniquePaths(int m, int n) {
7+
int[][] dp = new int[n][m];
8+
dp[0][0] = 0;
9+
Arrays.fill(dp[0], 1);
10+
for (int i = 0;i < n;i++) {
11+
for (int j = 0;j < m;j++) {
12+
if (i != 0 || j != 0) {
13+
dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
14+
}
15+
}
16+
}
17+
return dp[n - 1][m - 1];
18+
}
19+
}

‎Leetcode/Leetcode_989.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Leetcode;
2+
3+
import java.math.BigInteger;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
8+
public class Leetcode_989 {
9+
public static List<Integer> addToArrayForm(int[] num, int k) {
10+
List<Integer> r = new ArrayList<>();
11+
BigInteger n = new BigInteger(0 + "");
12+
for (int j : num) {
13+
n = n.multiply(new BigInteger(10 + "")).add(new BigInteger(j + ""));
14+
}
15+
n = n.add(new BigInteger(k + ""));
16+
System.out.println(n);
17+
for (int i = 0; i < (n + "").length(); i++) {
18+
r.add(Integer.parseInt((n + "").charAt(i) + ""));
19+
}
20+
return r;
21+
}
22+
23+
public static void main(String[] args) {
24+
System.out.println(addToArrayForm(new int[]{1, 2, 6, 3, 0, 7, 1, 7, 1, 9, 7, 5, 6, 6, 4, 4, 0, 0, 6, 3}, 516));
25+
}
26+
}

‎MyTree/BinarySearchTree/BinarySearchTree.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,23 @@ public void insert(Node node, int value) {
2121
}
2222

2323
public Node delete(Node node, int data) {
24-
if (data > node.data)
24+
if (node == null)
25+
return null;
26+
if (node.data < data)
2527
node.right = delete(node.right, data);
26-
else if (data < node.data)
28+
else if (node.data > data)
2729
node.left = delete(node.left, data);
2830
else {
29-
if (node.right != null && node.left != null) {
30-
intmax = node.right.data;
31-
} else if (node.right != null){
31+
if (node.left == null && node.right == null)
32+
returnnull;
33+
else if (node.left == null){
3234
return node.right;
33-
} else if (node.left != null) {
35+
} else if (node.right == null) {
3436
return node.left;
35-
} else {
36-
return null;
3737
}
38+
} else {
39+
3840
}
39-
return node;
4041
}
4142

4243
public Node search(Node node, int data) {
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /