Give an algorithm for inserting an element into binary tree in recersive way

package in.blogspot.arunj2ee.ds.tree.binary.que;

import in.blogspot.arunj2ee.ds.tree.BinaryTreeNode;
import in.blogspot.arunj2ee.ds.tree.util.BTreePrinter;
import in.blogspot.arunj2ee.ds.tree.util.TreeUtil;

/*
 * Que: Give an algorithm for inserting an element into binary tree in recersive way
 * Time complexity: O(n), Space Complexity: O(n)
 */
public class Que5b {
public static void insert(BinaryTreeNode root, int data){
if(root == null)
root = new BinaryTreeNode(data);

if(root.getLeft() == null){
root.setLeft(new BinaryTreeNode(data));
return;
}else {
insert(root.getRight(), data);
}
}

public static void main(String[] args) {
BinaryTreeNode rootNode = TreeUtil.createRandomBinaryTree();
BTreePrinter.printBinaryTreeNode(rootNode);
insert(rootNode, 500);
BTreePrinter.printBinaryTreeNode(rootNode);
}
}
========================================================================
Refer Core Classes: http://arunj2ee.blogspot.in/2017/05/tree-core-and-utility-classes.html
========================================================================
Share on Google Plus

About Admin

Arun is a JAVA/J2EE developer and passionate about coding and managing technical team.
    Blogger Comment
    Facebook Comment

1 comments:

  1. I am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.

    Top Software Testing Companies
    Top Security Testing Companies
    Top Mobile Testing Companies
    Top Test Automation Companies
    Top Performance Testing Companies
    Website testing services

    ReplyDelete