-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchtree.java
More file actions
52 lines (42 loc) · 1.48 KB
/
Searchtree.java
File metadata and controls
52 lines (42 loc) · 1.48 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.searchtree;
/**
*
* @author HP
*/
public class Searchtree {
public static Binarytreeinsert root = null;
public static void main(String[] args) {
root = insert(root, 10);
insert(root, 5);
insert(root, 15);
// Search for a node with data 5
Binarytreeinsert result = search(root, 5);
if (result != null) {
System.out.println("\nNode found: " + result.data);
} else {
System.out.println("\nNode not found.");
}
}
// Search method to find a node with specified data
public static Binarytreeinsert search(Binarytreeinsert root, int data) {
Binarytreeinsert current = root; // Start from the root
System.out.print("Visiting elements: ");
// Traverse the tree until we find the data or reach a leaf
while (current != null && current.data != data) {
System.out.print(current.data + " "); // Print the visited node
// Go to the left subtree if the data is less than the current node's data
if (data < current.data) {
current = current.leftChild;
}
// Otherwise, go to the right subtree
else {
current = current.rightChild;
}
}
// Return the found node or null if not found
return current;
}
}