Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

tsplay.c

Go to the documentation of this file.
00001 /*++ 00002 00003 Copyright (c) 1989 Microsoft Corporation 00004 00005 Module Name: 00006 00007 tsplay.c 00008 00009 Abstract: 00010 00011 Test program for the Splay Procedures 00012 00013 Author: 00014 00015 Gary Kimura [GaryKi] 24-May-1989 00016 00017 Revision History: 00018 00019 --*/ 00020 00021 #include <stdio.h> 00022 00023 #include "nt.h" 00024 #include "ntrtl.h" 00025 00026 ULONG RtlRandom ( IN OUT PULONG Seed ); 00027 00028 typedef struct _TREE_NODE { 00029 CLONG Data; 00030 RTL_SPLAY_LINKS Links; 00031 } TREE_NODE; 00032 typedef TREE_NODE *PTREE_NODE; 00033 00034 TREE_NODE Buffer[2048]; 00035 00036 PTREE_NODE 00037 TreeInsert ( 00038 IN PTREE_NODE Root, 00039 IN PTREE_NODE Node 00040 ); 00041 00042 VOID 00043 PrintTree ( 00044 IN PTREE_NODE Node 00045 ); 00046 00047 int 00048 _CDECL 00049 main( 00050 int argc, 00051 char *argv[] 00052 ) 00053 { 00054 PTREE_NODE Root; 00055 ULONG i; 00056 ULONG Seed; 00057 00058 DbgPrint("Start SplayTest()\n"); 00059 00060 Root = NULL; 00061 Seed = 0; 00062 for (i=0; i<2048; i++) { 00063 Buffer[i].Data = RtlRandom(&Seed); 00064 Buffer[i].Data = Buffer[i].Data % 512; 00065 RtlInitializeSplayLinks(&Buffer[i].Links); 00066 Root = TreeInsert(Root, &Buffer[i]); 00067 } 00068 00069 PrintTree(Root); 00070 00071 DbgPrint("End SplayTest()\n"); 00072 00073 return TRUE; 00074 00075 } 00076 00077 PTREE_NODE 00078 TreeInsert ( 00079 IN PTREE_NODE Root, 00080 IN PTREE_NODE Node 00081 ) 00082 00083 { 00084 PRTL_SPLAY_LINKS Temp; 00085 00086 if (Root == NULL) { 00087 00088 //DbgPrint("Add as root %u\n", Node->Data); 00089 return Node; 00090 00091 } 00092 00093 while (TRUE) { 00094 00095 if (Root->Data == Node->Data) { 00096 00097 //DbgPrint("Delete %u\n", Node->Data); 00098 00099 Temp = RtlDelete(&Root->Links); 00100 if (Temp == NULL) { 00101 return NULL; 00102 } else { 00103 return CONTAINING_RECORD(Temp, TREE_NODE, Links); 00104 } 00105 00106 } 00107 00108 if (Root->Data < Node->Data) { 00109 00110 // 00111 // Go right 00112 // 00113 00114 if (RtlRightChild(&Root->Links) == NULL) { 00115 00116 //DbgPrint("Add as right child %u\n", Node->Data); 00117 RtlInsertAsRightChild(&Root->Links, &Node->Links); 00118 return CONTAINING_RECORD(RtlSplay(&Node->Links), TREE_NODE, Links); 00119 00120 } else { 00121 00122 Root = CONTAINING_RECORD(RtlRightChild(&Root->Links), TREE_NODE, Links); 00123 00124 } 00125 00126 } else { 00127 00128 // 00129 // Go Left 00130 // 00131 00132 if (RtlLeftChild(&Root->Links) == NULL) { 00133 00134 //DbgPrint("Add as left child %u\n", Node->Data); 00135 RtlInsertAsLeftChild(&Root->Links, &Node->Links); 00136 return CONTAINING_RECORD(RtlSplay(&Node->Links), TREE_NODE, Links); 00137 00138 } else { 00139 00140 Root = CONTAINING_RECORD(RtlLeftChild(&Root->Links), TREE_NODE, Links); 00141 00142 } 00143 00144 } 00145 } 00146 } 00147 00148 VOID 00149 PrintTree ( 00150 IN PTREE_NODE Node 00151 ) 00152 00153 { 00154 PRTL_SPLAY_LINKS Temp; 00155 ULONG LastValue; 00156 00157 if (Node == NULL) { 00158 return; 00159 } 00160 00161 // 00162 // find smallest value 00163 // 00164 00165 while (RtlLeftChild(&Node->Links) != NULL) { 00166 Node = CONTAINING_RECORD(RtlLeftChild(&Node->Links), TREE_NODE, Links); 00167 } 00168 LastValue = Node->Data; 00169 //DbgPrint("%u\n", Node->Data); 00170 00171 // 00172 // while the is a real successor we print the successor value 00173 // 00174 00175 while ((Temp = RtlRealSuccessor(&Node->Links)) != NULL) { 00176 Node = CONTAINING_RECORD(Temp, TREE_NODE, Links); 00177 if (LastValue >= Node->Data) { 00178 DbgPrint("TestSplay Error\n"); 00179 } 00180 LastValue = Node->Data; 00181 //DbgPrint("%u\n", Node->Data); 00182 } 00183 00184 }

Generated on Sat May 15 19:42:05 2004 for test by doxygen 1.3.7