Question

In: Computer Science

Show that if all nodes in a splay tree are accessed in sequential order, then the...

Show that if all nodes in a splay tree are accessed in sequential order, then the
total access time is O(N), regardless of the initial tree.

Solutions

Expert Solution

Answer: The worst case time complexity of Binary Search Tree (BST) operations like search, delete, insert is O(n). The worst case occurs when the tree is skewed. We can get the worst case time complexity as O(Logn) with AVL and Red-Black Trees. All splay tree operations run in O(log n) time on average, where n is the number of entries in the tree. Any single operation can take Theta(n) time in the worst case.

Code:

#include <bits/stdc++.h>

using namespace std;

class node

{

    public:

    int key;

    node *left, *right;

};

node* newNode(int key)

{

    node* Node = new node();

    Node->key = key;

    Node->left = Node->right = NULL;

    return (Node);

}

   

node *rightRotate(node *x)

{

    node *y = x->left;

    x->left = y->right;

    y->right = x;

    return y;

}

  

node *leftRotate(node *x)

{

    node *y = x->right;

    x->right = y->left;

    y->left = x;

    return y;

}

  

node *splay(node *root, int key)

{

    if (root == NULL || root->key == key)

        return root;

  

    if (root->key > key)

    {

  

        if (root->left == NULL) return root;

        if (root->left->key > key)

        {  

            root->left->left = splay(root->left->left, key);

            root = rightRotate(root);

        }

        else if (root->left->key < key) // Zig-Zag (Left Right)

        {

            root->left->right = splay(root->left->right, key);

            if (root->left->right != NULL)

                root->left = leftRotate(root->left);

        }

        return (root->left == NULL)? root: rightRotate(root);

    }

    {

        if (root->right == NULL) return root;

        if (root->right->key > key)

        {

            root->right->left = splay(root->right->left, key);

            if (root->right->left != NULL)

                root->right = rightRotate(root->right);

        }

        else if (root->right->key < key)// Zag-Zag (Right Right)

        {

            root->right->right = splay(root->right->right, key);

            root = leftRotate(root);

        }

        return (root->right == NULL)? root: leftRotate(root);

    }

}

node *search(node *root, int key)

{

    return splay(root, key);

}

void preOrder(node *root)

{

    if (root != NULL)

    {

        cout<<root->key<<" ";

        preOrder(root->left);

        preOrder(root->right);

    }

}

int main()

{

    node *root = newNode(100);

    root->left = newNode(50);

    root->right = newNode(200);

    root->left->left = newNode(40);

    root->left->left->left = newNode(30);

    root->left->left->left->left = newNode(20);

    root = search(root, 20);

    cout << "Preorder traversal of the modified Splay tree is \n";

    preOrder(root);

    return 0;

}


Related Solutions

Show that every 2-tree with n internal nodes has n+ 1 external nodes
Show that every 2-tree with n internal nodes has n+ 1 external nodes
Consider the following splay tree: Show the paths from root to node 12, 10, 9, 5,...
Consider the following splay tree: Show the paths from root to node 12, 10, 9, 5, and 1 after search node 3. (Sample answer: for the above splay tree, the path from root to node 9 can be expressed as 10, 4, 6, 8, 9.) The path from root to node 12:Question Blank.The path from root to node 10:Question Blank.The path from root to node 9:Question Blank.The path from root to node 5:Question Blank.The path from root to node 1:Question...
in a decision tree, decision nodes are the nodes in which uncertainty is involved that is...
in a decision tree, decision nodes are the nodes in which uncertainty is involved that is out of the control of the decision maker. do you agree with the statement? explain with examples. projects may vary with the amount of leverage they will support. for example, acquisitions of real estate or capital equipment are often highly levered , whereas investments in intellectual property are not. do you agree with this statement. explain with examples.
Show that the external path length epl in a 2-tree with m external nodes satisfies epl≤...
Show that the external path length epl in a 2-tree with m external nodes satisfies epl≤ (1/2)(m^2+m−2). Conclude that epl≤ (1/2n)(n+ 3) for a 2-tree with n internal nodes.
write a java program that implements the splay tree data structure for the dictionary abstract data...
write a java program that implements the splay tree data structure for the dictionary abstract data type. Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line. in.dat file contents: 3456 5678 1234 2369 7721 3354 1321 4946 3210 8765 Then the program starts an interactive mode. The commands are as follows. S 1000 - splay the tree at 1000 F...
A binary tree model with 7 decision nodes will have how many terminal nodes?
A binary tree model with 7 decision nodes will have how many terminal nodes?
Create a kD tree with: x-nodes and y-nodes -- and maintain the following two properties: The...
Create a kD tree with: x-nodes and y-nodes -- and maintain the following two properties: The children of an x-node are y-nodes The children of a y-node are x-nodes Each type of node uses a different comparison to order points. This causes different levels of the tree to compare points differently, using the following rules: For every x-node: All descendants in the left subtree have a smaller x-coordinate than the point stored at the node. Visually, all descendant points are...
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Give a proof by induction that the number of nodes of a full binary tree with...
Give a proof by induction that the number of nodes of a full binary tree with a height of "h" is: (2^(h+1))-1.
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT