We've already seen many implementations of tree and its uses in java. Lets look at a simple implementation of traversing on a tree in C++. Code has been given below which I think is quite self descriptive.
Regards,
Jack
Regards,
Jack
package dsa.tree;
import java.util.Stack;
public class TracePath {
private Node n1;
private Stack<Node> mainStack = null;
public static void main(String args[]){
TracePath nodeFinder = new TracePath();
nodeFinder.find();
}
public void find(){
Tree t = getSampleTree();
trace(t,n1);
for(Node iNode:mainStack){
System.out.println(iNode.data);
}
}
private Tree getSampleTree() {
Tree bsTree = new BinarySearchTree();
int randomData[] = {43,887,11,3,8,33,6,0,46,32,78,76,334,45};
for(int i=0;i<randomData.length;i++){
bsTree.add(randomData[i]);
}
n1 = bsTree.search(76);
return bsTree;
}
public Stack<Node> trace(Tree t, Node node){
mainStack = new Stack<Node>();
trace(t.getRoot(),node);
return mainStack;
}
private boolean trace(Node parent, Node node){
mainStack.push(parent);
if(node.equals(parent)){
return true;
}
if(parent.left != null){
if (trace(parent.left, node))
return true;
}
if(parent.right!=null){
if (trace(parent.right, node))
return true;
}
mainStack.pop();
return false;
}
}

package dsa.tree;
import java.util.HashSet;
import java.util.Set;
public class FindCommonNode {
private Node n1,n2;
public static void main(String args[]){
FindCommonNode nodeFinder = new FindCommonNode();
nodeFinder.find();
}
public void find(){
Tree t = getSampleTree();
Node commonParent = findCommonParent(t,n1,n2);
System.out.println("Common Parent : "+commonParent.data);
}
private Tree getSampleTree() {
Tree bsTree = new BinarySearchTree();
int randomData[] = {43,887,11,3,8,33,6,0,46,32,78,76,334,45};
for(int i=0;i<randomData.length;i++){
bsTree.add(randomData[i]);
}
n1 = bsTree.search(45);
n2 = bsTree.search(334);
return bsTree;
}
public Node findCommonParent(Tree t, Node node1, Node node2){
Set<Node> firstNodeAddrSet = new HashSet<Node>();
//Traverse till root
while(node1!=null){
firstNodeAddrSet.add(node1);
node1 = node1.parent;
}
while(!firstNodeAddrSet.contains(node2) && node2!=null){
node2 = node2.parent;
}
return node2;
}
}
Order | Sequence |
In-order | 9, 12, 14, 17, 19, 23, 50, 54, 67, 72, 76 |
Pre-order | 50, 17, 9, 14, 12, 23, 19, 76, 54, 72, 67 |
Post-order | 12, 14, 9, 19, 23, 17, 67, 72, 54, 76, 50 |
public void add(int data); public boolean search(int searchData); public void printOrdered();Ofcourse, the above three methods are not the only methods present in a binary search tree, but once these methods are understood and analyzed, understanding the rest of the binary tree should not be a big problem.
package dsa.tree;
public interface Tree {
public void add(int currentData);
public Node search(int data);
}package dsa.tree;
public class Node {
Node left;
Node right;
Node parent;
int data;
}package dsa.tree;
public class BinarySearchTree implements Tree{
private Node root;
public void add(int currentData){
if(root == null){
root = new Node();
root.data = currentData;
return;
}
add(currentData, root);
}
private void add(int currentData, Node position){
if(currentData<position.data){
if(position.left==null){
position.left = new Node();
position.left.data = currentData;
position.left.parent = position;
return;
}
add(currentData, position.left);
}else{
if(position.right==null){
position.right = new Node();
position.right.data = currentData;
position.right.parent = position;
return;
}
add(currentData, position.right);
}
}
public Node search(int searchData){
if(root == null){
return null;
}
return search(searchData, root);
}
/*
* O(log n) on average case
*/
private Node search(int searchData, Node node){
if(node.data == searchData){
return node;
}
if(searchData < node.data){
return search(searchData, node.left);
}else{
return search(searchData, node.right);
}
}
public void printOrdered(){
if(root == null){
return;
}
printOrdered(root);
}
//DO A IN ORDER TRAVERSAL
//VISIT LEFT
//VISIT ROOT
//VISIT RIGHT
public void printOrdered(Node node){
if(node.left != null){
printOrdered(node.left);
}
System.out.println(node.data);
if(node.right!=null){
printOrdered(node.right);
}
}
public void printValues(){
print(root);
}
private void print(Node node){
if(node == null){
return;
}else{
print(node.left);
print(node.right);
}
}
public static void main(String args[]){
BinarySearchTree bTree = new BinarySearchTree();
for(int i=0;i<10;i++){
int t = (int)(Math.random()*20);
System.out.println(t);
bTree.add(t);
}
bTree.printValues();
for(int i=0;i<10;i++){
int t = (int)(Math.random()*20);
System.out.println("For i="+t+": "+bTree.search(t));
}
System.out.println();
bTree.printOrdered();
}
}