Introduction to Scapegoat Trees
Learn the implementation of ScapegoatTrees in this lesson.
ScapegoatTree
overview
Here, we study a binary search tree data structure, the ScapegoatTree
. This structure is based on the common wisdom that, when something goes wrong, the first thing people tend to do is find someone to blame (the scapegoat). Once blame is firmly established, we can leave
the scapegoat to fix the problem.
A ScapegoatTree
keeps itself balanced by partial rebuilding operations. During a partial rebuilding operation, an entire subtree is deconstructed and rebuilt into a perfectly balanced subtree. There are many ways of rebuilding a subtree rooted at node u
into a perfectly balanced
tree. One of the simplest is to traverse u
’s subtree, gathering all its nodes
into an array, a
, and then to recursively build a balanced subtree using
a
. If we let m = a.length/2
, then the element a[m]
becomes the root of the
new subtree, a get stored recursively in the left subtree and
get stored recursively in the right subtree.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy