Balanced binary search tree using sortedset?

Use recursion. Each branch generates a new branch, select the middle item in the unsorted set, the median. Put it in the current item in the tree.

Copy all items less than the median to another array, send that new array to the call of the same method. Copy all items greater than the median to another array, send that new array to the call of the same method.

Use recursion. Each branch generates a new branch, select the middle item in the unsorted set, the median. Put it in the current item in the tree.

Copy all items less than the median to another array, send that new array to the call of the same method. Copy all items greater than the median to another array, send that new array to the call of the same method. \ Balanced trees have to have an odd number of items, unless the main parent node is not filled in.

You need to decide if there are two values that are the Median, whether the duplicate belongs on the lower branch or upper branch. I put duplicates on the upper branch in my example. The median will be the number where an equal amount of numbers is less than and greater than the number.1,2,3,3,4,18,29,105,123 In this case, the median is 4, even though the mean (or average) is much higher.

I didn't include code that determines the median. BuildTreeItem(TreeItem Item, Array Set) { Array Smalls; Array Larges; Median = DetermineMedian(Set); Item. Value = Median; if(Set.Count() == 1) return; for (int I = 0; int I New(Seti); } else { Larges.

New(Seti); } } Item. Lower = new TreeItem; Item. Upper = new TreeItem; BuildTreeItem(TreeItem.

Lower, Smalls); BuildTreeItem(TreeItem. Upper, Larges); }.

So all what I would need is just to add the code for the Median and that would generate the tree. – Sam Omar Jan 21 at 2:06.

Unless it is homework the easiest solution would be to sort data first and then build a tree by using middle item as root and descending down each half. Method proposed by Xaade is similar , but much slower due to DetermineMedian complexity. The other option is to actually look at algorithms that build balanced trees (like en.wikipedia.org/wiki/Red-black_tree ) to see if it fits your requirements.

EDIT: removing incorrect statement about speed of Xaade algorithm - it is actually as fast as quick sort (n log n - check each element on every level of recursion with log n levels of recursion), not sure why I estimated it slower.

Ya it is homework... and this is what I got so far as code. – Sam Omar Jan 21 at 1:52 using System; namespace bst { public class Node { public int value; public Node Right = null; public Node Left = null; public Node(int value) { this. Value = value; } } public class BST { public Node Root = null; public BST() { } – Sam Omar Jan 21 at 1:57 public void Add(int new_value) { if(Search(new_value)) { Console.

WriteLine("value (" + new_value + ") already"); } else { AddNode(this. Root,new_value); } } – Sam Omar Jan 21 at 1:58 basically i'm completely lost it is a homework problem but first I need to deteremine how to make this tree to work on my homework and starting avering out the time and work on search functions but i'm just stcuk it would be awesome if I get help for the coding part... – Sam Omar Jan 21 at 1:59 1 Depends on the method of sorting. A bubble sort would be slower than my algorithm.

Quicksorting would be faster though – Xaade Jan 21 at 17:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions