Structures de données 101: arbre de recherche binaire

Comment combiner l'efficacité de l'insertion d'une liste liée et la recherche rapide d'un tableau ordonné.

Qu'est-ce qu'un arbre de recherche binaire?

Commençons par la terminologie de base afin que nous puissions partager le même langage et étudier les concepts connexes. Premièrement, quels sont les principes qui définissent un arbre de recherche binaire?

* À partir de maintenant, j'utiliserai "BST" par souci de concision

Un BST est considéré comme une structure de données composée de nœuds , comme les listes liées . Ces nœuds sont nuls ou ont des références (liens) vers d'autres nœuds. Ces «autres» nœuds sont des nœuds enfants, appelés nœud gauche et nœud droit. Les nœuds ont des valeurs . Ces valeurs déterminent où elles sont placées dans le BST.

De la même manière qu'une liste chaînée, chaque nœud est référencé par un seul autre nœud, son parent (à l'exception du nœud racine). On peut donc dire que chaque nœud d'un BST est en soi un BST. Parce que plus bas dans l'arbre, nous atteignons un autre nœud et ce nœud a une gauche et une droite. Ensuite, selon la direction dans laquelle nous allons, ce nœud a une gauche et une droite et ainsi de suite.

1. Le nœud gauche est toujours plus petit que son parent.

2. Le nœud droit est toujours supérieur à son parent.

3. Un BST est considéré comme équilibré si chaque niveau de l'arborescence est entièrement rempli à l'exception du dernier niveau. Au dernier niveau, l'arbre est rempli de gauche à droite.

4. Un Perfect BST est celui dans lequel il est à la fois complet et complet (tous les nœuds enfants sont au même niveau et chaque nœud a un nœud enfant gauche et un nœud enfant droit).

Pourquoi utiliserions-nous cela?

Quels sont des exemples concrets de BST? Les arbres sont souvent utilisés dans la recherche, la logique du jeu, les tâches de saisie semi-automatique et les graphiques.

La vitesse. Comme mentionné précédemment, le BST est une structure de données ordonnée. Lors de l'insertion, les nœuds sont placés de manière ordonnée. Cet ordre inhérent rend la recherche rapide. Semblable à la recherche binaire (avec un tableau trié), nous réduisons de moitié la quantité de données à trier à chaque passage. Par exemple, supposons que nous recherchions une petite valeur de nœud. À chaque passage, nous continuons à avancer le long du nœud le plus à gauche. Cela élimine automatiquement la moitié des valeurs les plus élevées!

De plus, contrairement à un tableau, les données sont stockées par référence. À mesure que nous ajoutons à la structure de données, nous créons un nouveau bloc en mémoire et nous y établissons un lien. C'est plus rapide que de créer un nouveau tableau avec plus d'espace, puis d'insérer les données du plus petit tableau vers le nouveau, plus grand.

En bref, l'insertion, la suppression et la recherche sont les étoiles pour un BST

Maintenant que nous comprenons les principes, les avantages et les composants de base d'un BST, implémentons-en un en javascript.

L'API pour un BST comprend les éléments suivants: Insertion, Contains, Get Min, Get Max, Remove Node, Check if Full, Is Balanced , et les types de recherche - Depth First (preOrder, inOrder, postOrder), Breadth First Search , et enfin Get Height . C'est une grande API, prenez-la juste une section à la fois.

la mise en oeuvre

Le constructeur

Le BST est composé de nœuds et chaque nœud a une valeur.

function Node(value){ this.value = value; this.left = null; this.right = null;}

Le constructeur BST est constitué d'un nœud racine.

function BinarySearchTree() { this.root = null;}
let bst = new BST();let node = new Node();
console.log(node, bst); // Node { value: undefined, left: null, right: null } BST { root: null }

… jusqu'ici tout va bien.

Insertion

BinarySearchTree.prototype.insert = function(value){ let node = new Node(value); if(!this.root) this.root = node; else{ let current = this.root; while(!!current){ if(node.value  current.value){ if(!current.right){ current.right = node; break; } current = current.right; } else { break; } } } return this; };
let bst = new BST();bst.insert(25); // BST { root: Node { value: 25, left: null, right: null } }

Ajoutons quelques valeurs supplémentaires.

bst.insert(40).insert(20).insert(9).insert(32).insert(15).insert(8).insert(27);
BST { root: Node { value: 25, left: Node { value: 20, left: [Object], right: null }, right: Node { value: 40, left: [Object], right: null } } }

Pour une visualisation sympa, allez ici !!

Déballons ceci.

  1. Tout d'abord, nous passons une valeur et créons un nouveau nœud
  2. Vérifiez s'il existe une racine, sinon, définissez ce nœud nouvellement créé sur le nœud racine
  3. S'il y a un nœud racine, nous créons une variable déclarée «courante», et définissons sa valeur sur le nœud racine
  4. Si le node.value nouvellement créé est plus petit que le nœud racine, nous nous déplacerons vers la gauche
  5. Nous continuons à comparer ce node.value aux nœuds de gauche.
  6. Si la valeur est suffisamment petite et que nous atteignons un point où il n'y a plus de nœuds à gauche, nous plaçons cet élément ici.
  7. Si la valeur node.value est supérieure, nous répétons les mêmes étapes que ci-dessus sauf que nous nous déplaçons le long de la droite.
  8. Nous avons besoin des instructions break car il n'y a pas d'étape de comptage pour terminer la boucle while.

Contient

C'est une approche assez simple.

BinarySearchTree.prototype.contains = function(value){ let current = this.root; while(current){ if(value === current.value) return true; if(value  current.value) current = current.right; } return false;};

Obtenez Min et Get Max.

Continuez à parcourir de gauche à la plus petite valeur ou à droite pour la plus grande.

BinarySearchTree.prototype.getMin = function(node){ if(!node) node = this.root; while(node.left) { node = node.left; } return node.value};
BinarySearchTree.prototype.getMax = function(node){ if(!node) node = this.root; while(node.right) { node = node.right; } return node.value;};

Suppression

Removing a node is the trickiest operation, because nodes have to be reordered to maintain the properties of a BST. There is a case if a node has only one child and a case if there is both a left and a right node. We use the larger helper function to do the heavy lifting.

BinarySearchTree.prototype.removeNode = function(node, value){ if(!node){ return null; } if(value === node.value){ // no children if(!node.left && !node.right) return null; // one child and it’s the right if(!node.left) node.right;// one child and it’s the left if(!node.right) node.left; // two kids const temp = this.getMin(node.right); node.value = temp; node.right = this.removeNode(node.right, temp); return node; } else if(value < node.value) { node.left = this.removeNode(node.left, value); return node; } else { node.right = this.removeNode(node.right, value); return node; }};
BinarySearchTree.prototype.remove = function(value){ this.root = this.removeNode(this.root, value);};

It works like this…

Unlike deleteMin and deleteMax, where we can just traverse all the way left or all the way right and pick off the last value, we have to take out a node and then replace it with something. This solution was developed in 1962 by T. Hibbard. We account for the case where we can delete a node with only one child or none, that’s minor. If no children, no problem. If a child is present, that child just moves up one.

But with a node scheduled to be removed that has two children, which child takes its place? Certainly, we can’t move a larger node down. So what we do is replace it with its successor, the next kingpin. We have to find the smallest right child on the right that is larger than the left child.

  1. Create a temp value and store the smallest node on its right. What this does is satisfy the property that values to the left are still smaller and values to the right are still greater.
  2. Reset the node’s value to this temp variable
  3. Remove the right node.
  4. Then we compare values on the left and the right and determine the assigned value.

This is best explained with a picture:

Searching

There are two types of search, Depth First and Breadth First. Breadth First is simply stopping at each level on the way down. It looks like this: we start at the root, then the left child, then the right child. Move to the next level, left child then right child. Think of this as moving horizontally. We employ, I should say simulate, a queue to help order the process. We pass a function, because many times we want to operate on a value.

BinarySearchTree.prototype.traverseBreadthFirst = function(fn) { let queue = []; queue.push(this.root); while(!!queue.length) { let node = queue.shift(); fn(node); node.left && queue.push(node.left); node.right && queue.push(node.right); }}

Depth First Search involves moving down the BST in a specified manner, either, preOrder, inOrder, or postOrder. I’ll explain the differences shortly.

In the spirit of concise code, we have a basic traverseDepthFirst function and we pass a function and a method. Again the function implies that we want to do something to the values along the way, while the method is the type of search we wish to perform. In the traverseDFS, we have a fallback: preOrder search in place.

Now, how is each one different? First, let’s dispatch inOrder. It should be self-explanatory but it isn’t. Do we mean in order of insertion, in order of highest to lowest or lowest to highest? I just wanted you to consider these things beforehand. In this case, yes, it does mean lowest to highest.

preOrder can be thought of as Parent, Left Child, then Right child.

postOrder as Left Child, Right Child, Parent.

BinarySearchTree.prototype.traverseDFS = function(fn, method){ let current = this.root; if(!!method) this[method](current, fn); else this._preOrder(current, fn);};
BinarySearchTree.prototype._inOrder = function(node, fn){ if(!!node){ this._inOrder(node.left, fn); if(!!fn) fn(node); this._inOrder(node.right, fn); }};
BinarySearchTree.prototype._preOrder = function(node, fn){ if(node){ if(fn) fn(node); this._preOrder(node.left, fn); this._preOrder(node.right, fn); }};
BinarySearchTree.prototype._postOrder = function(node, fn){ if(!!node){ this._postOrder(node.left, fn); this._postOrder(node.right, fn); if(!!fn) fn(node); }};

Check if the BST is full

Remember from earlier, a BST is full if every node has Zero or Two children.

// a BST is full if every node has zero two children (no nodes have one child)
BinarySearchTree.prototype.checkIfFull = function(fn){ let result = true; this.traverseBFS = (node) => { if(!node.left && !node.right) result = false; else if(node.left && !node.right) result = false; } return result;};

Get Height of BST

What does it mean to get the height of a tree? Why is this important? This is where Time Complexity (aka Big O) comes into play. Basic operations are proportional to the height of a tree. So as we alluded to earlier, if we search for a particular value, the number of operations we have to do is halved on each step.

That means if we have a loaf of bread and cut it in half, then cut that half in half, and keep doing that till we get the exact piece of bread we want.

In computer science, this is called O(log n). We start with an input size of some sort, and over time that size gets smaller (kind of flattening out). A straight linear search is denoted as O(n), as the input size increases so does the time it takes to run operations. O(n) conceptually is a 45-degree line starting at origin zero on a chart and moving right. The horizontal scale represents the size of an input and the vertical scale represents the time it takes to complete.

Constant time is O(1). No matter how large or small the input size is, the operation takes place in the same amount of time. For example, push() and pop() off of an array are constant time. Looking up a value in a HashTable is constant time.

I will explain more about this in a future article, but I wanted to arm you with this knowledge for now.

Back to height.

We have a recursive function, and our base case is: ‘if we have no node then we start at this.root’. This implies that we can start at values lower in the tree and get tree sub-heights.

So if we pass in this.root to start, we recursively move down the tree and add the function calls to the execution stack (other articles here). When we get to the bottom, the stack is filled. Then the calls get executed and we compare the heights of the left and the heights of the right and increment by one.

BinarySearchTree.prototype._getHeights = function(node){ if(!node) return -1; let left = this._getHeights(node.left); let right = this._getHeights(node.right); return Math.max(left, right) + 1;};
BinarySearchTree.prototype.getHeight = function(node){ if(!node) node = this.root; return this._getHeights(node);};

Lastly, Is Balanced

What we are doing is checking if the tree is filled at every level, and on the last level, if it is filled left to right.

BinarySearchTree.prototype._isBalanced = function(node){ if(!node) return true; let heightLeft = this._getHeights(node.left); let heightRight = this._getHeights(node.right); let diff = Math.abs(heightLeft — heightRight); if(diff > 1) return false; else return this._isBalanced(node.left) && this._isBalanced(node.right);};
BinarySearchTree.prototype.isBalanced = function(node){ if(!node) node = this.root; return this._isBalanced(node);};

Print

Use this to visualize all the methods you see, especially depth first and breadth first traversals.

BinarySearchTree.prototype.print = function() { if(!this.root) { return console.log(‘No root node found’); } let newline = new Node(‘|’); let queue = [this.root, newline]; let string = ‘’; while(queue.length) { let node = queue.shift(); string += node.value.toString() + ‘ ‘; if(node === newline && queue.length) queue.push(newline); if(node.left) queue.push(node.left); if(node.right) queue.push(node.right); } console.log(string.slice(0, -2).trim());};

Our Friend Console.log!! Play around and experiment.

const binarySearchTree = new BinarySearchTree();binarySearchTree.insert(5);binarySearchTree.insert(3);
binarySearchTree.insert(7);binarySearchTree.insert(2);binarySearchTree.insert(4);binarySearchTree.insert(4);binarySearchTree.insert(6);binarySearchTree.insert(8);binarySearchTree.print(); // => 5 | 3 7 | 2 4 6 8
binarySearchTree.contains(4);
//binarySearchTree.printByLevel(); // => 5 \n 3 7 \n 2 4 6 8console.log('--- DFS inOrder');
binarySearchTree.traverseDFS(function(node) { console.log(node.value); }, '_inOrder'); // => 2 3 4 5 6 7 8
console.log('--- DFS preOrder');
binarySearchTree.traverseDFS(function(node) { console.log(node.value); }, '_preOrder'); // => 5 3 2 4 7 6 8
console.log('--- DFS postOrder');
binarySearchTree.traverseDFS(function(node) { console.log(node.value); }, '_postOrder'); // => 2 4 3 6 8 7 5
console.log('--- BFS');
binarySearchTree.traverseBFS(function(node) { console.log(node.value); }); // => 5 3 7 2 4 6 8
console.log('min is 2:', binarySearchTree.getMin()); // => 2
console.log('max is 8:', binarySearchTree.getMax()); // => 8
console.log('tree contains 3 is true:', binarySearchTree.contains(3)); // => true
console.log('tree contains 9 is false:', binarySearchTree.contains(9)); // => false
// console.log('tree height is 2:', binarySearchTree.getHeight()); // => 2
console.log('tree is balanced is true:', binarySearchTree.isBalanced(),'line 220'); // => true
binarySearchTree. remove(11); // remove non existing node
binarySearchTree.print(); // => 5 | 3 7 | 2 4 6 8
binarySearchTree.remove(5); // remove 5, 6 goes up
binarySearchTree.print(); // => 6 | 3 7 | 2 4 8
console.log(binarySearchTree.checkIfFull(), 'should be true');
var fullBSTree = new BinarySearchTree(10);
fullBSTree.insert(5).insert(20).insert(15).insert(21).insert(16).insert(13);
console.log(fullBSTree.checkIfFull(), 'should be true');
binarySearchTree.remove(7); // remove 7, 8 goes up
binarySearchTree.print(); // => 6 | 3 8 | 2 4
binarySearchTree.remove(8); // remove 8, the tree becomes unbalanced
binarySearchTree.print(); // => 6 | 3 | 2 4
console.log('tree is balanced is false:', binarySearchTree.isBalanced()); // => true
console.log(binarySearchTree.getHeight(),'height is 2')
binarySearchTree.remove(4);
binarySearchTree.remove(2);
binarySearchTree.remove(3);
binarySearchTree.remove(6);
binarySearchTree.print(); // => 'No root node found'
//binarySearchTree.printByLevel(); // => 'No root node found'
console.log('tree height is -1:', binarySearchTree.getHeight()); // => -1
console.log('tree is balanced is true:', binarySearchTree.isBalanced()); // => true
console.log('---');
binarySearchTree.insert(10);
console.log('tree height is 0:', binarySearchTree.getHeight()); // => 0
console.log('tree is balanced is true:', binarySearchTree.isBalanced()); // => true
binarySearchTree.insert(6);
binarySearchTree.insert(14);
binarySearchTree.insert(4);
binarySearchTree.insert(8);
binarySearchTree.insert(12);
binarySearchTree.insert(16);
binarySearchTree.insert(3);
binarySearchTree.insert(5);
binarySearchTree.insert(7);
binarySearchTree.insert(9);
binarySearchTree.insert(11);
binarySearchTree.insert(13);
binarySearchTree.insert(15);
binarySearchTree.insert(17);
binarySearchTree.print(); // => 10 | 6 14 | 4 8 12 16 | 3 5 7 9 11 13 15 17
binarySearchTree.remove(10); // remove 10, 11 goes up
binarySearchTree.print(); // => 11 | 6 14 | 4 8 12 16 | 3 5 7 9 x 13 15 17
binarySearchTree.remove(12); // remove 12; 13 goes up
binarySearchTree.print(); // => 11 | 6 14 | 4 8 13 16 | 3 5 7 9 x x 15 17
console.log('tree is balanced is true:', binarySearchTree.isBalanced()); // => true
//console.log('tree is balanced optimized is true:', binarySearchTree.isBalancedOptimized()); // => true
binarySearchTree.remove(13); // remove 13, 13 has no children so nothing changes
binarySearchTree.print(); // => 11 | 6 14 | 4 8 x 16 | 3 5 7 9 x x 15 17
console.log('tree is balanced is false:', binarySearchTree.isBalanced()); // => false
// yields ...5 | 3 7 | 2 4 6 8--- DFS inOrder2345678--- DFS preOrder5324768--- DFS postOrder2436875--- BFS5372468min is 2: 2max is 8: 8tree contains 3 is true: truetree contains 9 is false: falsetree is balanced is true: true line 2205 | 3 7 | 2 4 6 86 | 3 7 | 2 4 8true 'should be true'true 'should be true'6 | 3 8 | 2 46 | 3 | 2 4tree is balanced is false: false2 'height is 2'No root node foundtree height is -1: -1tree is balanced is true: true---tree height is 0: 0tree is balanced is true: true10 | 6 14 | 4 8 12 16 | 3 5 7 9 11 13 15 1711 | 6 14 | 4 8 12 16 | 3 5 7 9 13 15 1711 | 6 14 | 4 8 13 16 | 3 5 7 9 15 17tree is balanced is true: true11 | 6 14 | 4 8 16 | 3 5 7 9 15 17tree is balanced is false: false

Time Complexity

1. Insertion O(log n)

2. Removal O(log n)

3. Search O(log n)

Wow, that is indeed a lot of information. I hope the explanations were as clear and as introductory as possible. Again, writing helps me solidify concepts and as Richard Feynman said, “When one person teaches, two learn.”

Resources

Probably the best resource for visualizing, definitely use it:

Data Structure Visualization

David Galles Computer Science University of San Franciscowww.cs.usfca.eduBinaryTreeVisualiser - Binary Search Tree

Site description herebtv.melezinek.czVisuAlgo - Binary Search Tree, AVL Tree

A Binary Search Tree (BST) is a binary tree in which each vertex has only up to 2 children that satisfies BST property…visualgo.netBig-O Algorithm Complexity Cheat Sheet (Know Thy Complexities!) @ericdrowell

Hi there! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When…www.bigocheatsheet.comAlgorithms, 4th Edition by Robert Sedgewick and Kevin Wayne

The textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne surveys the most important algorithms and data…algs4.cs.princeton.eduBinary search tree - Wikipedia

In computer science, binary search trees ( BST), sometimes called ordered or sorted binary trees, are a particular type…en.wikipedia.org