static RET_CODE search_start(control_t *p_ctrl, u16 msg,u32 para1, u32 para2) { ui_scan_param_t scan_param; memset(&scan_param, 0, sizeof(ui_scan_param_t)); get_scan_param(ctrl_get_parent(p_ctrl), &scan_param); if(SCAN_TYPE_RANGE == get_search_type()) range_search(ctrl_get_parent(p_ctrl), &scan_param); else manage_open_menu(ROOT_ID_DO_SEARCH, get_search_type(), (u32)&scan_param); return SUCCESS; }
// This function operates a search on a node for points within // the specified range. // It assumes the current node is at a depth corresponding to // dimension "dim" int KDTree::range_search(int nodeIdx, Range * range, int dim) { if (nodeIdx < 0 || nPntsInRange > maxPoints ) return 0; struct KDTreeNode *node = m_Root+nodeIdx; // If this is a leaf node, check to see if the // data is in range. If so, operate on it. if (node->pntidx>=0) { // Return if not in range for (int i = 0; i < ndim; i++) { int idx = node->pntidx*ndim + i; if (points[idx] < range[i][0] || points[idx] > range[i][1]) return 0; } pntsInRange[nPntsInRange++] = node->pntidx; return 0; } // Search left, if necessary if( rand()%2 == 0 ) { if (node->key >= range[dim][0]) range_search(node->leftIdx, range, (dim + 1) % ndim); // Search right,if necessary if (node->key <= range[dim][1]) range_search(node->rightIdx, range, (dim + 1) % ndim); }else //reverse search { // Search right,if necessary if (node->key <= range[dim][1]) range_search(node->rightIdx, range, (dim + 1) % ndim); if (node->key >= range[dim][0]) range_search(node->leftIdx, range, (dim + 1) % ndim); } return 0; } // end of range_search
main() { /*Declarations*/ tree tr; status_code SC=SUCCESS; int ht,num,option,contnue,num_lo=0,num_hi=0; char dumbo[2]; int number; char name[NAME_LEN]; char dsgn[DSGN_LEN]; char add[ADD_LEN]; long int phone; initialize(&tr); /*Declarations End*/ do { /* Asking for different operations */ puts("ENTER the option as per the operation you want to do. ENTER :-\n"); puts("1- insert/update\n2- delete\n3- Search\n4- getNumRecords\n5- Height\n6- Range Search\n7- Print Database\n\n"); printf("Your Choice is:- "); scanf("%d",&option); printf("\n\n"); switch (option) { /* INSERT/UPDATE */ case 1: { printf("***********INSERT**********\n\n"); /* Data Entering Start */ printf("Enter Employee's Number :-\t"); scanf("%d",&number); printf("Enter Employee Name :-\t"); gets(dumbo); gets(name); remove_space_make_uppercase(name); printf("Enter Employee's Designation :-\t"); gets(dsgn); remove_space_make_uppercase(dsgn); printf("Enter Employee's Address :-\t"); gets(add); remove_space_make_uppercase(add); printf("Enter Employee's Phone Number :-\t"); scanf("%lu",&phone); /* Data Entering End */ /* Inserting/Updating Data */ SC=insert_emp(&tr,number,name,dsgn,add,phone); if(SC==SUCCESS) { puts("\n**********Data inserted**********\n"); } else { puts("\n**********Data insertion failed**********\n"); } break; } /*Search*/ case 3: { printf("Enter Employee's Number :-\t"); scanf("%d",&number); search(&tr,number); break; } /* Get Num Records */ case 4: { num=get_num_records(&tr); printf("Number of records present in the tree is %d",num); break; } /* Height of the tree */ case 5: { ht=height(&tr); printf("Height of the tree is %d",ht); break; } /* Range Search */ case 6: { printf("\nEnter lower bound :- \t"); scanf("%d",&num_lo); printf("\nEnter upper bound :- \t"); scanf("%d",&num_hi); if(num_lo<num_hi) { range_search(&tr,num_lo,num_hi); } else { printf("Please enter valid bounds.\n"); } break; } /* Print Database */ case 7: { print_database(&tr); break; } /* Default */ default: { break; } } /* Asking for CHOICE to Continue */ puts("\n\nIf you want to continue... ? Enter 1 if YES or 0 if NO\n"); printf("Your Choice is:- "); scanf("%d",&contnue); printf("\n\n"); }while(contnue==1); getch(); }