Question
help please! due tomorrow!

Write code in Java

Write a program that takes as input an unordered list of integers, creates a Btree of minimum degree t 4 and then outputs the sorted list of integers. A simple inorder traversal of the B tree will output the list of the integers in a increasing order. You can choose your programming language and the platform you run on. The documentation is required for any programming assignment.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As per your requirement the below one is solution please follow it step by step

class BTreeNode
{
   BTreeNode left, right;
int data;

public BTreeNode(int n)
{
left = null;
right = null;
data = n;
}

public void setLeft(BTreeNode n)
{
left = n;
}

public void setRight(BTreeNode n)
{
right = n;
}

public BTreeNode getLeft()
{
return left;
}
  
public BTreeNode getRight()
{
return right;
}
  
public void setData(int d)
{
data = d;
}

public int getData()
{
return data;
}   
}

class BinartSearchTree
{
private BTreeNode root;


public BinartSearchTree()
{
root = null;
}
  
public boolean isEmpty()
{
return root == null;
}

public void insert(int data)
{
root = insert(root, data);
}

private BTreeNode insert(BTreeNode root, int key) {
if (root == null) {
root = new BTreeNode(key);
return root;
}

if (key < root.data)
root.left = insert(root.left, key);
else if (key > root.data)
root.right = insert(root.right, key);
  
return root;
}   
  
public void inorder()
{
inorder(root);
}

private void inorder(BTreeNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
}

public class BinaryTree
{
public static void main(String[] args)
{
   BinartSearchTree bt = new BinartSearchTree();
int arr[] = {100,98,107,43,65,7876};
  
for(int i=0;i<arr.length;i++)
   bt.insert(arr[i]);   
System.out.print("\nIn order : ");
  
bt.inorder();
}
}

Add a comment
Know the answer?
Add Answer to:
help please! due tomorrow! Write code in Java Write a program that takes as input an...
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
ADVERTISEMENT