bool BST(TreeNode* root,long& last) { if(root==nullptr) return true; if(!BST(root->left, last)) return false; if(root->val<=last) return false; else last=root->val; // if(!BST(root->right, last)) return false; // else return true; return BST(root->right, last); }
void BST(TreeNode * & root, vector<int> & num, int low, int high) { if(low > high) return ; int mid = (low + high) / 2; if(!root) { root = new TreeNode(num[mid]); } BST(root->left, num, low, mid-1); BST(root->right, num, mid+1, high); }
TreeNode* BST(vector<int>& nums, int l, int r) { int mid = (l + r) / 2; if (mid < 0 || mid >= nums.size() || l > r) { return 0; } TreeNode* node = new TreeNode(nums[mid]); if (l == r) { return node; } node->left = BST(nums, l, mid - 1); node->right = BST(nums, mid + 1, r); return node; }
struct TreeNode* BST(int* nums,int start,int end){ if(start>end){ return NULL ; }else{ int middle = (start+end)/2 ; struct TreeNode* temp = (struct TreeNode*)malloc(sizeof(struct TreeNode)) ; temp->val = nums[middle] ; temp->left = BST(nums,start,middle-1) ; temp->right = BST(nums,middle+1,end) ; return temp ; } }
/* * USAGE: * hw3 bst [-displayall] [file] * hw3 bst_delete [-displayall] string1 string2 ... stringN file * hw3 avl [-displayall] [file] * hw3 avl_delete [-displayall] string1 string2 ... stringN file * **/ int main (int argc, char *argv[]) { if(argc<2) { fprintf(stderr,"(malformed command)\n"); GUsage(); } if( !strcmp(argv[1],"bst") ) { #ifdef DEBUG1 fprintf(stdout,"----------BST----------------------\n"); #endif BST(argc,argv,0); ///<int operation = 0 = INSERT return 1; } if(!strcmp(argv[1],"bst_delete") ) { #ifdef DEBUG1 fprintf(stdout,"----------BST-------------\n"); #endif BST(argc,argv,1); ///< int operation = 1 = DELETE return 1; } if( !strcmp(argv[1],"avl") ) { #ifdef DEBUG1 fprintf(stdout,"----------AVL----------------------\n"); #endif AVL(argc,argv,0); return 1; } if(!strcmp(argv[1],"avl_delete") ) { #ifdef DEBUG1 fprintf(stdout,"----------AVL-------------\n"); #endif AVL(argc,argv,1); ///< 1 is delete operation return 1; } ///< TEST THE TREES #ifdef DEBUG0 if(!strcmp(argv[argc-1],"-LTest")) { int choice; cout<<"Enter some test case"<<endl; cin>>choice; switch(choice) { //case 1: cout<<"Test:CreateList and print"<<endl;test1(); break;//Just a createlist and print default:cout<<"please enter a valid testcase number"<<endl;break; } return 1; }
TreeNode* sortedArrayToBST(vector<int>& nums) { int n = nums.size(); if (!n) { return 0; } return BST(nums, 0, n - 1); }
static void InquiryResult(BTDEVHDL dev_hdl) { BtDevice dev; BTUINT32 dev_class; BST(Btsdk_GetRemoteDeviceClass(dev_hdl, &dev_class)); BtSdkRemoteDevicePropertyStru props; BST(Btsdk_GetRemoteDeviceProperty(dev_hdl, &props)); byte nameBuf[256]; WORD len = sizeof(nameBuf); BST(Btsdk_GetRemoteDeviceName(dev_hdl, nameBuf, &len)); dev.name = (char*)nameBuf; BST(Btsdk_GetRemoteDeviceAddress(dev_hdl, dev.address.a)); { CriticalSectionHandler csh(&gBt.critSec); LOGBT("d%i: \"%s\" (%s)\n", gBt.devices.size(), dev.name.c_str(), btaddr2string(dev.addr)); gBt.devices.push_back(dev); } gBt.deviceCallback(); }
void TEST_subtree() { std::cout << "TEST whether BST s is BST t's subtree.\n"; int t[] = {1,2,3,4,5,6,7,8,9,10}; int len_t = sizeof(t)/sizeof(t[0]); int s[] = {1,2,3,4}; int len_s = sizeof(s)/sizeof(s[0]); BST bst_t = BST(t, len_t); std::cout << "Tree t: \n"; bst_t.printPretty(); BST bst_s = BST(s, len_s); std::cout << "Tree s: \n"; bst_s.printPretty(); if ( isSubTree(bst_t.getRoot(), bst_s.getRoot()) ) { std::cout << "Tree s is Tree t's subtree.\n"; } else { std::cout << "Tree s is NOT Tree t's subtree.\n"; } }
int main() { char str[1000]; int kase; bool flag=0; scanf("%d",&kase); gets(str); gets(str); while(kase--) { init(); n=0; while(gets(str)&&str[0]!='\0') {BST(str);n++;} if(flag) printf("\n"); flag=1; inorder(root); } return 0; }
/************************************** Main ********************************* PURPOSE: The purpose of the program is to allow users to insert 128k numbers into the BinarySearchTree and SplayTree and calculate the average time for several test cases of inserting 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, and 128000 numbers. The main function first initializes the array to hold numbers from a file called NUM128K.DAT. Then, it calls getMenu() which allows users to choose which algorithm to test or change the test case size. BST and splayTree functions have a timer each to calculate the execution time in nanosecond. The program will also output the time result. DATA TABLE: x...............................LONGLONG type that holds time result m...............................integer type that holds ifstream data size............................integer type that holds the size of the array (test cases) choice..........................integer type that is returned by getMenu() inFile..........................ifstream type that allows to get data from NUM128K.DAT file checkRead.......................boolean type that verifies file is opened properly Frequency.......................timer variable for converting to nanosecond PerformanceCount1...............timer starts PerformanceCount2...............timer stops *****************************************************************************/ int main( ) { LONGLONG x; int m; int size; int choice; ifstream inFile; //char buffer[80]; // initialize array to zero for( int s = 0; s < MAX_SIZE; s++ ) { myArray[s] = 0; } // check if file is opened for read int checkRead = FileOpenRead( inFile ); if( checkRead ) cout << "File Opened Successfully" << endl; else { cout << "File Opened Failed" << endl; exit(0); } // grab data from file into the array while( inFile.good() ) { int s = 0; while( inFile >> m && s < MAX_SIZE ) { myArray[s] = m; s++; } inFile.close(); } cout << "Input test cases of N ( 500 - 128000 ) -> " << flush; cin >> size; while( true ) { choice = getMenu(); switch( choice ) { case 1: // Timer starts QueryPerformanceFrequency( &Frequency ); QueryPerformanceCounter( &PerformanceCount1 ); // Call Binary Search Tree Function BST( size ); // Timer stops QueryPerformanceCounter( &PerformanceCount2 ); // subtract timer from start to end x = PerformanceCount2.QuadPart - PerformanceCount1.QuadPart; // convert to nanosecond unit x = 1000000000*x/Frequency.QuadPart; // output the time result //sprintf( buffer, "Execute time: %I64d\n", x / size ); printf("Execute time: %I64d\n", x / size ); // Empty the tree for the next test t.makeEmpty(); break; case 2: // Timer starts QueryPerformanceFrequency( &Frequency ); QueryPerformanceCounter( &PerformanceCount1 ); // Call SplayTree function splayTree( size ); // Timer stops QueryPerformanceCounter( &PerformanceCount2 ); // subtract timer from start to end x = PerformanceCount2.QuadPart - PerformanceCount1.QuadPart; // convert to nanosecond unit x = 1000000000*x/Frequency.QuadPart; // output the time result //sprintf( buffer, "Execute time: %I64d\n", x / size ); printf("Execute time: %I64d\n", x / size ); // Empty the tree for the next test splay.makeEmpty(); break; case 3: cout << "Input test cases of N ( 500 - 128000 ) -> " << flush; cin >> size; break; case 4: return 0; default: cout << "Invalid input\n"; break; } } return 0; }
LRESULT CSetPgFonts::OnInitDialog(HWND hDlg, bool abInitial) { SetDlgItemText(hDlg, tFontFace, gpFontMgr->FontFaceName()); SetDlgItemText(hDlg, tFontFace2, gpFontMgr->BorderFontFaceName()); if (abInitial) { // Добавить шрифты рисованные ConEmu for (INT_PTR j = 0; j < gpFontMgr->m_RegFonts.size(); ++j) { const RegFont* iter = &(gpFontMgr->m_RegFonts[j]); if (iter->pCustom) { BOOL bMono = iter->pCustom->GetFont(0,0,0,0)->IsMonospace(); int nIdx = SendDlgItemMessage(hDlg, tFontFace, CB_ADDSTRING, 0, (LPARAM)iter->szFontName); SendDlgItemMessage(hDlg, tFontFace, CB_SETITEMDATA, nIdx, bMono ? 1 : 0); nIdx = SendDlgItemMessage(hDlg, tFontFace2, CB_ADDSTRING, 0, (LPARAM)iter->szFontName); SendDlgItemMessage(hDlg, tFontFace2, CB_SETITEMDATA, nIdx, bMono ? 1 : 0); } } CSetDlgFonts::StartEnumFontsThread(); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, tFontSizeY), CSetDlgLists::eFSizesY, gpSet->FontSizeY, true); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, tFontSizeX), CSetDlgLists::eFSizesX, gpSet->FontSizeX, true); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, tFontSizeX2), CSetDlgLists::eFSizesX, gpSet->FontSizeX2, true); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, tFontSizeX3), CSetDlgLists::eFSizesX, gpSet->FontSizeX3, true); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, lbExtendFontBoldIdx), CSetDlgLists::eColorIdx, gpSet->AppStd.nFontBoldColor, false); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, lbExtendFontItalicIdx), CSetDlgLists::eColorIdx, gpSet->AppStd.nFontItalicColor, false); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, lbExtendFontNormalIdx), CSetDlgLists::eColorIdx, gpSet->AppStd.nFontNormalColor, false); CSetDlgLists::FillListBoxItems(GetDlgItem(hDlg, tFontCharset), CSetDlgLists::eCharSets, gpFontMgr->LogFont.lfCharSet, false); } else { //TODO: Обновить значения в списках? } checkDlgButton(hDlg, cbExtendFonts, gpSet->AppStd.isExtendFonts); CSetDlgLists::EnableDlgItems(hDlg, CSetDlgLists::eExtendFonts, gpSet->AppStd.isExtendFonts); checkDlgButton(hDlg, cbFontAuto, gpSet->isFontAutoSize); MCHKHEAP checkRadioButton(hDlg, rNoneAA, rCTAA, (gpFontMgr->LogFont.lfQuality == CLEARTYPE_NATURAL_QUALITY) ? rCTAA : (gpFontMgr->LogFont.lfQuality == ANTIALIASED_QUALITY) ? rStandardAA : rNoneAA); // 3d state - force center symbols in cells checkDlgButton(hDlg, cbMonospace, BST(gpSet->isMonospace)); checkDlgButton(hDlg, cbBold, (gpFontMgr->LogFont.lfWeight == FW_BOLD) ? BST_CHECKED : BST_UNCHECKED); checkDlgButton(hDlg, cbItalic, gpFontMgr->LogFont.lfItalic ? BST_CHECKED : BST_UNCHECKED); /* Alternative font, initially created for prettifying Far Manager borders */ { checkDlgButton(hDlg, cbFixFarBorders, BST(gpSet->isFixFarBorders)); checkDlgButton(hDlg, cbFont2AA, gpSet->isAntiAlias2 ? BST_CHECKED : BST_UNCHECKED); LPCWSTR cszFontRanges[] = { L"Far Manager borders: 2500-25C4;", L"Dashes and Borders: 2013-2015;2500-25C4;", L"Pseudographics: 2013-25C4;", L"CJK: 2E80-9FC3;AC00-D7A3;F900-FAFF;FE30-FE4F;FF01-FF60;FFE0-FFE6;", NULL }; CEStr szCharRanges(gpSet->CreateCharRanges(gpSet->mpc_CharAltFontRanges)); LPCWSTR pszCurrentRange = szCharRanges.ms_Val; bool bExist = false; HWND hCombo = GetDlgItem(hDlg, tUnicodeRanges); SendDlgItemMessage(hDlg, tUnicodeRanges, CB_RESETCONTENT, 0,0); // Fill our drop down with font ranges for (INT_PTR i = 0; cszFontRanges[i]; i++) { LPCWSTR pszRange = wcsstr(cszFontRanges[i], L": "); if (!pszRange) { _ASSERTE(pszRange); continue; } SendMessageW(hCombo, CB_ADDSTRING, 0, (LPARAM)cszFontRanges[i]); if (!bExist && (lstrcmpi(pszRange+2, pszCurrentRange) == 0)) { pszCurrentRange = cszFontRanges[i]; bExist = true; } } if (pszCurrentRange && *pszCurrentRange && !bExist) SendMessageW(hCombo, CB_ADDSTRING, 0, (LPARAM)pszCurrentRange); // And show current value SetWindowText(hCombo, pszCurrentRange ? pszCurrentRange : L""); } /* Alternative font ends */ checkDlgButton(hDlg, cbFontMonitorDpi, gpSet->FontUseDpi ? BST_CHECKED : BST_UNCHECKED); checkDlgButton(hDlg, cbFontAsDeviceUnits, gpSet->FontUseUnits ? BST_CHECKED : BST_UNCHECKED); gpSetCls->mn_LastChangingFontCtrlId = 0; return 0; }
uint8_t spi_recv(void) { led_on(); while (!BST(SPSR, SPIF)) { } led_off(); return SPDR; }
void uart_write(uint8_t val) { while (!BST(UCSR0A, UDRE0)) { } UDR0 = val; }
TreeNode *sortedArrayToBST(vector<int> &num) { TreeNode * root = NULL; BST(root, num, 0, num.size()-1); return root; }
bool isValidBST(TreeNode* root) { // in-order traverse to check the order. // no need to store the array just need previous one and compare---> brilliant idea. time n, space 1.. long last=LONG_MIN; return BST(root, last); }
struct TreeNode* sortedArrayToBST(int* nums,int numsSize){ return BST(nums,0,numsSize-1) ; }
void Soleil::startDeviceDiscovery() { BST(Btsdk_StartDeviceDiscovery(0, 0, 0)); }
uint8_t uart_read(void) { led_on(); while (!BST(UCSR0A, RXC0)) { } led_off(); return UDR0; }
void spi_send(uint8_t val) { SPDR = val; while (!BST(SPSR, SPIF)) { } }
BST<KeyType> PRIORITY_QUEUE_ON_BINARY_SEARCH_TREE<KeyType>::getTree(void) const{ return BST(*tree_); }