O'Reilly logo

C# Data Structures and Algorithms by Marcin Jamro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example – keep the tree balanced

As mentioned in the introduction to the topic of AVL trees, there is a very simple test that can cause a BST tree to become unbalanced. You can just add ordered numbers to create a long and skinny tree. So, let's try to create an example of adding exactly the same set of data to an AVL tree, implemented using the Adjunct library.

The code placed in the Main method in the Program class is as follows:

AvlTree<int> tree = new AvlTree<int>(); for (int i = 1; i < 10; i++) { tree.Add(i); } Console.WriteLine("In-order: " + string.Join(", ", tree.GetInorderEnumerator())); Console.WriteLine("Post-order: " + string.Join(", ", tree.GetPostorderEnumerator())); Console.WriteLine("Breadth-first: " + string.Join(", ", tree.GetBreadthFirstEnumerator())); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required