Skip to main content

Tree Traversal Implementation

Tree Traversal Implementation:


public class Node {

public int data;
public Node leftNode;
public Node rightNode;
Node(int Data){
this.data=Data;
this.leftNode=null;
this.rightNode=null;
}

}


public class Traversal {

public void inOrder(Node n) {
if(n==null) return;
inOrder(n.leftNode);
System.out.println(n.data);
inOrder(n.rightNode);
}
public void preOrder(Node n) {
if(n==null) return;
System.out.println(n.data);
preOrder(n.leftNode);
preOrder(n.rightNode);
}
public void postOrder(Node n) {
if(n==null) return;
postOrder(n.leftNode);
postOrder(n.rightNode);
System.out.println(n.data);
}
}

public class main {
public static void main(String s[]) {
Node n1= new Node(5);
Node n2= new Node(3);
Node n3= new Node(2);
Node n4= new Node(8);
Node n5= new Node(7);
n1.leftNode=n2;
n2.leftNode=n3;
n1.rightNode=n4;
n4.leftNode=n5;
Traversal t=new Traversal();
t.inOrder(n1);
System.out.println("************");
t.preOrder(n1);
System.out.println("************");
t.postOrder(n1);
}
}
OutPut:
2
3
5
7
8
************
5
3
2
8
7
************
2
3
7
8
5

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- randomknowledgeshare_ads_AdSense1_ -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-7425797582945891"
     data-ad-slot="7203573961"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

Comments

Popular posts from this blog

ifference between @RestController and @Controller

Difference between @RestController and @Controller Annotation in Spring MVC and REST Read more:  http://javarevisited.blogspot.com/2017/08/difference-between-restcontroller-and-controller-annotations-spring-mvc-rest.html#ixzz4sbGy2Glh The  @RestController  annotation in Spring MVC is nothing but a combination of  @Controller  and  @ResponseBody  annotation. It was added into Spring 4.0 to make the development of RESTful Web Services in Spring framework easier. If you are familiar with the  REST web services  you know that the fundamental difference between a web application and a REST API is that the response from a web application is generally view (HTML + CSS + JavaScript) while REST API just return data in form of JSON or XML. This difference is also obvious in the  @Controller  and  @RestController  annotation. The job of  @Controller  is to create a Map of model object and find a view but  @RestContr...

Presentation topics

Topics for presentation : Knockout.js, Angular.js, Node,js, Kendo UI, MVC 4, Nuget, Ninject, Unity Framework, Dependency Injection , PMC Package Manager Control, Mordenizer, Moq, Json, Html5, Entity Framework code1st. Spring.net Castle Windsor , Structure Map and Microsoft Unity ,
What happens when you compile/run the following code: class MyClass { public static void main(String[] args) { new MyClass(); } Ans: It executes and create a object for that class without reference. The output of the program is nothing.