Skip to content

Commit

Permalink
added binary trees traversals & complexity in java
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Dec 21, 2018
1 parent f4068ca commit 2eeb370
Show file tree
Hide file tree
Showing 11 changed files with 521 additions and 97 deletions.
37 changes: 37 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
public class BinaryTreeNode
{
public int data;
public BinaryTreeNode left;
public BinaryTreeNode right;

public BinaryTreeNode(int data)
{
this.data=data;
left=null;
right=null;
}
public int getData()
{
return data;
}
public void setData(int data)
{
this.data=data;
}
public BinaryTreeNode getLeft()
{
return left;
}
public void setLeft(BinaryTreeNode left)
{
this.left = left;
}
public BinaryTreeNode getRight()
{
return right;
}
public void setRight(BinaryTreeNode right)
{
this.right=right;
}
}
34 changes: 34 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/Inorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Inorder
{
public static void main(String[] args)
{
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

inorder(ROOT);
}
public static void inorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
inorder(ROOT.left);
System.out.print(ROOT.data+" ");
inorder(ROOT.right);
}
}
54 changes: 54 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/IterativeInorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.*;
public class IterativeInorder
{
public static void main(String[] args) {
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

inorder(ROOT);
}

public static void inorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
Stack<BinaryTreeNode> S = new Stack<BinaryTreeNode>();
boolean done=false;
BinaryTreeNode current=ROOT;
while(!done)
{
if(current!=null)
{
S.push(current);
current=current.left;
}
else //if null value found
{
if(S.isEmpty())
done=true;
else
{
current = S.pop();
System.out.print(current.data+" ");
current=current.right;
}
}
}
}
}
63 changes: 63 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/IterativePostorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.*;
public class IterativePostorder
{
public static void main(String[] args) {
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

postorder(ROOT);
}
public static void postorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
Stack<BinaryTreeNode> S = new Stack<BinaryTreeNode>();
S.push(ROOT);
BinaryTreeNode prev=null;
while(!S.isEmpty())
{
BinaryTreeNode current = S.peek();
if(prev==null || prev.left==current || prev.right==current)
{
//traverse left then right
if(current.left!=null)
{
S.push(current.left);
}
else if(current.right!=null)
{
S.push(current.right);
}
}
else if(current.left==prev)
{
if(current.right!=null)
{
S.push(current.right);
}
}
else
{
System.out.print(current.data+" ");
S.pop();
}
prev=current;
}
}
}
48 changes: 48 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/IterativePreorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.*;
public class IterativePreorder
{
public static void main(String[] args)
{
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

preorder(ROOT);
}
public static void preorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
Stack<BinaryTreeNode> s = new Stack<BinaryTreeNode>();
s.push(ROOT);
while(!s.empty())
{
BinaryTreeNode tmp= s.pop();
System.out.print(tmp.data+" ");

if(tmp.right!=null)
{
s.push(tmp.right);
}
if(tmp.left!=null)
{
s.push(tmp.left);
}
}
}
}
48 changes: 48 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/LevelOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.*;
public class LevelOrder
{
public static void main(String[] args) {
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

levelorder(ROOT);
}
public static void levelorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
Queue<BinaryTreeNode> Q = new LinkedList<BinaryTreeNode>();
BinaryTreeNode current=ROOT;

while(current!=null)
{
System.out.print(current.data +" ");
if(current.left!=null)
{
Q.add(current.left);
}
if(current.right!=null)
{
Q.add(current.right);
}

current=Q.poll();
}
}
}
33 changes: 33 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/Postorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Postorder
{
public static void main(String[] args) {
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

postorder(ROOT);
}
public static void postorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
postorder(ROOT.left);
postorder(ROOT.right);
System.out.print(ROOT.data+" ");
}
}
34 changes: 34 additions & 0 deletions Java/Data-Structures/TREES/BINARY-TREE/Preorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Preorder
{
public static void main(String[] args)
{
BinaryTreeNode ROOT = null;
BinaryTreeNode a = new BinaryTreeNode(1);
BinaryTreeNode b = new BinaryTreeNode(2);
BinaryTreeNode c = new BinaryTreeNode(3);
BinaryTreeNode d = new BinaryTreeNode(4);
BinaryTreeNode e = new BinaryTreeNode(5);
BinaryTreeNode f = new BinaryTreeNode(6);
BinaryTreeNode g = new BinaryTreeNode(7);

ROOT=a;
ROOT.setLeft(b);
ROOT.setRight(c);
b.setLeft(d);
b.setRight(e);
c.setLeft(f);
c.setRight(g);

preorder(ROOT);
}
public static void preorder(BinaryTreeNode ROOT)
{
if(ROOT==null)
{
return;
}
System.out.print(ROOT.getData()+" "); //root
preorder(ROOT.getLeft()); //left
preorder(ROOT.getRight()); //right
}
}
18 changes: 18 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@

#### TREES

##### BINARY TREES

* [Binary Tree Node Example](Data-Structures/TREES/BINARY-TREE/BinaryTreeNode.java)
* Traversals
* Depth First Search
* IN ORDER
* [ITERATIVE](Data-Structures/TREES/BINARY-TREE/IterativeInorder.java)
* [RECURSIVE](Data-Structures/TREES/BINARY-TREE/Inorder.java)
* PRE ORDER
* [ITERATIVE](Data-Structures/TREES/BINARY-TREE/IterativePreorder.java)
* [RECURSIVE](Data-Structures/TREES/BINARY-TREE/Preorder.java)
* POST ORDER
* [ITERATIVE](Data-Structures/TREES/BINARY-TREE/IterativePostorder.java)
* [RECURSIVE](Data-Structures/TREES/BINARY-TREE/Postorder.java)
* Breadth First Search
* LEVEL ORDER
* [ITERATIVE](Data-Structures/TREES/BINARY-TREE/LevelOrder.java)

### :rocket: DYNAMIC PROGRAMMING

### :rocket: MISC
Expand Down
Loading

0 comments on commit 2eeb370

Please sign in to comment.