O'Reilly logo

A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

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

Insertion

To discover the algorithm for inserting a new value into a binary tree, let’s work with an example. Say that we want to insert the number 45 into the previous tree.

The first thing we’d have to do is find the correct node to attach the 45 to. To begin our search, we start at the root:

images/chapter13/binary_trees_Part6.png

Since 45 is less than 50, we drill down to the left child:

images/chapter13/binary_trees_Part10.png

Since 45 is greater than 25, we must inspect the right child:

images/chapter13/binary_trees_Part11.png

45 is greater than 33, so we check ...

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