Changeset 55 for trunk/src/helpers/tree.c
- Timestamp:
- Apr 6, 2001, 7:12:11 PM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/tree.c
r54 r55 1097 1097 } 1098 1098 1099 1099 /* 1100 *@@ treeBuildArray: 1101 * builds an array of TREE* pointers containing 1102 * all tree items in sorted order. 1103 * 1104 * This returns a TREE** pointer to the array. 1105 * Each item in the array is a TREE* pointer to 1106 * the respective tree item. 1107 * 1108 * The array has been allocated using malloc() 1109 * and must be free()'d by the caller. 1110 * 1111 * NOTE: This will only work if you maintain a 1112 * tree node count yourself, which you must pass 1113 * in *pulCount on input. 1114 * 1115 * This is most useful if you want to delete an 1116 * entire tree without having to traverse it 1117 * and rebalance the tree on every delete. 1118 * 1119 * Example usage for deletion: 1120 * 1121 + TREE *G_TreeRoot; 1122 + treeInit(&G_TreeRoot); 1123 + 1124 + // add stuff to the tree 1125 + TREE *pNewNode = malloc(...); 1126 + treeInsertID(&G_TreeRoot, pNewNode, FALSE) 1127 + 1128 + // now delete all nodes 1129 + ULONG cItems = ... // insert item count here 1130 + TREE** papNodes = treeBuildArray(G_TreeRoot, 1131 + &cItems); 1132 + if (papNodes) 1133 + { 1134 + ULONG ul; 1135 + for (ul = 0; ul < cItems; ul++) 1136 + { 1137 + TREE *pNodeThis = papNodes[ul]; 1138 + free(pNodeThis); 1139 + } 1140 + 1141 + free(papNodes); 1142 + } 1143 + 1144 * 1145 *@@added V0.9.9 (2001-04-05) [umoeller] 1146 */ 1147 1148 TREE** treeBuildArray(TREE* pRoot, 1149 unsigned long *pulCount) // in: item count, out: array item count 1150 { 1151 TREE **papNodes = NULL, 1152 **papThis = NULL; 1153 unsigned long cb = (sizeof(TREE*) * (*pulCount)), 1154 cNodes = 0; 1155 1156 if (cb) 1157 { 1158 papNodes = (TREE**)malloc(cb); 1159 papThis = papNodes; 1160 1161 if (papNodes) 1162 { 1163 TREE *pNode = (TREE*)treeFirst(pRoot); 1164 1165 memset(papNodes, 0, cb); 1166 1167 // copy nodes to array 1168 while ( pNode 1169 && cNodes < (*pulCount) // just to make sure 1170 ) 1171 { 1172 *papThis = pNode; 1173 cNodes++; 1174 papThis++; 1175 1176 pNode = (TREE*)treeNext(pNode); 1177 } 1178 1179 // output count 1180 *pulCount = cNodes; 1181 } 1182 } 1183 1184 return (papNodes); 1185 } 1186 1187
Note:
See TracChangeset
for help on using the changeset viewer.