Forum: Object-Pascal / Delphi-Language
Delphi
by alzaimar,
22. Apr 2006
:gruebel: The mentioned algorithm constructs a perfectly balanced tree out of *any* list, no matter how it is sorted.
If the list is sorted in ascending order, the resulting tree is exactly what you expect: The left part is always smaller than the root, the right side is larger.
Now, if the list is sorted in descending order, but you want to remain the left-right order, you have to reverse...
Forum: Object-Pascal / Delphi-Language
Delphi
by alzaimar,
22. Apr 2006
What do you mean by 'two lists, one pre- the other one postorder' :gruebel: ?
If Yo have the list in reversed order, simply reverse 'left' and 'right'.
BTW: If you must deal with binary trees, ever tried AVL-Trees or Red-Black-Trees? They are self-balancing.
Forum: Object-Pascal / Delphi-Language
Delphi
by alzaimar,
22. Apr 2006
Hi,
Try something like this:
Divide the list into two halves. The mid element is your root.
Now do a recursive call to create a tree out of the left sub list and return the root as the left child of the above mentioned root. Do the same with the right sub list.
Procedure CreateTree (aList : TList; aLeft, aRight : Integer; Var aRoot : TNode);
Begin
If aLeft >= aRight Then
aRoot...