forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
48 lines (43 loc) · 807 Bytes
/
BST.java
File metadata and controls
48 lines (43 loc) · 807 Bytes
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
package com.tree;
public class BST {
Node root;
public BST() {
root=null;
}
public Node NodeCreate(int value){
return new Node(value, null, null);
}
public void add(Node start, Node newNode){
if(root==null){
root=newNode;
return;
}
if(newNode.value> start.value){
if( start.right==null)
start.right=newNode;
add(start.right,newNode);
}
if(newNode.value< start.value){
if( start.left==null)
start.left=newNode;
add(start.left,newNode);
}
}
public void Search(int value, Node start){
if(start==null){
System.out.println("node isnot found");
return;
}
if(start.value==value)
{
System.out.println("node is found");
return;
}
if( value>start.value){
Search(value, start.right);
}
if( value<start.value){
Search(value, start.left);
}
}
}