Skip to main content

Binary Search Tree in Java implementation (reference based, dynamic memory)

  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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.Scanner;
 class BST {
    static BST.Node root = null;
    public void insert(int num) {
        if (root == null) {
            root = new BST.Node(num);
        } else {// root node is not empty
            BST.Node temp = root;
            while (temp != null) {

                if (num <= temp.getVal()) {
                    if (temp.getLeft() != null)
                        temp = temp.getLeft();
                    else {
                        temp.setLeft(new Node(num));
                        break;
                    }
                } else {
                    if (temp.getRight() != null)
                        temp = temp.getRight();
                    else {


                        temp.setRight(new Node(num));
                        break;
                    }

                }

            }


        }
    }

    public void search(int num, Node root) {
        if (root == null) {
            System.out.println("Number is not present ");
            return;
        }
        if (root.getVal() == num) {
            System.out.println("Number is present ");
        } else {
            if (num < root.getVal()) search(num, root.getLeft());
            else search(num, root.getRight());
        }


    }

    public void display(Node root) {
        if (root == null)
            return;
        display(root.getLeft());
        System.out.println(" " + root.getVal() + " ");
        display(root.getRight());

    }

    public static void main(String[] args) {
        BST bst= new BST();
        byte choice = 0;
        while (choice != 4) {
            int num;

            Scanner in = new Scanner(System.in);
            System.out.print("\nPress \n1 to insert \n2. To search \n3. Display \n4 to exit->");
            choice = in.nextByte();
            switch (choice) {
                case 1:
                    System.out.print("Enter the number ->");
                    num = in.nextInt();
                    bst.insert(num);
                    break;
                case 2:
                    System.out.print("Enter the Number ");
                    num = in.nextInt();
                    bst.search(num, root);
                    break;
                case 3:
                    bst.display(root);


            }

        }

    }

    private static class Node {
        private int val;
        private Node left = null;
        private Node right = null;

        public Node(int i) {
            this.val = i;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }

    }
}

Comments

  1. Your Affiliate Profit Machine is waiting -

    Plus, making profit with it is as easy as 1..2..3!

    Here are the steps to make it work...

    STEP 1. Input into the system what affiliate products the system will advertise
    STEP 2. Add push button traffic (it takes JUST 2 minutes)
    STEP 3. Watch the system grow your list and up-sell your affiliate products for you!

    Are you ready to make money ONLINE???

    Click here to activate the system

    ReplyDelete
  2. Did you realize there is a 12 word phrase you can tell your crush... that will trigger intense emotions of love and impulsive attraction to you buried within his chest?

    That's because hidden in these 12 words is a "secret signal" that triggers a man's impulse to love, look after and guard you with his entire heart...

    12 Words Who Fuel A Man's Love Response

    This impulse is so hardwired into a man's mind that it will drive him to try harder than ever before to make your relationship as strong as it can be.

    In fact, triggering this powerful impulse is so essential to getting the best ever relationship with your man that the instance you send your man one of these "Secret Signals"...

    ...You will immediately find him expose his soul and mind for you in a way he's never expressed before and he will distinguish you as the one and only woman in the galaxy who has ever truly appealed to him.

    ReplyDelete
  3. According to Stanford Medical, It's really the one and ONLY reason this country's women get to live 10 years longer and weigh 19 KG lighter than we do.

    (And actually, it has totally NOTHING to do with genetics or some secret-exercise and really, EVERYTHING about "HOW" they are eating.)

    BTW, I said "HOW", and not "WHAT"...

    Tap this link to see if this brief questionnaire can help you discover your true weight loss possibilities

    ReplyDelete
  4. Your Affiliate Money Making Machine is waiting -

    Plus, making profit with it is as simple as 1-2-3!

    This is how it all works...

    STEP 1. Choose affiliate products you want to promote
    STEP 2. Add some PUSH button traffic (it takes JUST 2 minutes)
    STEP 3. Watch the affiliate system explode your list and upsell your affiliate products all for you!

    Are you ready to make money automatically??

    Get the full details here

    ReplyDelete

Post a Comment

share your thoughts ....

Popular posts from this blog

13 websites to register your free domain

Register your Free Domain Now!! 1)  .tk Dot TK is a FREE domain registry for websites on the Internet. It has exactly the same power as other domain extensions, but it’s free! Because it’s free, millions of others have been using .TK domains since 2001 – which makes .TK powerful and very recognizable.  Your website will be like www.yourdomainname.tk . It is free for 1 year. It’s a ccTLD domain whixh having the abbreviation  Tokelau. To create a .tk domain, Visit   www.dot.tk 2) co.cc Co.cc is completely free domain which is mostly used by blogspot bloggers because of it’s easy to use DNS system. Creating a co.cc for blogger is simple ( for instructions- “click here”). Your website will be like www.yourdomainname.co.cc . To create a .co.cc domain, visit www.co.cc 3)   co.nr co.nr is too like co.cc. Your website will be like  www.yourdomainname.co.nr . You can add it for blogger also.. To create a .co.cc domain, vi...

How to Put Google Adsense Below Post Title in Blogger?

Adsense is used by majority of expert bloggers for their website monetization because it is a cookie based contextual advertising system that shows targeted ads relevant to the content and reader. As bloggers are paid on per click basis, they try various ad placements on the blog to  increase the revenue  and get maximum clicks on the ad units. Well, on some blogs, you might have seen Adsense ad units placed below the post title. Do you know why? It is because the area just below the post title gets the most exposure and is the best place to put AdSense ad units to increase  Click Through Rate (CTR). Even though ads below post title work like a charm but this doesn’t mean that it will work for you as well. If you want to find out the best AdSense ads placement for your blog, try experimenting by placing ads at various locations such as header, sidebar, footer, etc. You can try other  blog monetization methods  as well to effectively monetize y...

Batch file to C++ souce code generator ...

Batch file to C++ souce code generator ... Well this project was made upon CODEBLOCK using c++ HERE IS THE SOURCE CODE: download it as .cpp form by clicking here.. mirror 1                                                   mirror 2. One drive if YOu want the exe file only then click here (500 KB) Mirror 1. MULTCLOUD  FOR PASSWORD TO THIS SITE VISIT THE FOLLOWING LINK OR SUMBIT THE FORM BELOW  >>> LINK :   https://goo.gl/forms/DiQHvEjfavsaU18U2                              or Loading...

Streamlining Java Web Application Deployment with React WAR Generator

In the ever-evolving world of web development, managing builds and deployments can often be cumbersome and error-prone. Today, we're excited to introduce a tool designed to simplify and streamline this process: the React WAR Generator . What is the React WAR Generator? The React WAR Generator is a Python-based tool that automates the creation of WAR (Web Application Archive) files for Java web applications. It caters specifically to frontend projects built with React or similar frameworks, making it easier to package and deploy your web applications to a Tomcat server. Key Features Profile-Based Builds : With support for multiple profiles ( dev , test , prod , default ), you can build your application according to different environments and configurations. Version File Generation : Optionally generate a version file that integrates versioning information directly into your TypeScript files, ensuring your build versions are always up-to-date. Tomcat Deployment : Simplify your deploy...

what is LOREM ipsum and why do designers use it

What is Lorem Ipsum? Lorem Ipsum  is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now...

How to Bypass Right Click Block on Any Website

In order to block the right-click activity, most websites make use of JavaScript which is one of the popular scripting languages used to enhance functionality, improve user experience and provide rich interactive features. In addition to this, it can also be used to strengthen the website’s security by adding some of the simple security features such as  disabling right-click ,  protecting images ,  hiding or masking parts of a web page  and so on. How JavaScript Works? Before you proceed to the next part which tells you how to disable the JavaScript functionality and bypass any of the restrictions imposed by it, it would be worthwhile for you to take up a minute to understand how JavaScript works. JavaScript is a client side scripting language (in most cases), which means when loaded it runs from your own web browser. Most modern browsers including IE, Firefox, Chrome and others support JavaScript so that they can interpret the code and carry out action...

Binary Search Tree in C++( dynamic memory based )

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 #include<bits/stdc++.h> using namespace std; struct bst { int val; bst * left, * right; }; bst * root = nullptr; void srch ( int num,bst * head) { if (head == nullptr){ cout << " \n Number is not present \a " << endl; return ; } if (head -> val == num) { cout << " \n Number is present \n\a " ; return ; } else { if (num < head -> val) srch(num,head -> left); else srch(num,head -> right); ...

C++ Program to implement Operator Overloading (>>, <<)

Write a class which contains a string as private data member. Add a member function IsPalindrome that returns TRUE/FALSE depending upon whether the str is palindrome or not. Overload the <> operator to insert the string and print the string respectively along with appropriate message. 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 53 54 55 56 57 58 59 60 61 #include #include #include   enum bool { false , true };   class Cpal{      private :          char *str;      public :          Cpal(){}            bool IsPalindrome();      ...

How to Protect an Email Account from being Hacked

If this is the case, then what is the reason for many people to lose their accounts? The answer is very simple. They don’t know how to protect themselves from being hacked! In fact most of the people who lose their email accounts are not the victims of hacking but the victims of Trapping. They lose their passwords not because they are hacked by some expert hackers but they are fooled to such an extent that they themselves give away their password. Are you confused? If so continue reading and you’ll come to know… Now I’ll mention some of the most commonly used online scams which fool people and make them lose their passwords. I’ll also mention how to protect your email account from these scams. 1 . WEBSITE SPOOFING =Website spoofing is the act of creating a website, with the intention of misleading the readers. The website will be created by a different person or organisation (Other than the original) especially for the purposes of cheating. Normally, the website...