package in.blogspot.arunj2ee.ds.tree.traversal;
import in.blogspot.arunj2ee.ds.tree.BinaryTreeNode;
import in.blogspot.arunj2ee.ds.tree.util.TreeUtil;
/**
* Traversing a tree using Preorder traveral recursively
* Preorder ==> Node, Left,Right
* Time Complexity: O(n), Space Complexity: O(n)
* @author Arun.Singh
*
*/
public class PreorderTraversalRecursion {
public static void preOrderTraversal(BinaryTreeNode root) {
if (root != null) {
System.out.print(root.getData());
preOrderTraversal(root.getLeft());
preOrderTraversal(root.getRight());
}
}
public static void main(String[] args) {
preOrderTraversal(TreeUtil.makeSampleBinaryTree());
}
}
========================================================================
import in.blogspot.arunj2ee.ds.tree.BinaryTreeNode;
import in.blogspot.arunj2ee.ds.tree.util.TreeUtil;
/**
* Traversing a tree using Preorder traveral recursively
* Preorder ==> Node, Left,Right
* Time Complexity: O(n), Space Complexity: O(n)
* @author Arun.Singh
*
*/
public class PreorderTraversalRecursion {
public static void preOrderTraversal(BinaryTreeNode root) {
if (root != null) {
System.out.print(root.getData());
preOrderTraversal(root.getLeft());
preOrderTraversal(root.getRight());
}
}
public static void main(String[] args) {
preOrderTraversal(TreeUtil.makeSampleBinaryTree());
}
}
========================================================================
Refer Core Classes: http://arunj2ee.blogspot.in/2017/05/tree-core-and-utility-classes.html========================================================================
0 comments:
Post a Comment