Cov txheej txheem:

Yuav ua li cas koj siv binary tshawb nrhiav ntoo hauv Java?
Yuav ua li cas koj siv binary tshawb nrhiav ntoo hauv Java?

Video: Yuav ua li cas koj siv binary tshawb nrhiav ntoo hauv Java?

Video: Yuav ua li cas koj siv binary tshawb nrhiav ntoo hauv Java?
Video: YUAV UA LI CAS LUAG THIAJ SAIB TAUS YUS 2024, Hlis ntuj nqeg
Anonim

Ua tiav Binary Search Tree (BST) hauv Java

  1. Sab laug subtree ntawm ib lub node tsuas muaj cov nodes uas muaj cov yuam sij tsawg dua cov node tus yuam sij.
  2. Txoj cai subtree ntawm ib lub node tsuas muaj cov nodes uas muaj cov yuam sij ntau dua qhov node tus yuam sij.
  3. Sab laug thiab sab xis subtree txhua tus kuj yuav tsum yog a binary nrhiav tsob ntoo .
  4. Yuav tsum tsis muaj cov nodes duplicate.

Cov lus nug tseem yog, yuav ua li cas binary tshawb nrhiav hauv Java?

Cia peb pom ib qho piv txwv ntawm kev tshawb nrhiav binary hauv java qhov twg peb yuav mus tshawb cov khoom los ntawm ib qho array siv recursion

  1. chav kawm BinarySearchExample1{
  2. pej xeem zoo li qub int binarySearch(int arr, int ua ntej, int kawg, int key){
  3. yog tias (kawg> = thawj) {
  4. int mid = first + (last - first)/2;
  5. if (arr[mid] == key){
  6. rov nruab nrab;
  7. }

Qhov thib ob, peb siv binary nrhiav tsob ntoo nyob qhov twg? Binary Nrhiav tsob ntoo - Siv hauv ntau nrhiav cov ntawv thov uas cov ntaub ntawv nkag mus / tawm mus tas li, xws li daim ntawv qhia thiab teeb tsa cov khoom hauv ntau hom lus tsev qiv ntawv. Binary Space Partition - Siv nyob rau hauv yuav luag txhua txhua 3D video game los txiav txim seb yam khoom twg yuav tsum tau ua.

Yog li ntawd, binary ntoo tsim li cas?

Tsim Binary Tsob Ntoo Siv Recursion

  1. Nyeem ib cov ntaub ntawv hauv x.
  2. Muab lub cim xeeb rau ntawm qhov tshiab thiab khaws qhov chaw nyob hauv pointer p.
  3. Khaws cov ntaub ntawv x hauv qhov node p.
  4. Recursively tsim sab laug subtree ntawm p thiab ua rau nws tus me nyuam sab laug ntawm p.
  5. Recursively tsim txoj cai subtree ntawm p thiab ua kom nws tus me nyuam txoj cai p.

Dab tsi yog qhov nyuaj ntawm kev tshawb nrhiav binary?

Binary nrhiav khiav hauv lub sij hawm logarithmic phem tshaj plaws, ua O (log n) kev sib piv, qhov twg n yog tus naj npawb ntawm cov ntsiab lus hauv array, O yog Big O notation, thiab log yog logarithm. Binary nrhiav siv qhov chaw tsis tu ncua (O(1)), txhais tau hais tias qhov chaw coj los ntawm algorithm yog tib yam rau txhua tus lej ntawm cov ntsiab lus hauv array.

Pom zoo: