Skip to content

Commit 1992361

Browse files
authored
Merge branch 'master' into new-branch
2 parents f64ed91 + 9e3b08a commit 1992361

File tree

77 files changed

+2909
-571
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+2909
-571
lines changed

.editorconfig

Lines changed: 0 additions & 13 deletions
This file was deleted.

.github/code-of-conduct.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Attribution
36+
37+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
38+
39+
[homepage]: http://contributor-covenant.org
40+
[version]: http://contributor-covenant.org/version/1/4/

.github/contributing.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Contributing
2+
3+
> Please note that this project is released with a [Contributor Code of Conduct](code-of-conduct.md). By participating in this project you agree to abide by its terms.

.github/issue-template.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am creating an issue because...

.github/pull-request-template.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
I am creating a pull request for...
2+
3+
- [ ] New algorithm
4+
- [ ] Update to an algorithm
5+
- [ ] Fix an error
6+
- [ ] Other - *Describe below*

.gitignore

Lines changed: 0 additions & 7 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Binary Tree Traversals implementation in Java
3+
*
4+
* @author: {Sudheer Reddy T}
5+
* @github: {tadipatrisudheerreddy}
6+
*/
7+
8+
9+
public class BinaryTree {
10+
11+
public static TreeNode mirrorTree(TreeNode node){
12+
if(node == null)
13+
return node;
14+
15+
TreeNode left = mirrorTree(node.left);
16+
TreeNode right = mirrorTree(node.right);
17+
node.left = right;
18+
node.right = left;
19+
20+
return node;
21+
}
22+
23+
public static void main(String[] args){
24+
25+
TreeNode root = new TreeNode(8);
26+
TreeNode.insert(root, new TreeNode(31));
27+
TreeNode.insert(root, new TreeNode(45));
28+
TreeNode.insert(root, new TreeNode(16));
29+
TreeNode.insert(root, new TreeNode(24));
30+
TreeNode.insert(root, new TreeNode(19));
31+
TreeNode.insert(root, new TreeNode(29));
32+
TreeNode.insert(root, new TreeNode(7));
33+
34+
StringBuffer inorderbuf=new StringBuffer();
35+
StringBuffer preorderbuf=new StringBuffer();
36+
StringBuffer postorderbuf=new StringBuffer();
37+
38+
39+
TreeTraversal.getInorder(root,inorderbuf);
40+
TreeTraversal.getPreorder(root, preorderbuf);
41+
TreeTraversal.getPostorder(root, postorderbuf);
42+
43+
System.out.println("Before mirroring,In order tree is: "+inorderbuf);
44+
45+
BinaryTree.mirrorTree(root);
46+
47+
StringBuffer inorderbufAfter = new StringBuffer();
48+
TreeTraversal.getInorder(root, inorderbufAfter);
49+
System.out.println("After mirroring,In order tree is: "+inorderbufAfter);
50+
51+
System.out.println("Post order traversal:" + postorderbuf);
52+
System.out.println("Pre order traversal:"+ preorderbuf);
53+
54+
System.out.println("Nodes with no siblings:");
55+
TreeWithNoSiblings.printOnlyLeaves(root);
56+
57+
}
58+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* To find Lowest Common Ancestor for a binary tree implementation in Java
3+
*
4+
* @author: {Sudheer Reddy T}
5+
* @github: {tadipatrisudheerreddy}
6+
*/
7+
8+
public class LowestCommonAncestor {
9+
10+
TreeNode root;
11+
12+
public TreeNode LCA(int p,int q){
13+
return findLCA(root,p,q);
14+
}
15+
16+
private TreeNode findLCA(TreeNode root, int p, int q) {
17+
if(root == null)
18+
return null;
19+
if(root.value == p || root.value == q)
20+
return root;
21+
22+
TreeNode left = findLCA(root.left,p,q);
23+
TreeNode right = findLCA(root.right,p,q);
24+
25+
if(left!=null && right!=null)
26+
return root;
27+
28+
return left!=null? left : right;
29+
}
30+
31+
32+
33+
public static void main(String[] args){
34+
35+
LowestCommonAncestor lca = new LowestCommonAncestor();
36+
37+
TreeNode root = new TreeNode(1);
38+
root.left = new TreeNode(2);
39+
root.right = new TreeNode(3);
40+
root.left.left = new TreeNode(4);
41+
root.left.right = new TreeNode(5);
42+
root.right.left = new TreeNode(6);
43+
root.right.right = new TreeNode(7);
44+
45+
46+
System.out.println("Lowest Common Ancestor of (4,5): "+ lca.findLCA(root, 4, 5).value);
47+
System.out.println("Lowest Common Ancestor of (4,6): "+ lca.findLCA(root, 4, 6).value);
48+
System.out.println("Lowest Common Ancestor of (3,4): "+ lca.findLCA(root, 3, 4).value);
49+
}
50+
}

BinaryTrees-traversals/SubTree.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Binary Tree Identical implementation in Java
3+
*
4+
* @author: {Sudheer Reddy T}
5+
* @github: {tadipatrisudheerreddy}
6+
*/
7+
public class SubTree {
8+
9+
TreeNode root1, root2;
10+
11+
public boolean isIdentical(TreeNode root1,TreeNode root2){
12+
13+
//Since every null tree is also subset of main tree
14+
if(root1==null&&root2==null)
15+
return true;
16+
//Need to check the null condition only once,since we are using recursion at second time it should not be null
17+
if(root1==null&&root2==null)
18+
return false;
19+
20+
return (root1.value==root2.value)&& (isIdentical(root1.left,root2.left))
21+
&&((isIdentical(root1.right,root2.right)) );
22+
}
23+
24+
public boolean isSubTree(TreeNode p, TreeNode q){
25+
26+
//Since every null tree is also subset of main tree
27+
if(q==null)
28+
return true;
29+
30+
if(p==null)
31+
return false;
32+
33+
if(isIdentical(p,q))
34+
return true;
35+
36+
return isSubTree(p.left,q)||isSubTree(p.right,q);
37+
}
38+
39+
40+
public static void main(String[] args) {
41+
42+
SubTree tree = new SubTree();
43+
44+
TreeNode root1 = new TreeNode(26);
45+
root1.right = new TreeNode(3);
46+
root1.right.right = new TreeNode(3);
47+
root1.left = new TreeNode(10);
48+
root1.left.left = new TreeNode(4);
49+
root1.left.left.right = new TreeNode(30);
50+
root1.left.right = new TreeNode(6);
51+
52+
TreeNode root2 = new TreeNode(10);
53+
root2.right = new TreeNode(6);
54+
root2.left = new TreeNode(4);
55+
root2.left.right = new TreeNode(30);
56+
57+
if(tree.isSubTree(tree.root1,tree.root2))
58+
System.out.println("Tree2 is subtree of Tree1");
59+
else
60+
System.out.println("Tree2 is not a subtree of Tree1");
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)