-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_demo(Binary)
More file actions
71 lines (59 loc) · 1.54 KB
/
Copy pathTree_demo(Binary)
File metadata and controls
71 lines (59 loc) · 1.54 KB
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
70
71
package pracss;
class Node{
int key;
int data;
Node left , right;
public Node (int item) {
data = item;
left = right = null;
}
}
public class Binary_tree {
//root of binary tree
Node root;
Binary_tree() {
root = null;
}
void printPostorder(Node node) {
if(node == null)
return;
//first recur on left subtree
printPostorder(node.left);
printPostorder(node.right);
System.out.print(node.data+ " " );
}
void printInorder(Node node) {
if(node == null)
return;
//first recur on left subtree
printInorder(node.left);
System.out.print(node.data+ " " );
printInorder(node.right);
}
void printPreorder(Node node) {
if(node == null)
return;
//first recur on left subtree
System.out.print(node.data+ " " );
printPreorder(node.left);
printPreorder(node.right);
}
public static void main(String args[]) {
Binary_tree tree = new Binary_tree();
tree.root = new Node(40); // root node always 1
tree.root.left = new Node(30);
tree.root.right = new Node(50);
tree.root.left.left = new Node(20);
tree.root.left.right = new Node(35);
tree.root.left.left.left = new Node(15);
tree.root.left.left.right = new Node(25);
System.out.println("Inorder traversal of binary treee is :");
tree.printInorder(tree.root);
System.out.println();
System.out.println("Postorder traversal of binary treee is :");
tree.printPostorder(tree.root);
System.out.println();
System.out.println("Preorder traversal of binary treee is :");
tree.printPreorder(tree.root);
}
}