ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

searchTree.cc
Go to the documentation of this file.
00001 /*
00002 ** License Applicability. Except to the extent portions of this file are
00003 ** made subject to an alternative license as permitted in the SGI Free
00004 ** Software License B, Version 1.1 (the "License"), the contents of this
00005 ** file are subject only to the provisions of the License. You may not use
00006 ** this file except in compliance with the License. You may obtain a copy
00007 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
00008 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
00009 ** 
00010 ** http://oss.sgi.com/projects/FreeB
00011 ** 
00012 ** Note that, as provided in the License, the Software is distributed on an
00013 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
00014 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
00015 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
00016 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
00017 ** 
00018 ** Original Code. The Original Code is: OpenGL Sample Implementation,
00019 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
00020 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
00021 ** Copyright in any portions created by third parties is as indicated
00022 ** elsewhere herein. All Rights Reserved.
00023 ** 
00024 ** Additional Notice Provisions: The application programming interfaces
00025 ** established by SGI in conjunction with the Original Code are The
00026 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
00027 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
00028 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
00029 ** Window System(R) (Version 1.3), released October 19, 1998. This software
00030 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
00031 ** published by SGI, but has not been independently verified as being
00032 ** compliant with the OpenGL(R) version 1.2.1 Specification.
00033 **
00034 ** $Date: 2006-03-12 00:07:02 +0000 (Sun, 12 Mar 2006) $ $Revision: 1.1 $
00035 */
00036 /*
00037 ** $Header: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/glu32/libnurbs/nurbtess/searchTree.cc,v 1.1 2004/02/02 16:39:15 navaraf Exp $
00038 */
00039 
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include "zlassert.h"
00043 
00044 #include "searchTree.h"
00045 
00046 #define max(a,b) ((a>b)? a:b)
00047 
00048 treeNode* TreeNodeMake(void *key)
00049 {
00050   treeNode *ret = (treeNode*) malloc(sizeof(treeNode));
00051   assert(ret);
00052   ret->key = key;
00053   ret->parent = NULL;
00054   ret->left = NULL;
00055   ret->right = NULL;
00056   return ret;
00057 }
00058 
00059 void TreeNodeDeleteSingleNode(treeNode* node)
00060 {
00061   free(node);
00062 }
00063 
00064 void TreeNodeDeleteWholeTree(treeNode* node)
00065 {
00066   if(node == NULL) return;
00067   TreeNodeDeleteWholeTree(node->left);
00068   TreeNodeDeleteWholeTree(node->right);
00069   TreeNodeDeleteSingleNode(node);
00070 }
00071 
00072 void TreeNodePrint(treeNode* node, 
00073            void (*keyPrint) (void*))
00074 {
00075   if(node ==NULL) return;
00076   TreeNodePrint(node->left, keyPrint);
00077   keyPrint(node->key);
00078   TreeNodePrint(node->right, keyPrint);  
00079 }
00080 
00081 int TreeNodeDepth(treeNode* root)
00082 {
00083   if(root == NULL) return 0;
00084   else{
00085     int leftdepth = TreeNodeDepth(root->left);
00086     int rightdepth = TreeNodeDepth(root->right);  
00087     return 1 + max(leftdepth, rightdepth);
00088   }
00089 }
00090 
00091 /*return the node with the key.
00092  *NULL is returned if not found
00093  */
00094 treeNode* TreeNodeFind(treeNode* tree, void* key,
00095                int (*compkey) (void*, void*))          
00096 {
00097   if(tree == NULL) 
00098     return NULL;
00099   if(key == tree->key)
00100     return tree;
00101   else if(compkey(key, tree->key) < 0)
00102     return TreeNodeFind(tree->left, key, compkey);
00103   else 
00104     return TreeNodeFind(tree->right, key, compkey);    
00105 }
00106 
00107 
00108 treeNode* TreeNodeInsert(treeNode* root, treeNode* newnode,
00109             int (*compkey) (void *, void *))
00110 {
00111   treeNode *y = NULL;
00112   treeNode *x = root;
00113   /*going down the tree from the root.
00114    *x traces the path, y is the parent of x.
00115    */
00116   while (x != NULL){
00117     y = x;
00118     if(compkey(newnode->key,x->key) < 0) /*if newnode < x*/
00119       x = x->left;
00120     else 
00121       x = x->right;
00122   }
00123 
00124   /*now y has the property that 
00125    * if newnode < y, then y->left is NULL
00126    * if newnode > y, then y->right is NULL.
00127    *So we want to isnert newnode to be the child of y
00128    */
00129   newnode->parent = y;
00130   if(y == NULL) 
00131     return newnode;
00132   else if( compkey(newnode->key, y->key) <0)
00133     {
00134       y->left = newnode;
00135     }
00136   else
00137     {
00138       y->right = newnode;
00139     }
00140 
00141   return root;
00142 }
00143     
00144 treeNode* TreeNodeDeleteSingleNode(treeNode* tree, treeNode* node)
00145 {
00146   treeNode* y;
00147   treeNode* x;
00148   treeNode* ret;
00149   if(node==NULL) return tree;
00150 
00151   if(node->left == NULL || node->right == NULL) {
00152 
00153     y = node;
00154     if(y->left != NULL) 
00155       x = y->left;
00156     else
00157       x = y->right;
00158 
00159     if( x != NULL)
00160       x->parent = y->parent;
00161 
00162     if(y->parent == NULL) /*y is the root which has at most one child x*/
00163       ret = x;
00164     else /*y is not the root*/
00165       {
00166     if(y == y->parent->left) 
00167       y->parent->left = x;
00168     else
00169       y->parent->right = x;
00170     ret =  tree;
00171       }
00172   }
00173   else { /*node has two children*/
00174 
00175      y = TreeNodeSuccessor(node);
00176      assert(y->left == NULL);
00177 
00178      if(y == node->right) /*y is the right child if node*/
00179        {
00180      y->parent = node->parent;
00181      y->left = node->left;
00182      node->left->parent = y;
00183      
00184        }
00185      else  /*y != node->right*/
00186        {
00187      x = y->right;
00188      if(x!= NULL)
00189        x->parent = y->parent;
00190 
00191      assert(y->parent != NULL);
00192      if(y == y->parent->left) 
00193        y->parent->left = x;
00194      else
00195        y->parent->right = x;
00196      /*move y to the position of node*/
00197      y->parent = node->parent;
00198      y->left = node->left;
00199      y->right = node->right;
00200      node->left->parent = y;
00201      node->right->parent = y;
00202        }
00203     if(node->parent != NULL) {
00204       if(node->parent->left == node)
00205     node->parent->left = y;
00206       else
00207     node->parent->right = y;
00208       ret = tree; /*the root if the tree doesn't change*/
00209     }
00210     else /*node->parent is NULL: node is the root*/
00211       ret = y;    
00212   }
00213 
00214   /*finally free the node, and return the new root*/
00215   TreeNodeDeleteSingleNode(node);
00216   return ret;
00217 }
00218      
00219 
00220 /*the minimum node in the tree rooted by node
00221  */
00222 treeNode* TreeNodeMinimum(treeNode* node)
00223 {
00224   treeNode* temp = node;
00225   if(temp == NULL) return NULL;
00226   while(temp->left != NULL) {
00227     temp = temp->left;
00228   }
00229   return temp;
00230 }
00231 
00232 /*the maximum node in the tree rooted by node
00233  */
00234 treeNode* TreeNodeMaximum(treeNode* node)
00235 {
00236   treeNode* temp = node;
00237   if(temp == NULL) return NULL;
00238   while(temp->right != NULL) {
00239     temp = temp->right;
00240   }
00241   return temp;
00242 }
00243 
00244 /*return the first node (in sorted order) which is to the right of this node
00245  */
00246 treeNode* TreeNodeSuccessor(treeNode* node)
00247 {
00248   if(node == NULL) return NULL;
00249   if(node->right != NULL)
00250     return TreeNodeMinimum(node->right);
00251   else{ /*node->right is NULL*/
00252 
00253     /*find the first right-ancestor*/
00254     treeNode *y = node->parent;
00255     treeNode* x = node;
00256     while(y != NULL && x == y->right) /*if y is a left parent of x*/
00257       {
00258 
00259     x = y;
00260     y = y->parent;
00261       }
00262     return y;
00263   }
00264 }
00265 
00266 /*return the first node (in sorted order) which is to the left of this node
00267  */
00268 treeNode* TreeNodePredecessor(treeNode* node)
00269 {
00270   if(node == NULL) return NULL;
00271   if(node->left != NULL)
00272     return TreeNodeMaximum(node->left);
00273   else{ /*node->left is NULL*/
00274     /*find the first left-ancestor*/
00275     treeNode *y = node->parent;
00276     treeNode *x = node;
00277     while(y != NULL && x == y->left) /*if y is a right parent of x*/
00278       {
00279     x = y;
00280     y = y->parent;
00281       }
00282     return y;
00283   }
00284 }

Generated on Sun May 27 2012 04:23:50 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.