Created
May 2, 2021 08:48
-
-
Save syedjafer/867d656cde3e1a62adede9e067607e26 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Node{ | |
| int data; | |
| Node left; | |
| Node right; | |
| Node(int dat) | |
| { | |
| data = dat; | |
| left = right = null; | |
| } | |
| } | |
| public class HelloWorld{ | |
| static void insert(Node node, int data){ | |
| if (data < node.data){ | |
| if(node.left==null){ | |
| // System.out.println(data); | |
| node.left = new Node(data); | |
| } | |
| else{ | |
| insert(node.left, data); | |
| } | |
| } | |
| else if(data > node.data){ | |
| if(node.right==null){ | |
| // System.out.println(data); | |
| node.right = new Node(data); | |
| } | |
| else{ | |
| insert(node.right, data); | |
| } | |
| } | |
| } | |
| static void print_tree(Node node){ | |
| if (node.left!=null){ | |
| print_tree(node.left); | |
| } | |
| System.out.println(node.data +" ,"); | |
| if (node.right!=null){ | |
| print_tree(node.right); | |
| } | |
| } | |
| public static void main(String []args){ | |
| Node root = new Node(27); | |
| // System.out.println(root.data); | |
| insert(root, 14); | |
| insert(root, 35); | |
| insert(root, 31); | |
| insert(root, 10); | |
| insert(root, 19); | |
| print_tree(root); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment