Question

Write a program (in Java) that can convert a sorted array into a balanced binary search...

Write a program (in Java) that can convert a sorted array into a balanced binary search tree. For this project, a balanced binary tree is one where the size of the left and right subtrees at each node differs at most by one. Your program should have a graphical user interface. The program allows the user to enter a number n, generates an array of n random integers, sorts the array, and then converts the sorted array into a balanced binary search tree. The program should display a graphical representation of the binary search tree.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package DS;

import java.awt.event.*;

import java.awt.*;

import java.util.*;

import javax.swing.*;

// Defines a class

class BBSTNode

{

// Declares left and right child

BBSTNode leftChild, rightChild;

int value;

int height;

// Default Constructor

public BBSTNode()

{

leftChild = null;

rightChild = null;

value = 0;

height = 0;

}

  

// Parameterized Constructor

public BBSTNode(int num)

{

leftChild = null;

rightChild = null;

value = num;

height = 0;

}

}

// Defines a class for BalancedBinarySearchTree

class BalancedBinarySearchTree

{

private BBSTNode rootNode;

String result;

  

// Default Constructor

public BalancedBinarySearchTree()

{

rootNode = null;

result = "";

}

// Method to check if tree is empty

public boolean isEmpty()

{

return rootNode == null;

}

// Method to make the tree empty

public void clear()

{

rootNode = null;

}

// Method to insert a node

public void insert(int data)

{

rootNode = insert(data, rootNode);

}

  

// Method to get height of node

private int height(BBSTNode tree)

{

return tree == null ? -1 : tree.height;

}

// Method to return max of left/right node

private int max(int lhs, int rhs)

{

return lhs > rhs ? lhs : rhs;

}

// Method to insert nodes recursively

private BBSTNode insert(int number, BBSTNode treeRoot)

{

if (treeRoot == null)

treeRoot = new BBSTNode(number);

else if (number < treeRoot.value)

{

treeRoot.leftChild = insert(number, treeRoot.leftChild);

if (height(treeRoot.leftChild) - height(treeRoot.rightChild) == 2)

if (number < treeRoot.leftChild.value)

treeRoot = rotateWithLeftChild(treeRoot);

else

treeRoot = doubleWithLeftChild(treeRoot);

}

else if (number > treeRoot.value)

{

treeRoot.rightChild = insert(number, treeRoot.rightChild);

if (height(treeRoot.rightChild) - height(treeRoot.leftChild) == 2)

if (number > treeRoot.rightChild.value)

treeRoot = rotateWithRightChild(treeRoot);

else

treeRoot = doubleWithRightChild(treeRoot);

}

else

; // Duplicate; do nothing

treeRoot.height = max(height(treeRoot.leftChild),

height(treeRoot.rightChild) ) + 1;

return treeRoot;

}

  

// Method to rotate binary tree node with left child

private BBSTNode rotateWithLeftChild(BBSTNode current)

{

BBSTNode temp = current.leftChild;

current.leftChild = temp.rightChild;

temp.rightChild = current;

current.height = max(height(current.leftChild),

height(current.rightChild) ) + 1;

temp.height = max(height(temp.leftChild), current.height ) + 1;

return temp;

}

// Method to rotate binary tree node with right child

private BBSTNode rotateWithRightChild(BBSTNode current)

{

BBSTNode temp = current.rightChild;

current.rightChild = temp.leftChild;

temp.leftChild = current;

current.height = max(height(current.leftChild),

height(current.rightChild) ) + 1;

temp.height = max(height(temp.rightChild), current.height ) + 1;

return temp;

}

/**

* Method to double rotate binary tree node: first left child

* with its right child; then node k3 with new left child

*/

private BBSTNode doubleWithLeftChild(BBSTNode current)

{

current.leftChild = rotateWithRightChild(current.leftChild);

return rotateWithLeftChild(current);

}

/**

* Method to double rotate binary tree node: first right child

* with its left child; then node k1 with new right child */   

private BBSTNode doubleWithRightChild(BBSTNode current)

{

current.rightChild = rotateWithLeftChild(current.rightChild);

return rotateWithRightChild(current);

}   

  

// Method for inorder traversal

public void inorder()

{

inorder(rootNode);

}

private void inorder(BBSTNode rootNode)

{

if (rootNode != null)

{

inorder(rootNode.leftChild);

result += rootNode.value + " ";

inorder(rootNode.rightChild);

}

}

// Method for preorder traversal

public void preorder()

{

preorder(rootNode);

}

private void preorder(BBSTNode rootNode)

{

if (rootNode != null)

{

result += rootNode.value + " ";

preorder(rootNode.leftChild);

preorder(rootNode.rightChild);

}

}

  

// Method for postorder traversal

public void postorder()

{

postorder(rootNode);

}

private void postorder(BBSTNode rootNode)

{

if (rootNode != null)

{

postorder(rootNode.leftChild);

postorder(rootNode.rightChild);

result += rootNode.value + " ";

}

}

// Method for bubble sort

public void sortArray(int array[])

{

// Stores the length of array

int n = array.length;

// Loops till length minus one times

for (int i = 0; i < n - 1; i++)

{

// Loops till length minus i minus one times

for (int j = 0; j < n - i - 1; j++)

{

// Checks if the current position of array value is greater than the next position value then swap

if (array[j] > array[j+1])

{

// Swapping process

int temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;

}// End of if

}// End of inner for loop

}// End of outer for loop

}// End of method

}

public class BalancedBinarySearchTreeGUI implements ActionListener

{

Random rand = new Random();

BalancedBinarySearchTree bbst = new BalancedBinarySearchTree();

JFrame jf;

JTextField numT;

JLabel numL;

JTextArea jta;

JPanel jp1, jp2, mainP;

JButton createB;

BalancedBinarySearchTreeGUI()

{

jf = new JFrame("BalancedBinarySearchTree");

numT = new JTextField(5);

numL = new JLabel("Enter a number: ");

jta = new JTextArea(20, 60);

jp1 = new JPanel();

jp2 = new JPanel();

mainP = new JPanel();

createB = new JButton("Create");

createB.addActionListener(this);

jp1.add(numL);

jp1.add(numT);

jp1.add(jta);

jp2.add(createB);

mainP.add(jp1);

mainP.add(jp2);

mainP.setLayout(new GridLayout(2, 1));

jf.add(mainP);

jf.setVisible(true);

jf.setSize(800, 400);

jf.setLocationRelativeTo(null);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == createB)

{

int n = Integer.parseInt(numT.getText());

int numberArray[] = new int[n];

  

for(int c = 0; c < numberArray.length; c++)

numberArray[c] = rand.nextInt(100) + 1;

bbst.sortArray(numberArray);

  

for(int c = 0; c < numberArray.length; c++)

bbst.insert(numberArray[c]);

  

//System.out.print();

bbst.inorder();

jta.append("\n Inorder Traversal: " + bbst.result);

bbst.result = "";

bbst.preorder();

jta.append("\n Preorder Traversal: " + bbst.result);

bbst.result = "";

bbst.postorder();

jta.append("\n Postorder Traversal: " + bbst.result);

bbst.result = "";

}

}

public static void main(String[] ss)

{

new BalancedBinarySearchTreeGUI();

}

}

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Write a program (in Java) that can convert a sorted array into a balanced binary search...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT