Insertion in a Binary Search Tree :_
Insertion in a BST follow simple operations. All the elements less than root lies on left subtree and all the elements greater than root lies on right subtree.
Every BST class will have a root variable of Node type that represents the head node of the tree.
Insertion in a BST follow simple operations. All the elements less than root lies on left subtree and all the elements greater than root lies on right subtree.
Every BST class will have a root variable of Node type that represents the head node of the tree.
public class BinarySearchTree { Node root; public BinarySearchTree() { root=null; }
public Node insert(Node node,Node root) { if(root==null) { root=node; return root; } else{ if(node.data>root.data) { root.right=insert(node,root.right); } else if(node.data<root.data) { root.left=insert(node,root.left); } } return root; } }
Comments
Post a Comment