-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraversal.java
More file actions
48 lines (37 loc) · 1.28 KB
/
Traversal.java
File metadata and controls
48 lines (37 loc) · 1.28 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.traversal;
/**
*
* @author HP
*/
public class Traversal {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int[] array = { 27, 14, 35, 10, 19, 31, 42 };
for (int value : array) {
tree.insert(value);
}
int searchValue1 = 31;
Node result1 = tree.search(searchValue1);
if (result1 != null) {
System.out.println("\n[" + result1.data + "] Element found.");
} else {
System.out.println("\n[x] Element not found (" + searchValue1 + ").");
}
int searchValue2 = 15;
Node result2 = tree.search(searchValue2);
if (result2 != null) {
System.out.println("[" + result2.data + "] Element found.");
} else {
System.out.println("[x] Element not found (" + searchValue2 + ").");
}
System.out.print("\nPreorder traversal: ");
tree.preOrderTraversal(tree.root);
System.out.print("\nInorder traversal: ");
tree.inOrderTraversal(tree.root);
System.out.print("\nPostorder traversal: ");
tree.postOrderTraversal(tree.root);
}
}