7    VisuAlgo.net / /bst Login Binary Search Tree AVL Tree
Exploration Mode ▿

>

>
slow
fast
go to beginning previous frame pause play next frame go to end

A Binary Search Tree (BST) is a binary tree in which each vertex has only up to 2 children that satisfy BST property: All vertices in the left subtree of a vertex must hold a value smaller than its own and all vertices in the right subtree of a vertex must hold a value larger than its own (we have assumption that all values are distinct in this visualization and small tweak is needed to cater for duplicates).


An Adelson-Velskii Landis (AVL) tree is a self-balancing BST that maintains it's height to be O(log N) when having N vertices in the AVL tree.


Remarks: By default, we show e-Lecture Mode for first time (or non logged-in) visitor.
Please login if you are a repeated visitor or register for a free account first.

X Esc
Next
N.Sect ]

To toggle between the standard Binary Search Tree and the AVL Tree (with different behavior during Insertion and Removal of an integer), select the respective header.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

You can view the BST visualisation here!


Root vertex does not have a parent. There can only be one root vertex in a BST. Leaf vertex does not have any child. There can be more than one leaf vertex in a BST. All other vertices that are not root nor leaf are called the internal vertices. All vertices have at least 4 attributes: parent, left, right, key/value/data although not all attributes will be used for all vertices. Some other implementation separates key (for ordering of vertices in the BST) with the actual satellite data.


As we do not allow duplicate integer in this visualization (the full implementation must consider duplicate integers too), notice the BST property: For every vertex X, all vertices on the left subtree of X are smaller than X and all vertices on the right subtree of X are greater than X.


All available operations on the BST/AVL Tree will be visualized/animated here.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

Search. Because of BST property, we can search for an integer in BST by setting the current vertex = root and check if the current vertex is smaller/equal/larger than what we are searching for. We then go to the right subtree/stop/go the left subtree, respectively. We keep doing this until we either find the required vertex or we don't.


Similarly because of BST Property, we can find the minimum/maximum element by starting from root and keep going to the left/right subtree, respectively.


[We recommend that you stop this e-Lecture mode now and try searching for an existing/non-existing integer in the BST, then try searching for an existing integer that is close/far from the root, then try searching for the minimum/maximum integer in the BST].

Search(5) Search(56) Search(23) Search(64) Search(100)


Analysis: Search/FindMin/FindMax operations run in O(h) where h is the height of the BST. It is O(log N) if the BST is height-balanced.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

Predecessor/Successor. Because of BST property, we can find the Successor of an integer X as follows:

  1. If X has a right subtree, the minimum integer in the right subtree of X must be the successor of X.
  2. If X does not have a right subtree, we need to traverse the ancestor(s) of X until we find 'a right turn' to vertex Y (or alternatively, until we find the first vertex Y that is greater than vertex X). Once we find vertex Y, we will see that vertex X is the maximum element in the left subtree of Y.

Only the maximum integer in the BST does not have successor.


[We recommend that you stop this e-Lecture mode now and try finding the successor of the i-th integer. The answer must be the (i+1)-th integer (except for the maximum integer)].

Search(5)


The operations for Predecessor of an integer X are defined similarly (just the mirror of Successor operations).


Analysis: Predecessor/Successor operation runs in O(h) where h is the height of the BST. It is O(log N) if the BST is height-balanced.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

Inorder Traversal. We can perform an Inorder Traversal of this BST to obtain a sorted integers inside this BST (in fact, if we 'flatten' the BST into one line, we will see that the vertices are ordered from smallest to largest). Inorder Traversal is a recursive method whereby we visit the left subtree first, exhausts all items in the left subtree, visit the current root, before exploring the right subtree and all items in the right subtree.


[We recommend that you stop this e-Lecture mode now and try this Inorder Traversal operation].


Analysis: Inorder Traversal runs in O(N), regardless of the height of the BST.


PS: Some people call insertion of N unordered integers into a BST in O(N log N) and then performing the O(N) Inorder Traversal as 'BST sort'.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

Insert. We can insert a new integer into BST by performing similar operation as Search. But this time, instead of reporting that the new integer is not found, we create a new vertex in that insertion point. This insert operation may entail some rotation(s) in AVL Tree to keep the tree height-balanced.


[We recommend that you stop this e-Lecture mode now and try inserting various random integers into the BST].


Analysis: Insert runs in O(h) where h is the height of the BST, but it is strictly O(log N) in AVL Tree.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

Remove. We can remove an existing integer in BST by performing similar operation as Search. But this time, instead of reporting that the existing integer is found, we perform one of the three possible removal cases (we suggest that you try each of them one by one):

  1. Deletion of a leaf vertex (easiest, just remove that leaf vertex),
  2. Deletion of a vertex with one child (connect that vertex's only child with that vertex's parent), and
  3. Deletion of a vertex with two children (replace that vertex with its successor, then delete its duplicated successor in its right subtree; PS: replacing that vertex with its predecessor is also a valid strategy).

This remove operation may entail some rotation(s) in AVL Tree to keep the tree height-balanced.


[We recommend that you stop this e-Lecture mode now and try deleting a leaf vertex, a vertex with one child, and a vertex with two children in this BST].


Analysis: Remove runs in O(h) where h is the height of the BST, but it is strictly O(log N) in AVL Tree.

X Esc
Prev
Next
[ P.Sect
N.Sect ]

Create. You can create a new BST. We have several options:

  1. Empty BST (you can then insert a few integers one by one),
  2. Random BST (we will soon add option to generate random-but-balanced BST),
  3. Skewed Left/Right BST (tall BST with N vertices and N-1 linked-list like edges, to showcase the worst case behavior of BST operations; this option is disabled in AVL Tree mode)
X Esc
Prev
Next
[ P.Sect

As the action is being carried out, each step will be described in the status panel.

X Esc
Prev
Next

You can also follow the pseudocode highlights to trace the algorithm.

X Esc
Prev
Next

Control the animation with the player controls! Keyboard shortcuts are:

Spacebar: play/pause/replay
Left/right arrows: step backward/step forward
-/+: decrease/increase speed
X Esc
Prev
Next

Return to 'Exploration Mode' to start exploring!


Note that if you notice any bug in this visualization or if you want to request for a new visualization feature, do not hesitate to drop an email to the project leader: Dr Steven Halim via his email address: stevenhalim at gmail dot com.

X Esc
Prev

Create

Insert

Remove

Pred-/Succ-essor

Inorder Traversal

>

Empty

Random

Skewed Left

Skewed Right

Go

Go

Get Predecessor

Get Successor

About Team Terms of use

About

VisuAlgo was conceptualised in 2011 by Dr Steven Halim as a tool to help his students better understand data structures and algorithms, by allowing them to learn the basics on their own and at their own pace.

VisuAlgo contains many advanced algorithms that are discussed in Dr Steven Halim's book ('Competitive Programming', co-authored with his brother Dr Felix Halim) and beyond. Today, some of these advanced algorithms visualization/animation can only be found in VisuAlgo.

Though specifically designed for National University of Singapore (NUS) students taking various data structure and algorithm classes (e.g. CS1010, CS1020, CS2010, CS2020, CS3230, and CS3230), as advocators of online learning, we hope that curious minds around the world will find these visualisations useful too.

VisuAlgo is not designed to work well on small touch screens (e.g. smartphones) from the outset due to the need to cater for many complex algorithm visualizations that require lots of pixels and click-and-drag gestures for interaction. The minimum screen resolution for a respectable user experience is 1024x768 and only the landing page is relatively mobile-friendly.

VisuAlgo is an ongoing project and more complex visualisations are still being developed.

The most exciting development is the automated question generator and verifier (the online quiz system) that allows students to test their knowledge of basic data structures and algorithms. The questions are randomly generated via some rules and students' answers are instantly and automatically graded upon submission to our grading server. This online quiz system, when it is adopted by more CS instructors worldwide, should technically eliminate manual basic data structure and algorithm questions from typical Computer Science examinations in many Universities. By setting a small (but non-zero) weightage on passing the online quiz, a CS instructor can (significantly) increase his/her students mastery on these basic questions as the students have virtually infinite number of training questions that can be verified instantly before they take the online quiz. The training mode currently contains questions for 12 visualization modules. We will soon add the remaining 8 visualization modules so that every visualization module in VisuAlgo have online quiz component.

Another active branch of development is the internationalization sub-project of VisuAlgo. We want to prepare a database of CS terminologies for all English text that ever appear in VisuAlgo system. This is a big task and requires crowdsourcing. Once the system is ready, we will invite VisuAlgo visitors to contribute, especially if you are not a native English speaker. Currently, we have also written public notes about VisuAlgo in various languages: zh, id, kr, vn, th.

Team

Project Leader & Advisor (Jul 2011-present)
Dr Steven Halim, Senior Lecturer, School of Computing (SoC), National University of Singapore (NUS)
Dr Felix Halim, Software Engineer, Google (Mountain View)

Undergraduate Student Researchers 1 (Jul 2011-Apr 2012)
Koh Zi Chun, Victor Loh Bo Huai

Final Year Project/UROP students 1 (Jul 2012-Dec 2013)
Phan Thi Quynh Trang, Peter Phandi, Albert Millardo Tjindradinata, Nguyen Hoang Duy

Final Year Project/UROP students 2 (Jun 2013-Apr 2014)
Rose Marie Tan Zhao Yun, Ivan Reinaldo

Undergraduate Student Researchers 2 (May 2014-Jul 2014)
Jonathan Irvin Gunawan, Nathan Azaria, Ian Leow Tze Wei, Nguyen Viet Dung, Nguyen Khac Tung, Steven Kester Yuwono, Cao Shengze, Mohan Jishnu

Final Year Project/UROP students 3 (Jun 2014-Apr 2015)
Erin Teo Yi Ling, Wang Zi

Final Year Project/UROP students 4 (Jun 2016-Dec 2017)
Truong Ngoc Khanh, John Kevin Tjahjadi, Gabriella Michelle, Muhammad Rais Fathin Mudzakir

List of translators who have contributed ≥100 translations can be found at statistics page.

Acknowledgements
This project is made possible by the generous Teaching Enhancement Grant from NUS Centre for Development of Teaching and Learning (CDTL).

Terms of use

VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter, course webpage, blog review, email, etc.

If you are a data structure and algorithm student/instructor, you are allowed to use this website directly for your classes. If you take screen shots (videos) from this website, you can use the screen shots (videos) elsewhere as long as you cite the URL of this website (http://visualgo.net) and/or list of publications below as reference. However, you are NOT allowed to download VisuAlgo (client-side) files and host it on your own website as it is plagiarism. As of now, we do NOT allow other people to fork this project and create variants of VisuAlgo. Using the offline copy of (client-side) VisuAlgo for your personal usage is fine.

Note that VisuAlgo's online quiz component is by nature has heavy server-side component and there is no easy way to save the server-side scripts and databases locally. Currently, the general public can only use the 'training mode' to access these online quiz system. Currently the 'test mode' is a more controlled environment for using these randomly generated questions and automatic verification for a real examination in NUS. Other interested CS instructor should contact Steven if you want to try such 'test mode'.

List of Publications

This work has been presented briefly at the CLI Workshop at the ACM ICPC World Finals 2012 (Poland, Warsaw) and at the IOI Conference at IOI 2012 (Sirmione-Montichiari, Italy). You can click this link to read our 2012 paper about this system (it was not yet called VisuAlgo back in 2012).

This work is done mostly by my past students. The most recent final reports are here: Erin, Wang Zi, Rose, Ivan.

Bug Reports or Request for New Features

VisuAlgo is not a finished project. Dr Steven Halim is still actively improving VisuAlgo. If you are using VisuAlgo and spot a bug in any of our visualization page/online quiz tool or if you want to request for new features, please contact Dr Steven Halim. His contact is the concatenation of his name and add gmail dot com.