You are reading the article **Binary Search Tree In Java** updated in December 2023 on the website Hatcungthantuong.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. *Suggested January 2024 Binary Search Tree In Java*

A binary search tree is a data structure that allows us to keep a sorted list of numbers in a short amount of time. It is also called a binary tree because each tree node can only have two siblings. The binary search tree may be used to search for the presence of a number; it is termed a search tree. The running time complexity is O(log(n)) time; the characteristics that distinguish a binary search tree from a basic binary tree are as follows –

Start Your Free Software Development Course

1. The nodes of the left subtree are all smaller than the root node.

2. The nodes of the right subtree are all greater than the root node.

3. Each node of the subtrees is likewise BSTs, meaning they have the same two qualities as the node itself.

Working on the binary search tree in Java1. Let the specified array is:

Given array: [8, 6, 2, 7, 9, 12, 4, 10]

2. Let’s start with the top element 43. Insert 43 as the tree’s root.

3. If the next element is less than the root node element, it should be inserted as the root of the left sub-tree.

4. If not, it should be inserted as the root of the right sub-tree.

The image below depicts the process of constructing a binary search tree using the provided elements.

Binary search tree operations:

The operations supported by the binary search tree in Java are shown in the below list –

1. Searching in BST – In a binary search tree, finding the position of a certain element.

2. Insertion in BST – Adding a new element to the binary search tree in the proper location ensures that the binary search tree property is not broken.

3. Deletion in BST – Remove a specific node in a binary search tree. However, depending on the number of children a node has, there can be a variety of deletion scenarios.

Examples Example #1// The program can be tested in Eclipse IDE, JAVA 11

package jex; import java.util.Scanner; class binarySearchTree { class Node { int key; Node left, right; public Node(int data){ key = data; left = right = null; } } Node root; binarySearchTree(){ root = null; } void deleteKey(int key) { root = delete(root, key); } Node delete(Node root, int key) { if (root == null) return root; if (key < root.key) root.left = delete(root.left, key); root.right = delete(root.right, key); else { if (root.left == null) return root.right; else if (root.right == null) return root.left; root.key = minKey(root.right); root.right = delete(root.right, root.key); } return root; } int minKey(Node root) { int min = root.key; while (root.left != null) { min = root.left.key; root = root.left; } return min; } void insertKey(int key) { root = insert(root, key); } Node insert(Node root, int key) { if (root == null) { root = new Node(key); return root; } if (key<root.key) root.left = insert(root.left, key); root.right = insert(root.right, key); return root; } void inorder() { inorder(root); } void inorder(Node root) { if (root != null) { inorder(root.left); System.out.print(root.key + " "); inorder(root.right); } } boolean searchKey(int key) { root = search(root, key); if (root!= null) return true; else return false; } Node search(Node root, int key) { return root; return search(root.left, key); return search(root.right, key); } } public class client{ public static void main(String[] args) { binarySearchTree t = new binarySearchTree(); t.insertKey(8); t.insertKey(6); t.insertKey(2); t.insertKey(7); t.insertKey(9); t.insertKey(12); t.insertKey(4); t.insertKey(10); System.out.println( "The binary search tree created with the input data :"); t.inorder(); System.out.println( "nThe binary search tree after deleting 4 leaf node :"); t.deleteKey(4); t.inorder(); System.out.println( "nThe binary search tree after Delete 12 node with 1 child is :"); t.deleteKey(12); t.inorder(); boolean res = t.searchKey (9); System.out.println( "n The node 9 found in binary search tree is :" + res ); res = t.searchKey (12); System.out.println( "n The node 10 found in binary search tree is :" + res ); } }Output:

As in the above program, the binarySearchTree class is created, which contains another inner class Node and also contains the constructor and methods. The methods defined in the class are deleteKey(), delete(), minKey(), insertKey(), insert(), inorder(), searchKey(), and search() to perform the specific operations. In the main function, the binarySearchTree class object is created, insert some elements into it, and next call the methods of the binary Search Tree class on its object, as seen in the above output.

ConclusionThe binary search tree is also known as a binary tree because each tree node can only have two siblings. A binary search tree is a data structure that allows keeping a sorted list of numbers in a short amount of time. The operation that can be performed on the binary search tree: traversing, inserting, deleting, and searching.

Recommended ArticlesThis is a guide to Binary Search Tree in Java. Here we discuss the Definition, working of the binary search tree in Java, and examples with code implementation. You may also have a look at the following articles to learn more –

You're reading __Binary Search Tree In Java__

## Binary Tree In Data Structure (Example)

What is a Binary Tree?

The word binary means two. In the tree data structure “Binary Tree”, means a tree where each node can have a maximum of two child nodes (left and right nodes). It is a simple binary tree.

However, there’s another binary tree that is used most frequently and has several use cases. It’s called the Binary Search Tree (BST). This tree can make the search algorithm way faster, precisely log(n) time complexity. In the data structure, n means the number of nodes in the binary tree.

What are the Differences Between Binary Tree and Binary Search Tree?The difference between the BST and regular Binary tree is in the BST left node has a smaller value than the root node, and the right node has a larger value than the root node. So, the left subtree will always contain a smaller value than the root, and the right subtree will always contain a larger value than the root.

In this Algorithm tutorial, you will learn:

Example of Binary Search TreesLet’s have the following example for demonstrating the concepts of the Binary Search Tree.

Here you can all the nodes follow the given discipline. There’s a formula for the maximum number of nodes in the Binary Search Tree. If we observe the above tree, we can see each node has two children except all the leaf nodes. And the height(h) of the given Binary Tree is 4. The formula is 2h – 1. So, it gives 15.

The above given image is not a complete Binary Tree or Balanced Binary Tree, is called the Complete Binary tree or Balanced Binary Tree. There’s another Data Structure called AVL (another type of Binary Tree) that optimizes the height of Binary Tree and performs search faster for the BST like in Fig 3.

Try to calculate the in-order traversal of the Binary Tree given above. You’ll find that it will give a non-decreasing sorted array, and Traversal algorithms will be the same as the Binary Tree.

Types of Binary Tree:Here are some important types of Binary Tree:

Full Binary Tree: Each node can have 0 or 2 child nodes in this binary tree. Only one child node is not allowed in this type of binary tree. So, except for the leaf node, all nodes will have 2 children.

Full Binary Tree: Each node can have 0 or 2 nodes. It seems like the Full Binary Tree, but all the leaf elements are lean to the left subtree, whereas in the full binary tree node can be in the right or left subtree.

Perfect Binary Tree: All the nodes must have 0 or 2 nodes, and all the leaf nodes should be at the same level or height. The above example of a full binary tree structure is not a Perfect Binary Tree because node 6 and node 1,2,3 are not in the same height. But the example of the Complete Binary Tree is a perfect binary tree.

Degenerate Binary Tree: Every node can have only a single child. All the operations like searching, inserting, and deleting take O(N) time.

Balanced Binary Tree: Here this binary tree, the height difference of left and right subtree is at most 1. So, while adding or deleting a node, we need to balance the tree’s height again. This type of Self-Balanced Binary Tree is called the AVL tree.

There are three basic operations of BST. Those are discussed in detail below.

Implementation of Binary Tree in C and C++:using namespace std; struct Node { int value; struct Node *left, *right; } struct Node *getEmptynode(int val) { struct Node *tempNode = (struct Node *)malloc(sizeof(struct Node)); return tempNode; } struct Node *successor(struct Node *node) { struct Node *present = node; { } return present; } struct Node *insert(struct Node *node, int value) { if (node == NULL) { return getEmptynode(value); } { } else { } return node; } int searchInBST(struct Node *node, int value) { struct Node *current = node; { { } else { } if (current == NULL) { return 0; } } return 1; } void inorder(struct Node *root) { if (root != NULL) { } } struct Node *deleteNode(struct Node *node, int value) { if (node == NULL) { return node; } { } { } else { { free(node); return temp; } { free(node); return temp; } } return node; } int main() { struct Node *root = NULL; root = insert(root, 8); root = insert(root, 4); root = insert(root, 12); root = insert(root, 2); root = insert(root, 6); root = insert(root, 10); root = insert(root, 14); root = insert(root, 1); root = insert(root, 3); root = insert(root, 5); root = insert(root, 7); root = insert(root, 9); root = insert(root, 11); root = insert(root, 13); root = insert(root, 15);

cout << “InOrder Traversal after inserting all nodes: ” << endl; inorder(root); root = insert(root, -10); cout << “nInOrder Traversal after inserting -10 : ” << endl; inorder(root); cout << “nSearching -5 in the BST: ” << searchInBST(root, -5) << endl; cout << “Searching -10 in the BST: ” << searchInBST(root, -10) << endl; root = deleteNode(root,8); cout<<“After deleting node 8, inorder traversal: “<<endl; inorder(root); root = deleteNode(root,-10); cout<<“nAfter deleting node -10, inorder traversal: “<<endl; inorder(root); }

Output:

InOrder Traversal after inserting all nodes: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 InOrder Traversal after inserting -10 : 10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Searching -5 in the BST: 0 Searching -10 in the BST: 1 After deleting node 8, inorder traversal: -10 1 2 3 4 5 6 7 9 10 11 12 13 14 15 After deleting node -10, inorder traversal: 1 2 3 4 5 6 7 9 10 11 12 13 14 15 Implementation of Binary Tree in Python class Node: def __init__(self,value): chúng tôi = None self.right = None self.value = value def insert(root,value): if root == None: return Node(value) if value< root.value: chúng tôi = insert(root.left,value) else: root.right = insert(root.right,value) return root def searchInBST(root,value): current = root while current.value != value: current = current.left else: current = current.right if current == None: return "Not found" return "Found" def inorder(root): if root != None: inorder(root.left) print(root.value,end=" ") inorder(root.right) def successor(root): present = root while present != None and chúng tôi != None: present = present.left return present def deleteNode(root,value): if root == None: return root if value < root.value: chúng tôi = deleteNode(root.left, value) root.right = deleteNode(root.right, value) else: if chúng tôi == None: temp = root.right root = None return temp elif root.right == None: temp = root.left root = None return temp temp = successor(root.right) root.value = temp.value root.right = deleteNode(root.right, temp.value) return root root = Node(8) root = insert(root, 4) root = insert(root, 12) root = insert(root, 2) root = insert(root, 6) root = insert(root, 10) root = insert(root, 14) root = insert(root, 1) root = insert(root, 3) root = insert(root, 5) root = insert(root, 7) root = insert(root, 9) root = insert(root, 11) root = insert(root, 13) root = insert(root, 15) print("InOrder Traversal after inserting all nodes: ") inorder(root) root = insert(root, -10) print("nInOrder Traversal after inserting -10 : ") inorder(root) print("nSearching -5 in the BST: ",searchInBST(root, -5)) print("Searching -5 in the BST: ",searchInBST(root, -10)) root = deleteNode(root,8) print("After deleting node 8, inorder traversal:") inorder(root) root = deleteNode(root,-10) print("nAfter deleting node -10, inorder traversal:") inorder(root)Output:

InOrder Traversal after inserting all nodes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 InOrder Traversal after inserting -10 : -10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Searching -5 in the BST: Not found Searching -5 in the BST: Found After deleting node 8, inorder traversal: -10 1 2 3 4 5 6 7 9 10 11 12 13 14 15 After deleting node -10, inorder traversal: 1 2 3 4 5 6 7 9 10 11 12 13 14 15 Application of Binary Tree:Here are some common applications of Binary Tree:

Organizing node data in sorted order

Used in the map and set node objects in programming language libraries.

Searching for elements in the data structures

» Learn our next tutorial about Combination Algorithm

## Kruskal’s Minimum Spanning Tree Algorithm

Adjacency matrix

Output: Edge: B–A And Cost: 1 Edge: E–B And Cost: 2 Edge: F–E And Cost: 2 Edge: C–A And Cost: 3 Edge: G–F And Cost: 3 Edge: D–A And Cost: 4 Total Cost: 15

Algorithm kruskal(g: Graph, t: Tree)Input − The given graph g, and an empty tree t

Output − The tree t with selected edges

Begin create set for each vertices in graph g for each set of vertex u do add u in the vertexSet[u] done sort the edge list. count := 0 while count <= V – 1 do //as tree must have V – 1 edges ed := edgeList[count] if the starting vertex and ending vertex of ed are in same set then merge vertexSet[start] and vertexSet[end] add the ed into tree t count := count +1 done #define V 7 #define INF 999 using namespace std; int costMat[V][V] = { {0, 1, 3, 4, INF, 5, INF}, {1, 0, INF, 7, 2, INF, INF}, {3, INF, 0, INF, 8, INF, INF}, {4, 7, INF, 0, INF, INF, INF}, {INF, 2, 8, INF, 0, 2, 4}, {5, INF, INF, INF, 2, 0, 3}, {INF, INF, INF, INF, 4, 3, 0} }; typedef struct { int u, v, cost; }edge; void swapping(edge &e1, edge &e2) { edge temp; temp = e1; e1 = e2; e2 = temp; } class Tree { int n; edge edges[V-1]; //as a tree has vertex-1 edges public: Tree() { n = 0; } void addEdge(edge e) { edges[n] = e; n++; } void printEdges() { //print edge, cost and total cost int tCost = 0; for(int i = 0; i<n; i++) { cout << "Edge: " << char(edges[i].u+'A') << "--" <<char(edges[i].v+'A'); cout << " And Cost: " << edges[i].cost << endl; tCost += edges[i].cost; } cout << "Total Cost: " << tCost << endl; } }; class VSet { int n; int set[V]; //a set can hold maximum V vertices public: VSet() { n = -1; } void addVertex(int vert) { set[++n] = vert; //add vertex to the set } int deleteVertex() { return set[n--]; } friend int findVertex(VSet *vertSetArr, int vert); friend void merge(VSet &set1, VSet &set2); }; void merge(VSet &set1, VSet &set2) { //merge two vertex sets together set1.addVertex(set2.deleteVertex()); } int findVertex(VSet *vertSetArr, int vert) { //find the vertex in different vertex sets for(int i = 0; i<V; i++) for(int j = 0; j<=vertSetArr[i].n; j++) if(vert == vertSetArr[i].set[j]) return i; } int findEdge(edge *edgeList) { //find the edges from the cost matrix of Graph and store to edgeList int count = -1, i, j; for(i = 0; i<V; i++) for(j = 0; j<i; j++) if(costMat[i][j] != INF) { count++; edgeList[count].u = i; edgeList[count].v = j; edgeList[count].cost = costMat[i][j]; } return count+1; } void sortEdge(edge *edgeList, int n) { //sort the edges of graph in ascending order of cost int flag = 1, i, j; for(i = 0; i<(n-1) && flag; i++) { flag = 0; for(j = 0; j<(n-i-1); j++) swapping(edgeList[j], edgeList[j+1]); flag = 1; } } } void kruskal(Tree &tr) { int ecount, maxEdge = V*(V-1)/2; edge edgeList[maxEdge], ed; int uloc, vloc; VSet VSetArray[V]; ecount = findEdge(edgeList); for(int i = 0; i < V; i++) VSetArray[i].addVertex(i); //each set contains one element sortEdge(edgeList, ecount); //ecount number of edges in the graph int count = 0; while(count <= V-1) { ed = edgeList[count]; uloc = findVertex(VSetArray, ed.u); vloc = findVertex(VSetArray, ed.v); if(uloc != vloc) { //check whether source abd dest is in same set or not merge(VSetArray[uloc], VSetArray[vloc]); tr.addEdge(ed); } count++; } } int main() { Tree tr; kruskal(tr); tr.printEdges(); } Output Edge: B--A And Cost: 1 Edge: E--B And Cost: 2 Edge: F--E And Cost: 2 Edge: C--A And Cost: 3 Edge: G--F And Cost: 3 Edge: D--A And Cost: 4 Total Cost: 15## Searching Elements In Vector Using Index In Java

Vectors implement the List interface and are used to create dynamic arrays. The array whose size is not fixed and can grow as per our needs is called as a dynamic array. The vectors are very similar to ArrayList in terms of use and features.

In this article, we will learn how we can create a vector and search for a particular element by its index in Java. Let’s discuss Vector first.

VectorAlthough vector is similar to ArrayList in many ways, there exist some differences too. The Vector class is synchronized and several legacy methods are contained by it.

Legacy Class − Before the release of Java version 1.2 when the Collection Framework was not introduced, there were some classes that depicts the features of this framework’s classes and were used in the place of those classes. For example, Vector, Dictionary, and Stack. With JDK 5, Java creators reengineered Vectors and made them fully compatible with Collections.

We use the following syntax to create a Vector.

SyntaxHere, in TypeOfCollection specify the data type of the elements that will be stored in collection. In nameOfCollection give suitable to your collection.

Program to Search elements in Vector by its Index indexOf()To search for an element in Vector by its index we can use this method. There exist two ways of using the ‘indexOf()’ method −

indexOf(nameOfObject) − It takes an object as an argument and returns its integer value of index. If the object does not belong to the specified collection, it simply returns -1.

indexOf(nameOfObject, index) − It takes two arguments one is an object and the other is an index. It will start searching for the object from specified index value.

Example 1In the following example, we will define a vector named ‘vectlist’ and store a few objects in it by using the ‘add()’ method. Then, using indexOf() method with single argument, we will search for the element.

import java.util.*; public class VectClass { public static void main(String args[]) { vectList.add("Tutorix"); vectList.add("Simply"); vectList.add("Easy"); vectList.add("Learning"); vectList.add("Tutorials"); vectList.add("Point"); int indexValue = vectList.indexOf("Tutorials"); System.out.println("Index of the specified element in list: " + indexValue); } } Output Index of the specified element in list: 4 Example 2The following example demonstrates if the element is not available in the collection then ‘indexOf()’ returns -1.

import java.util.*; public class VectClass { public static void main(String args[]) { vectList.add("Tutorix"); vectList.add("Simply"); vectList.add("Easy"); vectList.add("Learning"); vectList.add("Tutorials"); vectList.add("Point"); int indexValue = vectList.indexOf("Tutorialspoint"); System.out.println("Index of the specified element in list: " + indexValue); } } Output Index of the specified element in list: -1 Example 3The following example illustrates the use of ‘indexOf()’ with two arguments. The compiler will start searching for the given element from index 3.

import java.util.*; public class VectClass { public static void main(String args[]) { vectList.add("Tutorix"); vectList.add("Simply"); vectList.add("Easy"); vectList.add("Learning"); vectList.add("Tutorials"); vectList.add("Point"); vectList.add("Easy"); vectList.add("Learning"); int indexValue = vectList.indexOf("Easy", 3); System.out.println("Index of the specified element in list: " + indexValue); } } Output Index of the specified element in list: 6 ConclusionIn this article, we have discussed a few examples that show the usefulness of the indexOf() method while searching for a particular element in a Vector. We also learned about Vector in Java.

## How To Call A Method In Java

How to call a method in Java – the basics

To call a method in Java, you type the method’s name, followed by brackets.

For example, the following will call a method called “helloMethod()”:

Code

helloMethod();In order for this to work though, we first need to create our helloMethod() method. We can see what helloMethod might look like, here:

Code

public static void helloMethod() { System.out.println("Hello world!"); How to create methods in JavaLet’s rewind a moment and take a closer look at the method we created. Why does it look the way it does?

To build a method, we use a number of statements to define that method. In the previous example:

Public – Means that the method is accessible to other classes outside of this one

Static – Means that the method belongs to the class and not the instance of the class

Void – Means that the method does not return a value

If none of that makes any sense to you, don’t worry! Most new developers will be able to use “public static void” for the majority of their methods and won’t have to worry. That said, we’ll address two of these phrases in the coming sections.

It is considered good practice to name Java methods using “camel case.” As we aren’t allowed spaces, camel case gets around this by capitalizing every word except the first one. Methods should generally be verbs (though I have bent that rule here!).

How to use arguments when calling a method in JavaAs mentioned, we can place arguments inside of the brackets when defining our methods. This allows us to pass variables, and therefore values, between methods.

For example, a “String” is a type of variable that holds alphanumeric characters. We create a string by using the word “String” followed by the name.

Now, whenever we call that method, we need to add the value we want to use in the brackets.

Code

helloClass.helloMethod("Hello there!"); public static void helloMethod(String helloMessage) { System.out.println(helloMessage); } How to call a method from outside the classA public method is a method that can be called from outside your class. To do this, you use the following syntax:

nameOfClass.nameOfMethod(arguments)

For example:

Code

class Main { public static void main(String[] args) { helloClass.helloMethod(); } } class helloClass { public static void helloMethod() { System.out.println("Hello world!"); } }However, if we wanted to prevent this from working, we would simply replace the word “public” with the word “private”.

How to return valuesFinally, we can return specific values from our methods. Let’s see how this might be used.

Let’s say we decide we want the method to provide our greeting but not display it onto the screen. Thus, we might make the method return a string. To do this, we change the word “void” for the type of variable we want to return, and we add “return value” at the end of the method.

This changes how we call a method in Java, because we can simply insert the name of the method in-line in our code, as though it were a variable:

Code

class Main { public static void main(String[] args) { System.out.println(helloMethod()); } public static String helloMethod() { return "Hello there!"; }## How Compareto Works In Java With Examples

Introduction to compareTo Java

compareTo() is a method in Java that compares the string given with the current string in a lexicographical manner. Comparison is done on the basis of the Unicode value of characters available in the string.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Following are the different conditions in the compareTo() method.

If string 1 is lexicographically larger than string 2, a positive number will be returned.

If string 1 is lexicographically smaller than string 2, a negative number will be returned.

If string 1 is lexicographically equal to string 2, ‘0’will be returned.

Below is the syntax of compareTo() method:

public int compareTo(String s2)Here, s2 is the string that is used for comparison with the current string. An integer value will be returned on calling this method.

How compareTo works in Java?compareTo() method can be used in three ways.

Examples of compareTo Java

Given below are the examples of compareTo Java:

Example #1Java program to implement compareTo method that compares two strings.

Code:

public class compareToExample { public static void main(String args[]) { String s1 = "Happiness lies within you"; String s2 = "Happiness LIES WITHIN YOU"; String s3 = "Happiness lies within you"; System.out.println( " Compare s1 and s2 : "+ V1 ) ; System.out.println( " Compare s1 and s3 : "+ v2 ) ; System.out.println(" Compare s2 and s3 : "+ v3 ) ; }}Output:

In this program, three strings are created s1, s2 and s3. Three variables, v1, v2 and v3, are also created for storing the comparison results of s1&s2, s1&s3, and s2&s3, respectively. It can be seen that a positive number is returned on comparing s1 & s2, and a negative number is returned on comparing s2 &s3. As both s1 and s3 are equal, 0 is returned in the second case.

Example #2Java program to implement compareTo method that compares a string and an object.

Code:

public class compareToExample { public static void main(String args[]) { String s1 = "Happiness lies within you"; System.out.println( " Compare s1 and argument : "+ v2 ) ; } }Output:

In this program, a string s1 and variable v1 are created first. Another string is passed as an argument in the compareTo() method, and it can be seen that a positive number is returned on comparing s1 and argument.

Example #3Java program to find the length of a string using the compareTo method.

Code:

public class compareToExample { public static void main(String args[]) { String s1 = "Happiness lies within you"; String s2 = ""; System.out.println( " Length of s1 : "+ V1 ) ; System.out.println( " Length of s1 : "+ v2 ) ; } }Output:

In this program, two strings are created, s1 and s2, where s2 is a null string. If the given string is compared with a nullstring, then the length of the non-empty string will be returned. If a comparison is done in reverse order, a negative value of the length will be returned.

Example #4Java program to implement compareToIgnoreCase method that compares two strings.

Code:

public class compareToExample { public static void main(String args[]) { String s1 = "Happiness lies within you"; String s2 = "Happiness LIES WITHIN YOU"; String s3 = "Happiness lies within you"; System.out.println( " Compare s1 and s2 : "+ V1 ) ; System.out.println( " Compare s1 and s3 : "+ v2 ) ; System.out.println(" Compare s2 and s3 : "+ v3 ) ; } }As already seen, compareToIgnoreCase ignores the case and compares the strings. As the three strings differ only in cases, 0 will be returned on calling this method.

Example #5Java program to implement compareToIgnoreCase method that compares a string and an object.

Code:

public class compareToExample { public static void main(String args[]) { String s1 = "Happiness lies within you"; System.out.println( " Compare s1 and argument : "+ v2 ) ; } }Output:

In this program, a string s1 and variable v1 are created first. Another string is passed as an argument in the compareToIgnoreCase() method, and it can be seen that 0 is returned as the case is ignored.

ConclusioncompareTo() is a Java method that compares the string given with the current string in a lexicographical manner. In this article, different aspects such as syntax, working, and examples of the compareTo() method is seen in detail.

Recommended ArticlesThis is a guide to compareTo Java. Here we discuss the introduction, how compareTo works in java? Along with examples, respectively. You may also have a look at the following articles to learn more –

Update the detailed information about **Binary Search Tree In Java** on the Hatcungthantuong.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!