java binary tree

Solutions on MaxInterview for java binary tree by the best coders in the world

showing results for - "java binary tree"
Buster
12 Sep 2017
1package MyPackage;
2  
3public class Tree { 
4    static class Node {    
5    int value; 
6        Node left, right; 
7          
8        Node(int value){ 
9            this.value = value; 
10            left = null; 
11            right = null; 
12        } 
13    } 
14       
15    public void insert(Node node, int value) {
16        if (value < node.value) { 
17          	if (node.left != null) { 
18              	insert(node.left, value); 
19            } else { 
20              System.out.println(" Inserted " + value + " to left of " + node.value); 
21              node.left = new Node(value); 
22            } 
23        } else if (value > node.value) {
24          if (node.right != null) {
25            insert(node.right, value);
26          } else {
27            System.out.println("  Inserted " + value + " to right of "
28                + node.value);
29            node.right = new Node(value);
30          }
31        }
32      }
33     public void traverseInOrder(Node node) {
34        if (node != null) {
35            traverseInOrder(node.left);
36            System.out.print(" " + node.value);
37            traverseInOrder(node.right);
38        }
39     }
40     
41     public static void main(String args[]) 
42    { 
43    Tree tree = new Tree();
44                Node root = new Node(5);
45                System.out.println("Binary Tree Example");
46                System.out.println("Building tree with root value " + root.value);
47                tree.insert(root, 2);
48                tree.insert(root, 4);
49                tree.insert(root, 8);
50                tree.insert(root, 6);
51                tree.insert(root, 7);
52                tree.insert(root, 3);
53                tree.insert(root, 9);
54                System.out.println("Traversing tree in order");
55                tree.traverseLevelOrder();
56                
57              }
58}
59  
60
Denis
24 May 2018
1public class BinaryTree {
2
3    private int key;
4
5    private BinaryTree left, right;
6
7    /**
8     * Simple constructor.
9     *
10     * @param key
11     *     to set as key.
12     */
13    public BinaryTree(int key) {
14        this.key = key;
15    }
16
17    /**
18     * Extended constructor.
19     *
20     * @param key
21     *     to set as key.
22     * @param left
23     *     to set as left child.
24     * @param right
25     *     to set as right child.
26     */
27    public BinaryTree(int key, BinaryTree left, BinaryTree right) {
28        this.key = key;
29        setLeft(left);
30        setRight(right);
31    }
32
33    public int getKey() {
34        return key;
35    }
36
37    /**
38     * @return the left child.
39     */
40    public BinaryTree getLeft() {
41        return left;
42    }
43
44    /**
45     * @return the right child.
46     */
47    public BinaryTree getRight() {
48        return right;
49    }
50
51    public boolean hasLeft() {
52        return left != null;
53    }
54
55    public boolean hasRight() {
56        return right != null;
57    }
58
59    //Return a String representation of the BinaryTree using level order traversal
60    public String toString(){
61        int h = height(this);
62        int i;
63        String result = "";
64        for (i=1; i<=h; i++) {
65            result += printGivenLevel(this, i);
66        }
67        return result;
68    }
69
70    //returns the number of nodes in the BinaryTree
71    public int size(){
72        return size(this);
73    }
74
75    public static int size(BinaryTree tree){
76        if(tree == null) return 0;
77        return 1 + size(tree.getLeft()) + size(tree.getRight());
78    }
79
80    public int height(){ return height(this);}
81
82    public static int height(BinaryTree tree){
83        if(tree == null) return 0;
84        int left = height(tree.getLeft());
85        int right = height(tree.getRight());
86        return Math.max(left, right) + 1;
87    }
88
89    public String printGivenLevel (BinaryTree root ,int level) {
90        if (root == null) return "";
91        String result = "";
92        if (level == 1) {
93            result += root.getKey() + " ";
94            return result;
95        }else if (level > 1) {
96            String left = printGivenLevel(root.left, level-1);
97            String right = printGivenLevel(root.right, level-1);
98            return left + right;
99        }else{
100            return "";
101        }
102    }
103
104    /**
105     * @param left
106     *     to set
107     */
108    public void setLeft(BinaryTree left) {
109        this.left = left;
110    }
111
112    /**
113     * @param right
114     *     to set
115     */
116    public void setRight(BinaryTree right) {
117        this.right = right;
118    }
119}
queries leading to this page
how to create a tree in javahow to create a tree java binary tree javatree data structure in javaevery king of tree in javabst implementation using javabinary tree depth browse javabinary tree code javawhat is binary tree in javacode of binary tree in javabinary tree java implementationimplement a bst in javatree data structure example in javadisplay a binary tree in javabinary tree implementation in java using arraystrees java generating otherstrees ds in javajava binary search tree node classbinary tree in cbinary tree adt javacomplete binary tree implementation in javanode left javatree algorithm javabinary tree insertion and size in one class javahow to make a binary tree in javajava data structures treegeeks for geeks cpp treeimplement tree data structure in javabinary tree node javabinary tree javatree node in java programsnew tree algorithmimplementing binary search tree in javajava implement treebinary tree java baeldungjava balanced binary tree implementationjava treesimplementation of binary tree javacreating new nodes java binary treejava binary tree implementationtree basic level in javatree datastructers in c 2b 2badd node to tree javatree nodes javadata structures and algorithms in java treeshow to make a data tree in javahow to a logic read binary treesjava spread resource between nodes of treeimplement binary tree in javatree codebinry tree javacreate binary tree in javatrees in data structure 2b 22student 22treem implelemntaion in javatree in javabinary tree java code examplejava implementation of binary search treehow to create binary tree in javajava create node treemake a binary treemaking a tree datastructure in c 2b 2bbinary tree c 2b 2bbinary search tree implementation in javabinary tree functions javatree implementaion in javahow to use binarytrees in javatree operations javahow does a java code computation tree workbuild tree javaimplement a binary tree in javabinary tree in java exampleadd node binary tree javatree in java codebinary tree in java implementationtree tutorialare there binary trees in javaajava tree constructorbinary tree projectbinary search tree javatree using javatree structure javacreate a binary tree in javabinary treejavatree implementation javaadd collection type to a binary tree javabinart tree in javamake binary tree javarecursion tree library in javaimplementing a binary tree in javatree java data structurejava binarytreetree datastructure in javajava perfect binary string treejava binary treejava 8 bst librarytree in java javabinary tree in data structure in javawhat is a binary tree in javabinary trees codebinary serch tree javatree data structure tutorial in javabinary tree codehow implement bianry treejava binarytree democode to implement binary tree in javabinary tree ctutotial for java treejava binary tree algorithms examplesbinry tree in jvahow is binary tree implementedimplementing binary tree using javabinary tree class in javabinary tree program and a general tree program javabinary tree java classtree implemeentation pythonbinary tree array implementation javacreating a binary tree in javajava create binary treehow to create binary tree with n treeshow to implement tree data structure in javajava initialize treetree in dsa code in javajava binary tree structurebest way to use a tree in javabinary tree tutorial javahow to solve tree based problems using collections in javaupdate binary tree class javaimplement a tree in javabinary tree in java programsbinary search tree methods javacreate binary treebinary tree bulit in javahow to use binary trees in javatrees data structures in javais there a binary tree class in java 3fjava treejava binary tree guerracreate a binary tree using javabinary search tree java implementationinary tree operations in javacoding a binary treejava binary treessimple binary tree implementationbinary tree functions in javaimport binary tree javabinary tree in java using tree nodetree data structure in java tutorialbst implementation javabinary tree creation javatree in c programming geeksforgeeksbinary tree class javatree implementation in javanode root javatree structures javausing binary tree in java8 appbinary tree propertiesbinnary tree javahow to read a binary tree in pythonbinary tree code in javajava binary trees codecode to birnary tree javajava class binary treebinary tree javajava what is a binary treetree in data structure javabuild binary tree javatree data structure javajava binarytree formathow to create a binary tree in javabinary tree example javabynary tree with java classestree data structure c 2b 2bjava binarytree classinitalizing a binary tree javabinary tree 3cstring 3e tree javadata structure for tree javatree methods javafunction to create binary tree javarepresent binary tree in javacreate a binary tree javajava class bst implementationjava bst data structuresimple binary tree implementation in javatree jvaimplementing a binary tree javatree ds in javabinary tree classimplement binary treetree structure algorithm java code add itemhow to drae graph binary tree using javabinary trees javatree data structure in java 8trees in javatree of a program with node as each statementhow to make a tree into linear tree in javabinary tree operations in javajava data treebinary tree javabinary tree ajvawhich data structure is used in java to implement treetresss in javajava binary tree example code tree in javawirtinf tree nodes in javabinary tree implementation javadoes java come with implementation for treesconstruct binary tree javatrees javareference based binary tree javawhat kind of situations do we need to use binary trees in javajava easiest tree to implementationbinary search tree recursive implementation in javanode tree in javatrees java tutorialdata tree javacreating treebinary trees in chow to make a binary tree javabinary tree java source codeimplementing binary treejava node binary treebinary tree in java guicoding a binary tree javahow to work with trees in javajava create treejava 8 binary treetree ds javaimplementing binary tree in javajava create tree data structuretree concept in javajava create a tree data structurejava public class binary treehow to implement a binary tree in javanode trees javausing trees in javabinary tree introductionnode java bstis there any tree data structure in javajava binary tree codedata structures that can implement tree javabst java implementationimplementation of binary tree in javabinary tree create node javabinary tree data structure in java examplebinary tree of object javatree data structure implementation in javanew tree in javacan we implement using binary treetree programbinary tree array javajava two way treehow to implement binary tree in javabinary tree in javajava tree data structurepage to write a binary treenet beans bianry tree packagebinary tree example in javahow to initialize bst javatree data structure in c 2b 2bjava binary tree importjava binary tree classbinary tree class implementationstructure of tree in c 2b 2btree structure in javaconstruct a binary tree classtree and nodes javahow trees are represented in codebinarytree in java binary tree implementation in javaproperties of binary tree in data structuretree in dsa javajava balanced binary treemake a binary tree in javahow to create a binary tree in data structurejava binary tree implementation fight systembinarytree javabinary tree java codebinary tree java program examplesbinary trees in javabinary tree using an array javatree javatrees cpp gfgwhats a tree c 2b 2bhow to implement a binary tree using nodes in javabrinary tree in javahow to complete a tree in javawe can represent binary tree using 3fbinary tree data structure in javatrees in java data structurejava tree datastructurebinary tree object javacreating a binary tree javajava binary tree