Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals  

BinaryTrees.c

Go to the documentation of this file.
00001 /* Binary trees. */
00002 
00013 #include <stdlib.h>
00014 #include "LinkedLists.h"
00015 
00017 void InsertInTree(BinaryNodePtr theNode, BinaryNodePtr *root,
00018       SInt8 (*CompareFunction)(NodePtr a, NodePtr b))
00019 {
00020   BinaryNode **curPos = root;
00021   while (1) /* Who needs recursion? */
00022   {
00023     if (!*curPos)
00024     {
00025       *curPos = theNode;
00026       break;
00027     }
00028     else if (CompareFunction((NodePtr)(theNode), (NodePtr)(*curPos)) >= 0)
00029     {
00030       curPos = &((*curPos)->right);
00031     }
00032     else
00033     {
00034       curPos = &((*curPos)->left);
00035     }
00036   }
00037 }
00038 
00040 
00045 BinaryNodePtr SortedDoubleToBinaryWithSize(DoubleNodePtr linkedList, UInt32 size)
00046 {
00047   UInt32 halfSize = (size >> 1), counter, sizeA, sizeB;
00048   DoubleNodePtr middleNode;
00049   
00050   sizeA = halfSize;
00051   sizeB = halfSize;
00052   sizeB -= ((size & 1) ? (0) : (1));
00053   
00054   counter = halfSize;
00055   middleNode = linkedList;
00056   while (counter--)
00057     middleNode = middleNode->next;
00058   
00059   if (sizeB)
00060     middleNode->next = /* Or ->right */
00061     (DoubleNodePtr)(SortedDoubleToBinaryWithSize(middleNode->next, sizeB));
00062   else
00063     middleNode->next = (DoubleNodePtr)(0);
00064   
00065   if (sizeA)
00066     middleNode->prev = /* Or ->left */
00067     (DoubleNodePtr)(SortedDoubleToBinaryWithSize(linkedList, sizeA));
00068   else
00069     middleNode->prev = (DoubleNodePtr)(0);
00070   
00071   return (BinaryNodePtr)(middleNode);
00072 }
00073 
00075 DoubleNodePtr BinaryToDouble(BinaryNodePtr root, UInt8 fillPrev)
00076 {
00077   IndirectNodePtr list;
00078   DoubleNodePtr doubleList;
00079   LeftNodeRight(root, &list);
00080   ApplyIndirectOnSimple(list);
00081   if (fillPrev)
00082     FillPrev((DoubleNodePtr)(list->nodePtr));
00083   doubleList = (DoubleNodePtr)(list->nodePtr);
00084   DestroyLinkedList((NodePtr)(list), DestroyIndirectNode);
00085   return doubleList;
00086 }
00087 
00089 
00092 IndirectNodePtr* LeftNodeRight(BinaryNodePtr node, IndirectNodePtr *theList)
00093 {
00094   IndirectNodePtr newNode = malloc(sizeof(IndirectNode));
00095   if (node->left)
00096     theList = LeftNodeRight(node->left, theList);
00097   newNode->nodePtr = (NodePtr)(node);
00098   newNode->nodeHeader.next = (NodePtr)(0);
00099   *theList = newNode;
00100   theList = (IndirectNodePtr*)(&newNode->nodeHeader.next);
00101   if (node->right)
00102     theList = LeftNodeRight(node->right, theList);
00103   return theList;
00104 }
00105 
00107 
00110 void BalanceTree(BinaryNodePtr *root)
00111 {
00112   DoubleNodePtr doubleList = BinaryToDouble(*root, 0);
00113   *root = SortedDoubleToBinary(doubleList);
00114   /* It's that small! Wow! */
00115 }
00116 

Generated on Sun Dec 23 15:20:37 2001 for ANet by doxygen 1.2.12 written by Dimitri van Heesch, © 1997-2001

SourceForge
Logo