0% found this document useful (0 votes)
35 views

Demonstration of Binary Search Tree

This document defines a binary search tree (BST) data structure with methods for inserting, searching, deleting, and traversing nodes. It includes a TLNode class to represent tree nodes with data, left, and right child pointers. The BST class contains a root node and methods like insert() to add nodes, search() to find a node by its data, delete() to remove nodes, and inorder() to traverse the tree. The main() method demonstrates inserting several nodes into an empty tree, deleting a node, searching for a node, and printing the in-order traversal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Demonstration of Binary Search Tree

This document defines a binary search tree (BST) data structure with methods for inserting, searching, deleting, and traversing nodes. It includes a TLNode class to represent tree nodes with data, left, and right child pointers. The BST class contains a root node and methods like insert() to add nodes, search() to find a node by its data, delete() to remove nodes, and inorder() to traverse the tree. The main() method demonstrates inserting several nodes into an empty tree, deleting a node, searching for a node, and printing the in-order traversal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class TLNode{

int data;
TLNode left,right;

TLNode(int d)
{
data=d;
}
}

public class BST{

TLNode root;

TLNode insert(int d,TLNode root)


{
if(root==null)
root=new TLNode(d);

else if(d<=root.data)
root.left=insert(d,root.left);

else
root.right=insert(d,root.right);

return root;
}

TLNode search(int d,TLNode root)


{
if(root.data==d)
return root;
else if(d<root.data)
return search(d,root.left);
else
return search(d,root.right);
}

void inorder(TLNode r)
{
if(r==null)
return;

inorder(r.left);
System.out.println(r.data);
inorder(r.right);
}

TLNode delete(TLNode root, int data)


{

if (root == null) return root;

if (data < root.data)


root.left = delete(root.left, data);
else if (data > root.data)
root.right = delete(root.right, data);

else
{
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;

root.data = minValue(root.right);
root.right = delete(root.right, root.data);
}

return root;
}
int minValue(TLNode root)
{
int minv = root.data;
while (root.left != null)
{
minv = root.left.data;
root = root.left;
}
return minv;
}

public static void main(String[] args)


{
BST ob=new BST();
ob.root=ob.insert(50,ob.root);
ob.root=ob.insert(30,ob.root);
ob.root=ob.insert(20,ob.root);
ob.root=ob.insert(20,ob.root);
ob.root=ob.insert(70,ob.root);
ob.root=ob.insert(60,ob.root);
ob.root=ob.insert(80,ob.root);
ob.root=ob.delete(ob.root,50);
System.out.println("******" +ob.root.data);
ob.inorder(ob.root);

TLNode find=ob.search(30,ob.root);
if(find==null)
System.out.println("not found");
else
System.out.println("found : "+find.data);
}
}

Output:

******60
20
20
30
60
70
80
found : 30

You might also like