Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in Binary Search Tree in .NET 4.0?

Is there a built-in binary search tree in .NET 4.0, or do I need to build this abstract data type from scratch?

Edit

This is about the binary search tree specifically, and not abstract data type "trees" in general.

like image 725
Benny Skogberg Avatar asked Jul 16 '10 07:07

Benny Skogberg


People also ask

Does C# have binary search tree?

This articles describes the algorithm to insert and delete elements in a Binary Search Tree (BST) and it's implementation in C#. A Binary Search Tree is a binary tree with a search property where the elements in the left sub-tree are less than the root and elements in the right sub-tree are greater than the root.

How is a binary search tree implemented C#?

To find an element in a Binary Search Tree, we first need to compare the element with the root node; if it matches then we have the right node otherwise we need to check the left or right. The C# implementation of that same is as follows. The following is the test result of the implementation.

What is binary search in C#?

Binary search works on a sorted array. The value is compared with the middle element of the array. If equality is not found, then the half part is eliminated in which the value is not there. In the same way, the other half part is searched. Here is the mid element in our array.


1 Answers

I think the SortedSet<T> class in System.Collections.Generic is what you're looking for.

From this CodeProject article:

It is implemented using a self-balancing red-black tree that gives a performance complexity of O(log n) for insert, delete, and lookup. It is used to keep the elements in sorted order, to get the subset of elements in a particular range, or to get the Min or Max element of the set.

Source code https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/Generic/SortedSet.cs

like image 126
herzmeister Avatar answered Sep 22 '22 23:09

herzmeister