-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTinsert.java
More file actions
50 lines (44 loc) · 1.74 KB
/
BSTinsert.java
File metadata and controls
50 lines (44 loc) · 1.74 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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.binarysearchtreeinsert;
/**
*
* @author HP
*/
public class BSTinsert {
public void insert(int data) {
Node tempNode = new Node(data); // Create a new node with the given data
Node current; // Current node for traversal
Node parent = null; // Parent node to keep track of the parent
// If the tree is empty, set the new node as the root
if (root == null) {
root = tempNode;
} else {
current = root; // Start at the root node
// Traverse the tree to find the correct position for the new node
while (true) {
parent = current; // Store the current node as the parent
// If the data is smaller, move to the left child
if (data < current.data) {
current = current.leftChild;
// If the left child is null, insert the new node here
if (current == null) {
parent.leftChild = tempNode;
return;
}
}
// If the data is larger, move to the right child
else {
current = current.rightChild;
// If the right child is null, insert the new node here
if (current == null) {
parent.rightChild = tempNode;
return;
}
}
}
}
}
}