Answers for "binary tree code"

1

binary tree in golang

// Binary Tree in Golang
package main
  
import (
    "fmt"
    "os"
    "io"
)
  
type BinaryNode struct {
    left  *BinaryNode
    right *BinaryNode
    data  int64
}
  
type BinaryTree struct {
    root *BinaryNode
}
  
func (t *BinaryTree) insert(data int64) *BinaryTree {
    if t.root == nil {
        t.root = &BinaryNode{data: data, left: nil, right: nil}
    } else {
        t.root.insert(data)
    }
    return t
}
  
func (n *BinaryNode) insert(data int64) {
    if n == nil {
        return
    } else if data <= n.data {
        if n.left == nil {
            n.left = &BinaryNode{data: data, left: nil, right: nil}
        } else {
            n.left.insert(data)
        }
    } else {
        if n.right == nil {
            n.right = &BinaryNode{data: data, left: nil, right: nil}
        } else {
            n.right.insert(data)
        }
    }   
}
  
func print(w io.Writer, node *BinaryNode, ns int, ch rune) {
    if node == nil {
        return
    }
  
    for i := 0; i < ns; i++ {
        fmt.Fprint(w, " ")
    }
    fmt.Fprintf(w, "%c:%v\n", ch, node.data)
    print(w, node.left, ns+2, 'L')
    print(w, node.right, ns+2, 'R')
}
  
func main() {
    tree := &BinaryTree{}
    tree.insert(100).
        insert(-20).
        insert(-50).
        insert(-15).
        insert(-60).
        insert(50).
        insert(60).
        insert(55).
        insert(85).
        insert(15).
        insert(5).
        insert(-10)
    print(os.Stdout, tree.root, 0, 'M')
}
Posted by: Guest on May-25-2020
3

binary search tree insert java

public static Node insert(Node root, int x){
    if (root == null)
        return new Node(x);
    else if(x>root.getData())
        root.setRightChild(insert(root.getRightChild(),x));
    else
        root.setLeftChild(insert(root.getLeftChild(),x));
    return root;
}
Posted by: Guest on June-08-2020
3

java binary tree

public class BinaryTree {

    private int key;

    private BinaryTree left, right;

    /**
     * Simple constructor.
     *
     * @param key
     *     to set as key.
     */
    public BinaryTree(int key) {
        this.key = key;
    }

    /**
     * Extended constructor.
     *
     * @param key
     *     to set as key.
     * @param left
     *     to set as left child.
     * @param right
     *     to set as right child.
     */
    public BinaryTree(int key, BinaryTree left, BinaryTree right) {
        this.key = key;
        setLeft(left);
        setRight(right);
    }

    public int getKey() {
        return key;
    }

    /**
     * @return the left child.
     */
    public BinaryTree getLeft() {
        return left;
    }

    /**
     * @return the right child.
     */
    public BinaryTree getRight() {
        return right;
    }

    public boolean hasLeft() {
        return left != null;
    }

    public boolean hasRight() {
        return right != null;
    }

    //Return a String representation of the BinaryTree using level order traversal
    public String toString(){
        int h = height(this);
        int i;
        String result = "";
        for (i=1; i<=h; i++) {
            result += printGivenLevel(this, i);
        }
        return result;
    }

    //returns the number of nodes in the BinaryTree
    public int size(){
        return size(this);
    }

    public static int size(BinaryTree tree){
        if(tree == null) return 0;
        return 1 + size(tree.getLeft()) + size(tree.getRight());
    }

    public int height(){ return height(this);}

    public static int height(BinaryTree tree){
        if(tree == null) return 0;
        int left = height(tree.getLeft());
        int right = height(tree.getRight());
        return Math.max(left, right) + 1;
    }

    public String printGivenLevel (BinaryTree root ,int level) {
        if (root == null) return "";
        String result = "";
        if (level == 1) {
            result += root.getKey() + " ";
            return result;
        }else if (level > 1) {
            String left = printGivenLevel(root.left, level-1);
            String right = printGivenLevel(root.right, level-1);
            return left + right;
        }else{
            return "";
        }
    }

    /**
     * @param left
     *     to set
     */
    public void setLeft(BinaryTree left) {
        this.left = left;
    }

    /**
     * @param right
     *     to set
     */
    public void setRight(BinaryTree right) {
        this.right = right;
    }
}
Posted by: Guest on June-16-2020
2

binary tree in ds

# Python program to for tree traversals 

# A class that represents an individual node in a 
# Binary Tree 
class Node: 
	def __init__(self,key): 
		self.left = None
		self.right = None
		self.val = key 


# A function to do inorder tree traversal 
def printInorder(root): 

	if root: 

		# First recur on left child 
		printInorder(root.left) 

		# then print the data of node 
		print(root.val), 

		# now recur on right child 
		printInorder(root.right) 



# A function to do postorder tree traversal 
def printPostorder(root): 

	if root: 

		# First recur on left child 
		printPostorder(root.left) 

		# the recur on right child 
		printPostorder(root.right) 

		# now print the data of node 
		print(root.val), 


# A function to do preorder tree traversal 
def printPreorder(root): 

	if root: 

		# First print the data of node 
		print(root.val), 

		# Then recur on left child 
		printPreorder(root.left) 

		# Finally recur on right child 
		printPreorder(root.right) 


# Driver code 
root = Node(1) 
root.left	 = Node(2) 
root.right	 = Node(3) 
root.left.left = Node(4) 
root.left.right = Node(5) 
print "Preorder traversal of binary tree is"
printPreorder(root) 

print "\nInorder traversal of binary tree is"
printInorder(root) 

print "\nPostorder traversal of binary tree is"
printPostorder(root)
Posted by: Guest on March-13-2021
1

binary tree

class Node:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.val = key
 
root = Node(1)
root.left      = Node(2);
root.right     = Node(3);
root.left.left  = Node(4);
Posted by: Guest on March-16-2021
0

binary tree in c implementation

11 void insert(node ** tree, int val) {
12 node *temp = NULL;
13 if(!(*tree)) {
14   temp = (node *)malloc(sizeof(node));
15   temp->left = temp->right = NULL;
16   temp->data = val;
17   *tree = temp;
18   return;
19 }
20
21 if(val < (*tree)->data) {
22      insert(&(*tree)->left, val);
23   } else if(val > (*tree)->data) {
24     insert(&(*tree)->right, val);
25   }
26 }
Posted by: Guest on August-30-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language