Question

Java please Given the following numbers in the given order, show the AVL tree. Show the...

Java please

Given the following numbers in the given order, show the AVL tree. Show the steps as you do any rotations.

            100, 200, 150, 170, 165, 180, 220, 163, 164

Write the AVL tree code and insert the above numbers. Show the screen shot of the pre-order traversal of the resulting tree.

Compare the result with the Red-black tree result

I have the code for the RBT, please compare the results from these two trees

import java.util.Scanner;

/* Class Node */

class RedBlackNode

{

RedBlackNode left, right;

int element;

int color;

/* Constructor */

public RedBlackNode(int theElement)

{

this( theElement, null, null );

}

/* Constructor */

public RedBlackNode(int theElement, RedBlackNode lt, RedBlackNode rt)

{

left = lt;

right = rt;

element = theElement;

color = 1;

}

}

/* Class RBTree */

class RBTree

{

private RedBlackNode current;

private RedBlackNode parent;

private RedBlackNode grand;

private RedBlackNode great;

private RedBlackNode header;

private static RedBlackNode nullNode;

/* static initializer for nullNode */

static

{

nullNode = new RedBlackNode(0);

nullNode.left = nullNode;

nullNode.right = nullNode;

}

/* Black - 1 RED - 0 */

static final int BLACK = 1;

static final int RED = 0;

/* Constructor */

public RBTree(int negInf)

{

header = new RedBlackNode(negInf);

header.left = nullNode;

header.right = nullNode;

}

/* Function to check if tree is empty */

public boolean isEmpty()

{

return header.right == nullNode;

}

/* Make the tree logically empty */

public void makeEmpty()

{

header.right = nullNode;

}

/* Function to insert item */

public void insert(int item )

{

current = parent = grand = header;

nullNode.element = item;

while (current.element != item)

{

great = grand;

grand = parent;

parent = current;

current = item < current.element ? current.left : current.right;

// Check if two red children and fix if so

if (current.left.color == RED && current.right.color == RED)

handleReorient( item );

}

// Insertion fails if already present

if (current != nullNode)

return;

current = new RedBlackNode(item, nullNode, nullNode);

// Attach to parent

if (item < parent.element)

parent.left = current;

else

parent.right = current;

handleReorient( item );

}

private void handleReorient(int item)

{

// Do the color flip

current.color = RED;

current.left.color = BLACK;

current.right.color = BLACK;

if (parent.color == RED)

{

// Have to rotate

grand.color = RED;

if (item < grand.element != item < parent.element)

parent = rotate( item, grand ); // Start dbl rotate

current = rotate(item, great );

current.color = BLACK;

}

// Make root black

header.right.color = BLACK;

}

private RedBlackNode rotate(int item, RedBlackNode parent)

{

if(item < parent.element)

return parent.left = item < parent.left.element ? rotateWithLeftChild(parent.left) : rotateWithRightChild(parent.left) ;

else

return parent.right = item < parent.right.element ? rotateWithLeftChild(parent.right) : rotateWithRightChild(parent.right);

}

/* Rotate binary tree node with left child */

private RedBlackNode rotateWithLeftChild(RedBlackNode k2)

{

RedBlackNode k1 = k2.left;

k2.left = k1.right;

k1.right = k2;

return k1;

}

/* Rotate binary tree node with right child */

private RedBlackNode rotateWithRightChild(RedBlackNode k1)

{

RedBlackNode k2 = k1.right;

k1.right = k2.left;

k2.left = k1;

return k2;

}

/* Function for preorder traversal */

public void preorder()

{

preorder(header.right);

}

private void preorder(RedBlackNode r)

{

if (r != nullNode)

{

char c = '*';

if (r.color == 0)

c = ' ';

System.out.print(r.element +""+c+" ");

preorder(r.left);

preorder(r.right);

}

}

}

/* Class Main */

public class RBT

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

/* Creating object of RedBlack Tree */

RBTree rbt = new RBTree(Integer.MIN_VALUE);

System.out.println("Red Black Tree Test\n");

char ch;

do

{

System.out.println("Enter integer element to insert:");

rbt.insert(scan.nextInt());

System.out.println("Do you want to continue (Type y or n)");

ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');

System.out.print("\nPre order : ");

rbt.preorder();

scan.close();

}

}

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

The AVL tree insertion code is given below

OUTPUT is shown below

RBL Tree code is compiled, run and compared. The output is given below:

RBL does faster insertion than AVL trees due to fewer rotations but AVL trees provide faster lookups than Red-Black Trees because they are more strictly balanced. AVL trees store balance factors with each node, thus requires storage for an integer per node whereas Red Black Tree requires only 1 bit of information per node.

AVL tree formation:

Add a comment
Know the answer?
Add Answer to:
Java please Given the following numbers in the given order, show the AVL tree. Show the...
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
  • please make a pretty JAVA GUI for this code this is RED BLACK TREE and i...

    please make a pretty JAVA GUI for this code this is RED BLACK TREE and i Finished code already jus need a JAVA GUI for this code ... if poosible make it pretty to look thanks and please make own GUI code base on my code thanks ex: (GUI only have to show RBTree) ---------------------------------------- RBTree.java import java.util.Stack; public class RBTree{    private Node current;    private Node parent;    private Node grandparent;    private Node header;    private Node...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

  • Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from...

    Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that...

  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • Test Data would be as follows: Using java language. Build an expression tree. • When you...

    Test Data would be as follows: Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

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