Last active
December 10, 2019 07:25
-
-
Save t9toqwerty/89159fc75e1cea4a01442f63ec8484ee to your computer and use it in GitHub Desktop.
Binary Tree Traversal Using Stack & Recursion
This file contains hidden or 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
package com.company; | |
import java.util.Stack; | |
/** | |
* BinaryTree | |
*/ | |
public class BinaryTree { | |
BinaryTreeNode root; | |
BinaryTree() { | |
this.root = null; | |
} | |
private void inOrderTraversal(BinaryTreeNode node) { | |
if (node == null) { | |
return; | |
} | |
this.inOrderTraversal(node.left); | |
System.out.print(node.data + " "); | |
this.inOrderTraversal(node.right); | |
} | |
private void inOrderTraversalUsingStack(BinaryTreeNode node) { | |
Stack<BinaryTreeNode> s = new Stack<>(); | |
while ((node != null) || (!s.isEmpty())) { | |
if (node != null) { | |
s.push(node); | |
node = node.left; | |
} else { | |
node = s.pop(); | |
System.out.print(node.data + " "); | |
node = node.right; | |
} | |
} | |
} | |
private void preOrderTraversal(BinaryTreeNode node) { | |
if (node == null) { | |
return; | |
} | |
System.out.print(node.data + " "); | |
this.preOrderTraversal(node.left); | |
this.preOrderTraversal(node.right); | |
} | |
private void preOrderTraversalUsingStack(BinaryTreeNode node) { | |
Stack<BinaryTreeNode> s = new Stack<>(); | |
while ((node != null) || (!s.isEmpty())) { | |
if (node != null) { | |
s.push(node); | |
System.out.print(node.data + " "); | |
node = node.left; | |
} else { | |
node = s.pop(); | |
node = node.right; | |
} | |
} | |
} | |
private void postOrderTraversal(BinaryTreeNode node) { | |
if (node == null) { | |
return; | |
} | |
this.postOrderTraversal(node.left); | |
this.postOrderTraversal(node.right); | |
System.out.print(node.data + " "); | |
} | |
/** | |
* TODO: Fix This Method | |
* | |
* @param node | |
*/ | |
private void postOrderTraversalUsingStack(BinaryTreeNode node) { | |
} | |
private int countTotalNode(BinaryTreeNode node) { | |
if (node != null) { | |
return 1 + countTotalNode(node.left) + countTotalNode(node.right); | |
} else { | |
return 0; | |
} | |
} | |
private int countNonLeafNode(BinaryTreeNode node) { | |
if ((node.left != null) && (node.right != null)) { | |
return 1 + countNonLeafNode(node.left) + countNonLeafNode(node.right); | |
} else { | |
if (node.left != null) { | |
return countNonLeafNode(node.left); | |
} else if (node.right != null) { | |
return countNonLeafNode(node.right); | |
} | |
} | |
return 0; | |
} | |
/** | |
* @param node | |
* @return | |
*/ | |
private int countLeafNode(BinaryTreeNode node) { | |
if (node == null) { | |
return 0; | |
} | |
if (node.left == null && node.right == null) { | |
return this.countLeafNode(node.left) + this.countLeafNode(node.right) + 1; | |
} else { | |
return this.countLeafNode(node.left) + this.countLeafNode(node.right); | |
} | |
} | |
/** | |
* @param node | |
* @return | |
*/ | |
private int getHeight(BinaryTreeNode node) { | |
if (node == null) { | |
return 0; | |
} | |
if (getHeight(node.left) > getHeight(node.right)) { | |
return getHeight(node.left) + 1; | |
} else { | |
return getHeight(node.right) + 1; | |
} | |
} | |
public void inOrderTraversal() { | |
BinaryTreeNode temp = root; | |
this.inOrderTraversal(temp); | |
} | |
public void inOrderTraversalUsingStack() { | |
BinaryTreeNode temp = root; | |
this.inOrderTraversalUsingStack(temp); | |
} | |
public void preOrderTraversalUsingStack() { | |
BinaryTreeNode temp = root; | |
this.preOrderTraversalUsingStack(temp); | |
} | |
public void postOrderTraversalUsingStack() { | |
BinaryTreeNode temp = root; | |
this.postOrderTraversalUsingStack(temp); | |
} | |
public void preOrderTraversal() { | |
BinaryTreeNode temp = root; | |
this.preOrderTraversal(temp); | |
} | |
public void postOrderTraversal() { | |
BinaryTreeNode temp = root; | |
this.postOrderTraversal(temp); | |
} | |
public int countTotalNode() { | |
BinaryTreeNode temp = root; | |
return this.countTotalNode(temp); | |
} | |
public int countNonLeafNode() { | |
BinaryTreeNode temp = root; | |
return this.countNonLeafNode(temp); | |
} | |
public int countLeafNode() { | |
BinaryTreeNode temp = root; | |
return this.countLeafNode(temp); | |
} | |
public int getHeight() { | |
BinaryTreeNode temp = root; | |
return this.getHeight(temp); | |
} | |
} |
This file contains hidden or 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
package com.company; | |
/** | |
* BinaryTreeNode | |
*/ | |
public class BinaryTreeNode { | |
int data; | |
BinaryTreeNode left; | |
BinaryTreeNode right; | |
BinaryTreeNode(int data) { | |
this.data = data; | |
this.left = null; | |
this.right = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment