Friday, April 26, 2024
HomeJavaEasy methods to examine if a node exists in a binary tree...

Easy methods to examine if a node exists in a binary tree or not in Java? Instance Tutorial


is a knowledge construction through which every node can have at most two youngsters. That’s, every node within the binary tree could have information, left youngster and proper youngster.

Timber
are the non-linear information construction that shops information hierarchy. The tree is a
assortment of components known as nodes. Nodes are linked by way of edges and
comprise information. The primary node of the tree known as the Root. Every node might or
might not have a youngsters node. The node which does not have any youngster node is
known as a leaf.

Beneath is the picture of the Binary search tree, all of the
inexperienced circles are known as nodes and so they have at most two different nodes known as
youngsters.

 Node 6 has two
youngsters, node 4 and node 7 respectively, And to the correct aspect of node 8 which
is node 10, has simply just one youngster, node 14, node 14 too has just one
youngster. Nodes 4, 7, and 13 are known as leaves as a result of they don’t have
youngsters.

Necessary issues to notice when figuring out is
if a tree is a binary tree or not
, that are :

  1. All information within the nodes
    of the left sub-tree are lesser than the correct.
  2. Within the Kids nodes, All
    information on the left are lesser than the correct.
  3. All information within the nodes of the
    left sub-tree is lesser than the basis
  4. All information within the nodes of the correct
    sub-tree is bigger than the basis.

A Binary search tree (I.e BST) is
a sort of binary tree. It can be outlined as a node-based binary tree. BST
can be known as ‘Ordered Binary Tree’. The BST information construction is
thought-about to be very environment friendly when in comparison with Arrays and Linked lists when it
involves insertion/deletion and looking out of things.

Equally,
insertion and deletion operations are extra environment friendly in Binary Search Tree. Once we need to
insert a brand new component, we roughly know through which subtree (left or proper) we are going to
insert the component.

Java Program to examine if a given node exists in a binary tree or not

Having understood the Binary tree Information construction, we’d be fixing an issue.
The issue is to examine whether or not a given node exists in a binary tree, if it
exists, it ought to return true, if not it ought to return false.

And, here’s a full Java Program which not implement binary tree but in addition solves this downside.

 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
public class BinaryNodeTree { // Binary tree node
    static class Node {
        int information;
        Node left;
        Node proper;
        public Node(int information) {
            this.information = information;
            left = proper = null;
        }
    };
    // Operate to traverse the tree in preorder
    // and examine if the given node exists in it
    static boolean nodeExists(Node node, int key) {
        if (node == null)
            return false;
        if (node.information == key)
            return true;
        // then examine left subtree /
        boolean node1 = nodeExists(node.left, key);
        // node discovered, no have to look additional
        if (node1) return true;
        //if  node is just not present in left,
        // then examine proper the sub-tree /
        boolean node2 = nodeExists(node.proper, key);
        if (node2) {
            return true;
        }
        return false;
    }
    // Driver Code
    public static void principal(String[] args) {
        BinaryNodeTree.Node root = new BinaryNodeTree.Node(0);
        root.left = new BinaryNodeTree.Node(1);
        root.left.left = new BinaryNodeTree.Node(3);
        root.left.left.left = new BinaryNodeTree.Node(6);
        root.left.proper = new BinaryNodeTree.Node(4);
        root.left.proper.left = new BinaryNodeTree.Node(8);
        root.left.proper.proper = new BinaryNodeTree.Node(9);
        root.proper = new BinaryNodeTree.Node(2);
        root.proper.left = new BinaryNodeTree.Node(5);
        root.proper.proper = new BinaryNodeTree.Node(7);
        int key = 6;
        if (nodeExists(root, key))
            System.out.println("true");
        else
            System.out.println("false");
    }
}

Yeah, That’s
the answer to the issue, Now let me stroll you thru the algorithm with a
detailed rationalization. I numbered the codes so you’d perceive which line
I’m referring to particularly, Let’s go!

In the event you examine from line 1
to line 10, you’d see that there are two courses there, The category “Node”
was embedded within the class “Binary tree Search”.

Be aware: It isn’t
obligatory to have it embedded, as you may also have it in a separate java
file, They solely simply should be in the identical package deal so the courses can entry
themselves.

line 3 to line 5 are occasion variables of the category “Node”

Line 6
to line 10 is the constructor the place the occasion variables are constructed.

From
Line 13 to line 30, is the place the implementation occurs. Line 13 declared a
methodology “Node exists” which takes in two parameters, Node, and key
respectively. Afterward, a sequence of circumstances are checked as we go about
checking if the nodes with the important thing exist.

Line 16 returns false if
the node is null, Line 18 returns true if the info within the node is identical as
the parameter “key” given. And This system would finish there as a result of we’ve got
seen what we’re in search of. But when not, The looking out continues as different
circumstances acknowledged beneath are checked.

Line 22 checks if presents in
left sub-tree, if true. This system ends there, if not, it proceeds to examine
the correct sub-tree and returns both true or false.

Line 32 is the
principal methodology and from line 34 nodes are been initialized with values beginning
from the basis nodes, to its youngsters and youngsters’s youngsters.

Line
43 initialized the important thing we’re in search of to six and in line 44 the strategy was
known as to search out the important thing and right here is the output:

Output

true

It
was true as a result of 6 was current within the node.

That is all about easy methods to examine if a given node is current in a given binary tree or not in Java.
This is a crucial binary tree-based coding query and in case you are severe about doing nicely
on the technical or coding interviews, you must follow extra binary tree-based coding questions like this. In the event you want extra questions you
may also methods take a look at the next sources

Different information construction and algorithms tutorials for Java Programmers

  • High 50 Java Applications from Coding Interviews (see right here)
  • 5 Free Information Construction and Algorithms Programs for Programmers (programs)
  • Easy methods to take away a component from the array with out utilizing a third-party library (examine right here)
  • Easy methods to implement the Quicksort algorithm in Java? (answer)
  • Easy methods to examine if a String is a Palindrome in Java? [solution]
  • Easy methods to discover the lacking quantity in a sorted array in Java? [answer]
  • 10 Algorithms Books Each Programmer Ought to Learn (books)
  • 10 Algorithm books Each Programmer Ought to Learn (listing)
  • 5 Books to be taught information construction and algorithms in Java? (books)
  • 10 Free Information Construction and Algorithm Programs for Programmers (programs)
  • 100+ Information Construction Coding Issues from Interviews (questions)
  • Easy methods to print the Fibonacci sequence in Java with out utilizing Recursion? [solution]
  • Easy methods to examine if an integer is an influence of two with out utilizing division or modulo operator?[hint]
  • Easy methods to discover all permutations of String in Java? [solution]
  • High 21 String coding interview questions (see right here)
  • Easy methods to discover the most important and smallest quantity in an array in Java (learn right here)
  • Easy methods to discover two most numbers on an integer array in Java (examine right here)
In case you have any recommendations to make this algorithm higher, be happy to
recommend. The interviewer loves individuals who provide you with their very own
algorithms or give some contact to common algorithms.

P. S. – If you wish to enhance your information construction and algorithms
expertise and also you want sources then you may also check out my listing
of finest information construction and algorithm programs for Java builders. It accommodates some finest on-line programs from Udemy, Coursera, edX, and different sources for Java builders. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments