-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added binary trees traversals & complexity in java
- Loading branch information
1 parent
f4068ca
commit 2eeb370
Showing
11 changed files
with
521 additions
and
97 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Java/Data-Structures/TREES/BINARY-TREE/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
Java/Data-Structures/TREES/BINARY-TREE/IterativeInorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
Java/Data-Structures/TREES/BINARY-TREE/IterativePostorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
Java/Data-Structures/TREES/BINARY-TREE/IterativePreorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+" "); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.