-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.java
69 lines (60 loc) · 1.61 KB
/
BinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.company;
class Node {
int key;
Node left, right;
public Node(int item) {
this.key = item;
left = right = null;
}
}
class BinaryTree{
public Node root;
public BinaryTree()
{
root = null;
}
void InOrder(Node node)
{
if(node == null) return;
InOrder(node.left);
System.out.println(node.key + " ");
InOrder(node.right);
}
void PreOrder(Node node)
{
if(node == null) return;
System.out.println(node.key +" ");
PreOrder(node.left);
PreOrder(node.right);
}
void PostOrder(Node node)
{
if (node == null)return;
PostOrder(node.left);
PostOrder(node.right);
System.out.println(node.key +" ");
}
void InOrder(){InOrder(root);}
void PreOrder(){PreOrder(root);}
void PostOrder(){PostOrder(root);}
}
public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("Hello Harshal Raverkar");
BinaryTree bt = new BinaryTree();
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.left.right = new Node(5);
bt.root.right.left = new Node(6);
bt.root.right.right = new Node(7);
System.out.println("Inorder Traversal of binary tree");
bt.InOrder();
System.out.println("PreOrder Traversal of Binary tree");
bt.PreOrder();
System.out.println("PostOrder Traversal of binary tree");
bt.PostOrder();
}
}