This document provides a step-by-step guide on how to implement a Binary Search Tree in Java. It includes the code for creating a BST, methods for inserting and deleting nodes, and displaying the tree. The document also includes outputs of the program.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Running Head: IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA Name of the Student: Name of the University: Author Note:
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
1IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA Table of Contents The java code of BST.java...............................................................................................................2 Outputs of the program..................................................................................................................10
2IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA The java code of BST.java publicclassBSTimplementsTree { //================ // variables //================ // Tree Root privateBSTNode root; //================ // Constructors //================ // Create an empty Binary search Tree with a Root set to null publicBST() { root =null; } //================ // Methods
3IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA //================ // Print the Binary search Tree publicvoiddisplay() { System.out.print("( "); traverse(root); System.out.println(" )"); } // A recursive function that traverses and prints out // the binary search tree with aninordertraversal. privatevoidtraverse(BSTNode node) { if(node ==null) return; traverse(node.left); System.out.print(node.val +" "); traverse(node.right);
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
4IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA } //================ // Implement These //================ // This method takes an integer parameter n and inserts it // as a node in a binary search tree. publicvoidinsert(intn) { //TODO: Implement the insert function of // the Binary Search Tree root = insert(root, n); return; } privateBSTNode insert(BSTNode node,intval) { if(node ==null) {
5IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA node =newBSTNode(val); } else { if(val <= node.val) node.left = insert(node.left, val); else node.right = insert(node.right, val); } returnnode; } // This method searches the binary tree for an integer n // and if it exists deletes this node from the tree. publicvoiddelete(intn) { //TODO: Implement the delete function of // the Binary search Tree if( root ==null){
6IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA System.out.println("Tree is Empty.\n"); } else{ root = delete(root, n); } return; } //implementing the recursive delete function privateBSTNode delete(BSTNode node,intval){ if(node ==null){ returnnode; //if the tree is empty } // search the tree for the value if(val < node.val) node.left = delete(node.left, val); elseif(val> node.val) node.right = delete(node.right, val);
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
7IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA // if the value is same as root value then this node should be deleted else { // if the tree contains only one child or no child if(node.right ==null) returnnode.left; elseif(node.left ==null) returnnode.right; // if the node has two children then get the inorder successor node.val = minValue(node.right); // Deleting the inorder successor node.right = delete(node.right, node.val); } returnnode; } intminValue(BSTNode node){
8IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA intmvalue = node.val; while(node.left !=null) { mvalue = node.left.val; node = node.left; } returnmvalue; } // This function returns true if no node in the binary tree // has more than child, otherwise it returns false. publicbooleanonechildMax(intt1[],intsize) { //TODO: Implement the onechildMax function of // the Binary search Tree intnextSuccessor, lastSuccessor;
9IMPLEMENTATION OF BINARY SEARCH TREE IN JAVA for(inti = 0;i < size - 1;i++){ nextSuccessor = t1[i] - t1[i+1]; lastSuccessor = t1[i] - t1[size - 1]; if(nextSuccessor * lastSuccessor < 0){ returnfalse; }; } returntrue; } } //End of class BST
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser