Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:45:58+00:00 2026-05-13T08:45:58+00:00

Going through some excercises to hone my binary tree skills, I decided to implement

  • 0

Going through some excercises to hone my binary tree skills, I decided to implement a splay tree, as outlined in Wikipedia: Splay tree.

One thing I’m not getting is the part about insertion.

It says:

First, we search x in the splay tree. If x does not already exist, then we will not find it, but its parent node y. Second, we perform a splay operation on y which will move y to the root of the splay tree. Third, we insert the new node x as root in an appropriate way. In this way either y is left or right child of the new root x.

My question is this: The above text seems overly terse compared to the other examples in the article, why is that? It seems there are some gotchas left out here. For instance, after splaying the y node up to the root, I can’t just blindly replace root with x, and tack y onto x as either left or right child.

Let’s assume the value does not already exist in the tree.

I have this tree:

           10
          /  \
         5    15
        / \    \
       1   6    20

and I want to insert 8. With the description above, I will up finding the 6-node, and in a normal binary tree, 8 would be added as a right child of the 6-node, however here I first have to splay the 6-node up to root:

            6
           / \
          5   10
         /     \
        1       15
                 \
                  20

then either of these two are patently wrong:

          8                                  8
           \                                /
            6                              6
           / \                            / \
          5   10                         5   10
         /     \                        /     \
        1       15                     1       15
                 \                              \
                  20                             20

    6 is not greater than 8          10 is not less than 8

it seems to me that the only way to do the splaying first, and then correctly adding the new value as root would mean I have to check the following criteria (for adding the splayed node as the left child of the new root):

  1. the node I splayed to the root is less than the new root (6 < 8)
  2. the rightmost child of the node I splayed to the root is also less than the new root (20 8)

However, if I were to split up the node I splayed, by taking the right child and appending it as the right child of the new node, I would get this:

                        8
                       / \
                      6   10
                     /     \
                    5       15
                   /         \
                  1           20

But, is this simple alteration always going to give me a correct tree? I’m having a hard time coming up with an example, but could this lead to the following:

  • The new value I want to add is higher than the temporary root (the node I splayed to the root), but also higher than the leftmost child of the right-child of the temporary root?

Ie. a tree that would basically look like this after splaying, but before I replace the root?

                        10
                       /  \
                      5    15
                          /  \
                        11    20

and I want to add 13, which would make the new tree like this:

                        13
                       /  \
                     10    15
                     /    /  \
                    5   11    20  <-- 11, on the wrong side of 13

or can this never happen?

My second question is this: Wouldn’t it be much easier to just rewrite the operation as follows:

First, we search x in the splay tree. If x does not already exist, then we will not find it, but its parent node y. Then we add the new node as either a left or right child of the parent node. Thirdly, we perform a splay operation on the node we added which will move the new value to the root of the splay tree.

emphasis mine to show what I changed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:45:59+00:00Added an answer on May 13, 2026 at 8:45 am

    I don’t see how the problem you describe could happen. If you want to insert 13 into this tree you first have to find where it would be:

                        10
                       /  \
                      5    15
                          /  \
                        11    20
    

    From 10 you go right, from 15 you go left, from 11 you go right… and then you have no more elements. If 13 had been in the tree, we would have found it as a right child of 11. So according to the rule we perform a splay operation on 11 which will move 11 to the root of the splay tree:

                        11
                       /  \
                      10   15
                     /       \
                    5         20
    

    Then we add 13 as the new root, with 11 as the left child:

                        13
                       /  \
                      11   15
                     /       \
                    10        20
                   /
                  5
    

    Now there is no problem.

    First, we search x in the splay tree. If x does not already exist, then we will not find it, but its parent node y. Then we add the new node as either a left or right child of the parent node. Thirdly, we perform a splay operation on the node we added which will move the new value to the root of the splay tree.

    This sounds to me like it would work too, but if I were you, I’d just try to implement the version as it described in Wikipedia since lots of people have tested that and it is already well documented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try executing this command /opt/local/bin/ruby -v from the command line… May 13, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer The obvious problem would be the risk of abandoning a… May 13, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer try pdftotext. Then insert it into DB. May 13, 2026 at 4:27 pm

Related Questions

I'm currently working through the excercises in 'The C Programming Language'. Here's one of
I'm starting to learn C by reading K&R and going through some of the
I was going through this tutorial for fun, and got stuck on the very
just like in topic - is there any software to open (what?) and here
I'm working through Real World Haskell , and at the moment doing the exercises

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.