Question

In: Computer Science

Given the following numbers in the given order, show the red black tree              100, 200,...

Given the following numbers in the given order, show the red black tree

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

Show the pre-order traversal of this red black tree while showing the color of each node in the pre-order traversal.

Write (C++) the red black tree code and insert the above numbers. Show the screen shot of the pre-order traversal of the resulting tree. Distinguish the colors by writing a * next to the black color values. Compare the result with the previous question.

Solutions

Expert Solution

#include <iostream>
using namespace std;

struct Node {
  int data;
  Node *parent;
  Node *left;
  Node *right;
  int color;
};

typedef Node *NodePtr;

class RedBlackTree {
   private:
  NodePtr root;
  NodePtr TNULL;

  void initializeNULLNode(NodePtr node, NodePtr parent) {
    node->data = 0;
    node->parent = parent;
    node->left = nullptr;
    node->right = nullptr;
    node->color = 0;
  }

  // Preorder
  void preOrderHelper(NodePtr node) {
    if (node != TNULL) {
                string sColor = node->color ? "" : "*";
      cout << node->data << sColor<<endl;
      preOrderHelper(node->left);
      preOrderHelper(node->right);
    }
  }

  // For balancing the tree after insertion
  void insertFix(NodePtr k) {
    NodePtr u;
    while (k->parent->color == 1) {
      if (k->parent == k->parent->parent->right) {
        u = k->parent->parent->left;
        if (u->color == 1) {
          u->color = 0;
          k->parent->color = 0;
          k->parent->parent->color = 1;
          k = k->parent->parent;
        } else {
          if (k == k->parent->left) {
            k = k->parent;
            rightRotate(k);
          }
          k->parent->color = 0;
          k->parent->parent->color = 1;
          leftRotate(k->parent->parent);
        }
      } else {
        u = k->parent->parent->right;

        if (u->color == 1) {
          u->color = 0;
          k->parent->color = 0;
          k->parent->parent->color = 1;
          k = k->parent->parent;
        } else {
          if (k == k->parent->right) {
            k = k->parent;
            leftRotate(k);
          }
          k->parent->color = 0;
          k->parent->parent->color = 1;
          rightRotate(k->parent->parent);
        }
      }
      if (k == root) {
        break;
      }
    }
    root->color = 0;
  }


   public:
  RedBlackTree() {
    TNULL = new Node;
    TNULL->color = 0;
    TNULL->left = nullptr;
    TNULL->right = nullptr;
    root = TNULL;
  }

  void preorder() {
    preOrderHelper(this->root);
  }

  NodePtr minimum(NodePtr node) {
    while (node->left != TNULL) {
      node = node->left;
    }
    return node;
  }

  NodePtr maximum(NodePtr node) {
    while (node->right != TNULL) {
      node = node->right;
    }
    return node;
  }

  NodePtr successor(NodePtr x) {
    if (x->right != TNULL) {
      return minimum(x->right);
    }

    NodePtr y = x->parent;
    while (y != TNULL && x == y->right) {
      x = y;
      y = y->parent;
    }
    return y;
  }

  NodePtr predecessor(NodePtr x) {
    if (x->left != TNULL) {
      return maximum(x->left);
    }

    NodePtr y = x->parent;
    while (y != TNULL && x == y->left) {
      x = y;
      y = y->parent;
    }

    return y;
  }

  void leftRotate(NodePtr x) {
    NodePtr y = x->right;
    x->right = y->left;
    if (y->left != TNULL) {
      y->left->parent = x;
    }
    y->parent = x->parent;
    if (x->parent == nullptr) {
      this->root = y;
    } else if (x == x->parent->left) {
      x->parent->left = y;
    } else {
      x->parent->right = y;
    }
    y->left = x;
    x->parent = y;
  }

  void rightRotate(NodePtr x) {
    NodePtr y = x->left;
    x->left = y->right;
    if (y->right != TNULL) {
      y->right->parent = x;
    }
    y->parent = x->parent;
    if (x->parent == nullptr) {
      this->root = y;
    } else if (x == x->parent->right) {
      x->parent->right = y;
    } else {
      x->parent->left = y;
    }
    y->right = x;
    x->parent = y;
  }

  // Inserting a node
  void insert(int key) {
    NodePtr node = new Node;
    node->parent = nullptr;
    node->data = key;
    node->left = TNULL;
    node->right = TNULL;
    node->color = 1;

    NodePtr y = nullptr;
    NodePtr x = this->root;

    while (x != TNULL) {
      y = x;
      if (node->data < x->data) {
        x = x->left;
      } else {
        x = x->right;
      }
    }

    node->parent = y;
    if (y == nullptr) {
      root = node;
    } else if (node->data < y->data) {
      y->left = node;
    } else {
      y->right = node;
    }

    if (node->parent == nullptr) {
      node->color = 0;
      return;
    }

    if (node->parent->parent == nullptr) {
      return;
    }

    insertFix(node);
  }

};

int main() {
  RedBlackTree bst;
  bst.insert(100);
  bst.insert(200);
  bst.insert(150);
  bst.insert(170);
  bst.insert(165);
  bst.insert(180);
  bst.insert(220);
  bst.insert(163);
  bst.insert(164);

  bst.preorder();
 
}


Related Solutions

Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
1. Show the red-black tree that result after successively inserting the keys 1, 2, 5, 15,...
1. Show the red-black tree that result after successively inserting the keys 1, 2, 5, 15, 4, 7, 8, 14, 11 into an initially empty red-black tree. Show each step whenever you change a node’s color or make a rotation, mark your operations clearly.
What is the minimum number of nodes in a red-black tree of height 8?
What is the minimum number of nodes in a red-black tree of height 8?
Trace the following code segment 1. use the red numbers under each statement to show the...
Trace the following code segment 1. use the red numbers under each statement to show the order that you execute the code by. For example: (1) means int p=3; (2) means p<10; (3) means p+=3; (1)(2)(3) means you execute int p=3 then p<10 then P+=3; (2)(1)(3) means you execute p<10 then int p =3 then P+=3; 2. show its output for (int p = 3 ; p <10; p += 3 )    (1) (2) (3)          {    for...
Trace the following code segment 1. use the red numbers under each statement to show the...
Trace the following code segment 1. use the red numbers under each statement to show the order that you execute the code by. For example: (1) means int p=3; (2) means p<10; (3) means p+=3; (1)(2)(3) means you execute int p=3 then p<10 then P+=3; (2)(1)(3) means you execute p<10 then int p =3 then P+=3; 2. show its output: int x = 30,  num = 100;     (1)    while ( x > 0) (2)           { cout   <<   endl                          <<  “ x...
Suppose the following disk request sequence (track numbers) for a disk with 200 tracks is given:...
Suppose the following disk request sequence (track numbers) for a disk with 200 tracks is given: 23, 89, 132, 42, 97, 35, 187 Assume that the initial position of the R/W head is on track 100. Draw the diagrams for arm movement for the seek strategies given below. Calculate the total and average number of tracks travelled and conclude which strategy performed best in the given scenario. Note: You can use pencil and paper method to draw, but you have...
Suppose the following disk request sequence (track numbers) for a disk with 200 tracks is given:...
Suppose the following disk request sequence (track numbers) for a disk with 200 tracks is given: 23, 89, 132, 42, 97, 35, 187 Assume that the initial position of the R/W head is on track 100. Draw the diagrams for arm movement for the seek strategies given below. Calculate the total and average number of tracks travelled and conclude which strategy performed best in the given scenario. Note: You can use pencil and paper method to draw, but you have...
In a box, there are 400 blue marbles and 100 red marbles. When 200 marbles are...
In a box, there are 400 blue marbles and 100 red marbles. When 200 marbles are randomly chosen from the box, let the random variable X represent the number of chosen blue marbles. (a) Find P(X ≥ 102). [You may leave the answer in terms of “combinations” or “factorials”.] (b) Find P(X = 160) using binomial approximation
In C++ with lots of comments please Complete a binary search tree of 20 numbers Show...
In C++ with lots of comments please Complete a binary search tree of 20 numbers Show in the output the steps while it's performing the search with 20 numbers in a text file called list.txt The numbers will be imported to the program Simple program that should let you have the option to search for numbers, remove numbers, print numbers, and insert numbers in the binary tree If the number isn't there then give an error
Given the following set of keys: {1, 2, 3} determine the number of distinct left-leaning red-black...
Given the following set of keys: {1, 2, 3} determine the number of distinct left-leaning red-black trees that can be constructed with those keys. Draw the tree for each possible key-insertion order, showing the transformations involved at each step.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT