/*
Tree :
50
30 70
20 40 60 80
35 45
Boundary Traversal : 50 30 20 35 45 60 80 70
Logic:
1. Print Root Node.
2. Print Leftmost Branch except Leaf Node.
3. Print all leaf nodes.
4. Print Rightmost branch except Leaf Node.
*/
public void boundaryTraversal()
{
printRootNode();
printLeftBranch(root.getLeftChild());
printLeafNodes(root);
printRightBranch(root.getRightChild());
}
public void printRootNode()
{
System.out.println(root.getData());
}
public void printLeftBranch(BSTNode n)
{
if(n.getLeftChild() != null)
{
System.out.println(n.getData());
printLeftBranch(n.getLeftChild());
}
}
public void printLeafNodes(BSTNode n)
{
if(n.getRightChild() ==null && n.getLeftChild() == null)
{
System.out.println(n.getData());
return;
}
printLeafNodes(n.getLeftChild());
printLeafNodes(n.getRightChild());
}
public void printRightBranch(BSTNode n)
{
if(n.getRightChild() != null)
{
System.out.println(n.getData());
printRightBranch(n.getRightChild());
}
}
No comments:
Post a Comment