Binary tshawb nrhiav tsob ntoo - cov txheej txheem database uas muaj cov nodes, ob qhov txuas mus rau lwm cov nodes, sab xis thiab sab laug. Nodes yog ib yam khoom ntawm chav kawm uas muaj cov ntaub ntawv, thiab NULL yog lub cim uas cim qhov kawg ntawm tsob ntoo.
Nws feem ntau hu ua BST, uas muaj cov cuab yeej tshwj xeeb: cov nodes loj dua cov hauv paus hniav nyob rau sab xis ntawm nws, thiab cov me me rau sab laug.
kev xav dav dav thiab cov ntsiab lus
Nyob rau hauv ib tsob ntoo tshawb nrhiav binary, txhua lub ntawm, tsis suav nrog hauv paus, txuas nrog los ntawm ib qho taw qhia ntug ntawm ib qho mus rau lwm qhov, uas yog hu ua niam txiv. Txhua tus ntawm lawv tuaj yeem txuas nrog tus naj npawb ntawm cov nodes, hu ua menyuam yaus. Nodes tsis muaj "cov me nyuam" yog hu ua nplooj (sab nraud nodes). Cov ntsiab lus uas tsis yog nplooj yog hu ua sab hauv. Nodes nrog tib niam txiv yog cov kwv tij. Qhov saum toj kawg nkaus node hu ua lub hauv paus. Hauv BST, muab ib qho khoom rau txhua lub ntawm thiab xyuas kom lawv muaj cov cuab yeej tshwj xeeb rau lawv.
Tub ntoo terminology:
- Qhov tob ntawm ib qho yog tus lej ntawm cov npoo txhais los ntawm lub hauv paus mus rau nws.
- Qhov siab ntawm lub pob yog tus lej ntawm cov npoo txhais los ntawm nws mus rau nplooj tob tshaj plaws.
- Qhov siab ntawm tsob ntoo yog txiav txim siab ntawm qhov siab ntawm lub hauv paus.
- Binary nrhiav tsob ntoo yog qhov tsim tshwj xeeb, nws muab qhov zoo tshaj plaws piv ntawm qhov siab thiab tus naj npawb ntawm cov nodes.
- siab h nrog N ntawm feem ntau O (log N).
Koj tuaj yeem ua pov thawj qhov no yooj yim los ntawm kev suav cov nodes ntawm txhua qib, pib ntawm lub hauv paus, xav tias nws muaj cov lej loj tshaj plaws: n=1 + 2 + 4 + … + 2 h-1 + 2 h=2 h + 1 - 1 Kev daws qhov no rau h muab h=O (log n).
Nyob zoo ntawm ntoo:
- Reflect cov qauv kev sib raug zoo ntawm cov ntaub ntawv.
- Siv los sawv cev hierarchies.
- Ua kom muaj txiaj ntsig zoo thiab tshawb nrhiav.
- Ntaus yog cov ntaub ntawv hloov tau yooj yim, tso cai rau koj txav subtrees nrog kev siv zog tsawg.
Search method
Feem ntau, txhawm rau txiav txim siab seb tus nqi puas nyob hauv BST, pib ib tsob ntoo tshawb nrhiav binary ntawm nws cov hauv paus thiab txiav txim siab seb nws ua tau raws li qhov yuav tsum tau ua:
- be ntawm hauv paus;
- be in the laug subtree of root;
- nyob rau hauv txoj cai subtree ntawm cag.
Yog tias tsis muaj lub hauv paus sau npe txaus siab, qhov kev tshawb nrhiav rov ua dua yog ua nyob rau hauv cov ntoo subtree. Muaj ob txoj hauv kev xaiv:
- tsob ntoo khoob - rov cuav.
- Tus nqi yog nyob rau hauv lub hauv paus node - rov muaj tseeb.
Nws yuav tsum tau muab sau tseg tias ib tsob ntoo tshawb nrhiav binary nrog cov qauv tsim ib txwm pib tshawb nrhiav raws txoj hauv paus mus rau nplooj. Qhov phem tshaj plaws, nws mus tag nrho txoj kev mus rau nplooj. Yog li ntawd, lub sijhawm phem tshaj plaws yog proportional rau qhov ntev ntawm txoj kev ntev tshaj plaws los ntawm cov hauv paus mus rau nplooj, uas yog qhov siab ntawm tsob ntoo. Feem ntau, qhov no yog thaum koj yuav tsum paub tias nws yuav siv sij hawm ntev npaum li cas los saib ua haujlwm ntawm tus naj npawb ntawm cov txiaj ntsig khaws cia hauv tsob ntoo.
Hauv lwm lo lus, muaj kev sib raug zoo ntawm tus lej ntawm cov pob hauv BST thiab qhov siab ntawm tsob ntoo, nyob ntawm nws "zoo". Qhov phem tshaj plaws, cov nodes tsuas muaj ib tug me nyuam xwb, thiab ib tsob ntoo nrhiav binary sib npaug yog qhov tseem ceeb ntawm cov npe txuas. Piv txwv li:
50
/
10
15
30
/
20
tsob ntoo no muaj 5 nodes thiab qhov siab=5. Nrhiav qhov muaj nuj nqis nyob rau hauv thaj tsam 16-19 thiab 21-29 yuav tsum muaj txoj hauv kev hauv qab no los ntawm cov hauv paus mus rau nplooj (qhov node muaj tus nqi 20), i.e., nws yuav siv sij hawm proportional rau tus naj npawb ntawm cov nodes. Qhov zoo tshaj, lawv muaj 2 tug me nyuam, thiab cov nplooj nyob rau tib qhov tob.
tsob ntoo nrhiav binary no muaj 7 nodes thiab qhov siab=3. Feem ntau, ib tsob ntoo zoo li no (tag nrho tsob ntoo) yuav muaj qhov siab ntawm kwv yees li 2 (N), qhov N yog cov naj npawb ntawm cov ntoo.. Tus nqi ntawm lub log 2 (N) yog tus lej ntawm lub sijhawm (2) uas N tau muab faib ua ntej xoom mus txog.
Summarizing: lub sijhawm phem tshaj uas yuav tsum tau tshawb hauv BST yog O (ntoo qhov siab). Qhov phem tshaj plaws "linear" tsob ntoo yog O (N), qhov twg N yog tus naj npawb ntawm cov kab hauv tsob ntoo. Qhov zoo tshaj plaws, tsob ntoo "ua tiav" yog O (log N).
BST binary insert
xav qhov twg yuav tsumcov node tshiab nyob rau hauv BST, koj yuav tsum to taub lub logic, nws yuav tsum tau muab tso rau qhov twg cov neeg siv pom nws. Ntxiv rau, koj yuav tsum nco ntsoov cov cai:
- Txwv tsis pub luam tawm, sim ntxig tus nqi sib npaug yuav cuam tshuam qhov kev zam.
- Txoj kev tso rau pej xeem siv tus neeg pab cuam rov "pab" txoj hauv kev los ntxig.
- Ib tus ntawm uas muaj tus nqi tshiab yog ib txwm muab tso rau hauv nplooj hauv BST.
- Txoj kev ntxig rau pej xeem rov qab tsis muaj dab tsi, tab sis txoj kev pab cuam rov qab los ntawm BSTnode. Nws ua qhov no los daws cov ntaub ntawv uas qhov node dhau mus rau nws yog null.
Feem ntau, txoj kev pab qhia tau hais tias yog thawj binary tshawb nrhiav ntoo yog khoob, qhov tshwm sim yog tsob ntoo nrog ib qho. Txwv tsis pub, qhov tshwm sim yuav yog tus taw tes rau tib lub node uas tau dhau los ua kev sib cav.
Kev tshem tawm hauv binary algorithm
Raws li koj xav tau, kev rho tawm ib lub ntsiab lus cuam tshuam nrog kev nrhiav ib lub pob uas muaj tus nqi yuav raug tshem tawm. Muaj ob peb yam hauv no code:
- BST siv tus pab cuam, overloaded rho tawm txoj kev. Yog tias lub caij koj tab tom nrhiav tsis nyob hauv tsob ntoo, ces txoj kev pab yuav nws thiaj li raug hu nrog n==null. Qhov no tsis yog suav tias yog qhov yuam kev, tsob ntoo tsuas yog tsis hloov qhov no. Txoj kev tshem tawm tus pab cuam rov qab tus nqi - tus taw tes rau tsob ntoo tshiab.
- Thaum ib nplooj raug tshem tawm, qhov kev tshem tawm ntawm tsob ntoo tshawb nrhiav binary teeb tsa tus taw qhia tus menyuam sib raug ntawm nws niam nws txiv mus rau null, lossis lub hauv paus rau null yog tias tus raug tshem tawm yoglub pob yog cag thiab tsis muaj me nyuam.
- Nco ntsoov tias kev rho tawm hu yuav tsum yog ib qho hauv qab no: hauv paus=rho tawm (hauv paus, qhov tseem ceeb), n.setLeft (delete (n.getLeft (), qhov tseem ceeb)), n.setRight (delete(n. getRight(), key)). Yog li, nyob rau hauv tag nrho peb kis nws yog qhov tseeb tias txoj kev tshem tawm tsuas yog rov qab null.
- Thaum kev tshawb nrhiav rau ntawm qhov uas muaj tus nqi yuav raug rho tawm ua tiav, muaj peb txoj hauv kev: lub pob yuav tsum tau muab tshem tawm yog nplooj (tsis muaj menyuam), cov ntawm yuav raug rho tawm muaj ib tus menyuam, nws muaj ob me nyuam.
- Thaum lub pob raug tshem tawm muaj ib tus menyuam, koj tuaj yeem hloov nws nrog tus menyuam, rov qab tus taw tes rau tus menyuam.
- Yog hais tias lub node yuav deleted muaj pes tsawg los yog 1 me nyuam, ces tus rho tawm txoj kev yuav "ua raws li txoj kev" los ntawm lub hauv paus mus rau lub node. Yog li lub sij hawm phem tshaj yog proportional rau qhov siab ntawm tsob ntoo, rau ob qho tib si tshawb thiab ntxig.
Yog hais tias lub node yuav raug tshem tawm muaj ob tug me nyuam, cov kauj ruam hauv qab no tau ua:
- Nrhiav cov node kom raug tshem tawm, ua raws li txoj hauv kev los ntawm lub hauv paus mus rau nws.
- Nrhiav tus nqi me tshaj ntawm v nyob rau hauv txoj cai subtree, txuas ntxiv raws txoj kev mus rau nplooj.
- Recursively tshem tawm tus nqi v, ua raws li txoj hauv kev zoo li hauv kauj ruam 2.
- Yog li, qhov phem tshaj, txoj kev ntawm cov cag mus rau nplooj yog ua ob zaug.
Kev hla dhau
Kev hla mus yog txheej txheem uas mus ntsib txhua qhov ntawm tsob ntoo. Vim tias C binary tshawb nrhiav tsob ntoo yog cov ntaub ntawv tsis-linear, tsis muaj kev sib txawv tshwj xeeb. Piv txwv li, qee zaum ob peb traversal algorithmsmuab faib ua ob hom hauv qab no:
- hla qhov tob;
- first pass.
Tsuas muaj ib hom dav hla - hla qib. Qhov kev mus ntsib nodes theem qis thiab sab laug, sab saud thiab sab xis.
Muaj peb yam sib txawv ntawm qhov tob hla:
- Passing PreOrder - thawj zaug mus ntsib niam txiv thiab tom qab ntawd tus menyuam sab laug thiab sab xis.
- Passing InOrder - mus saib tus me nyuam sab laug, ces niam txiv thiab tus me nyuam txoj cai.
- Post Order - mus saib tus me nyuam sab laug, ces tus me nyuam txoj cai, ces niam txiv.
Piv txwv rau plaub txoj kev taug kev ntawm tsob ntoo nrhiav binary:
- PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3.
- InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11.
- PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8.
- LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2.
Daim duab qhia qhov kev txiav txim uas cov nodes tau mus xyuas. Tus naj npawb 1 yog thawj qhov ntawm ib qho kev sib tw, thiab 7 yog qhov kawg.
Cov kev taug kev dav dav no tuaj yeem sawv cev ua ib qho kev sib tw, piv txwv tias txhua qhov node tau mus xyuas peb zaug. Kev ncig xyuas Euler yog taug kev ncig ib tsob ntoo binary uas txhua ntug tau kho raws li phab ntsa uas tus neeg siv tsis tuaj yeem hla. Hauv kev taug kev no, txhua qhov ntawm yuav raug mus xyuas sab laug, lossis hauv qab, lossis sab xis. Lub Euler ncig saib, uas mus saib cov nodes ntawm sab laug, ua rau lub preposition yuav bypassed. Thaum cov nodes hauv qab no mus xyuas, lawv tau traversed nyob rau hauv kev txiav txim. Thiab thaum cov nodes ntawm sab xis mus xyuas - taustep-by-step bypass.
Navigation thiab Debugging
Yuav ua kom yooj yim rau kev taw qhia tsob ntoo, tsim cov haujlwm ua ntej kuaj seb lawv yog tus menyuam sab laug lossis sab xis. Txhawm rau hloov txoj hauj lwm ntawm lub node, yuav tsum muaj kev nkag tau yooj yim rau tus taw tes ntawm tus niam txiv node. Kev siv tsob ntoo kom raug yog qhov nyuaj heev, yog li koj yuav tsum paub thiab siv cov txheej txheem debugging. Ib tsob ntoo tshawb nrhiav binary nrog kev siv feem ntau muaj cov taw qhia uas tsis tau qhia txog kev mus ncig.
txhawm rau txheeb xyuas qhov no, ib qho haujlwm yog siv los kuaj xyuas seb tsob ntoo puas tuaj yeem raug, thiab pab nrhiav ntau qhov yuam kev. Piv txwv li, nws xyuas seb cov niam txiv node yog tus menyuam node. Nrog kev lees paub (is_wellformed (hauv paus)) ntau qhov yuam kev tuaj yeem ntes tau ntxov. Siv ob peb lub ntsiab lus hauv qhov kev ua haujlwm no, koj tuaj yeem txiav txim siab qhov twg tus taw tes tsis raug.
Function Konsolenausgabe
Qhov kev ua haujlwm no ntws tag nrho tsob ntoo mus rau lub console thiab yog li muaj txiaj ntsig zoo. Qhov kev txiav txim uas tsob ntoo tso tawm lub hom phiaj raug tua yog:
- Yuav ua li cas, ua ntej koj yuav tsum txiav txim siab seb cov ntaub ntawv twg yuav raug tso tawm los ntawm lub node.
- Thiab koj kuj yuav tsum paub tias tsob ntoo dav thiab siab npaum li cas txhawm rau suav tias qhov chaw tawm mus ntau npaum li cas.
- Cov haujlwm hauv qab no suav cov ntaub ntawv no rau tsob ntoo thiab txhua tsob ntoo. Txij li thaum koj tsuas tuaj yeem sau rau console kab los ntawm kab, koj tseem yuav tau luam cov kab ntoo los ntawm kab.
- Tam sim no peb xav tau lwm txoj hauv kev thim rov qabtag nrho tsob ntoo, tsis yog kab ntawm kab.
- Nrog kev pab ntawm cov pov tseg muaj nuj nqi, koj tuaj yeem nyeem cov ntoo thiab txhim kho cov txiaj ntsig zoo, kom deb li deb tau txhawj xeeb.
Txawm li cas los xij, txoj haujlwm no yuav nyuaj rau siv ntawm cov ntoo loj.
Copy constructor thiab destructor
Vim tsob ntoo tsis yog cov qauv ntaub ntawv tsis tseem ceeb, nws yog qhov zoo dua los siv tus qauv tsim, tus rhuav tshem, thiab tus neeg ua haujlwm. Tus destructor yog ib qho yooj yim los siv recursively. Rau cov ntoo loj heev, nws tuaj yeem ua "heap overflow". Nyob rau hauv cov ntaub ntawv no, nws yog formulated teratively. Lub tswv yim yog tshem tawm cov nplooj uas sawv cev rau qhov tsawg tshaj plaws ntawm tag nrho cov nplooj, yog li nws yog nyob rau sab laug ntawm tsob ntoo. Txiav tawm thawj nplooj yuav tsim cov tshiab, thiab tsob ntoo shrinks mus txog thaum kawg tsis muaj.
Tus qauv tsim kuj tuaj yeem siv rov ua dua, tab sis ceev faj yog tias muaj qhov tshwj xeeb pov tseg. Txwv tsis pub, tsob ntoo sai sai dhau los ua qhov tsis meej pem thiab ua yuam kev. Yog vim li cas qhov iteration version yog nyiam. Lub tswv yim yog mus hla tsob ntoo qub thiab tsob ntoo tshiab, raws li koj xav nyob rau hauv lub destructor, cloning tag nrho cov nodes uas nyob rau hauv cov ntoo qub tab sis tsis yog cov tshiab.
Nrog rau txoj kev no, kev siv binary tshawb nrhiav tsob ntoo yog ib txwm nyob hauv lub xeev noj qab haus huv thiab tuaj yeem raug tshem tawm los ntawm tus destructor txawm nyob rau hauv lub xeev tsis tiav. Yog tias muaj kev zam, txhua yam koj yuav tsum tau ua yog qhia tus destructor kom tshem tawm cov ntoo ib nrab. assignment operatortuaj yeem siv tau yooj yim siv Copy & Swap.
Creating a binary search tree
Qhov zoo tshaj plaws binary tshawb nrhiav ntoo yog qhov ua tau zoo kawg yog tias tswj tau zoo. Qee cov cai rau binary nrhiav ntoo:
- Ib tug niam txiv node muaj ntau tshaj 2 tus me nyuam nodes.
- Tus me nyuam ntawm sab laug ib txwm tsawg dua tus niam txiv node.
- Ib tug me nyuam node siv tau yog ib txwm loj dua los yog sib npaug rau niam txiv node.
Cov array uas yuav siv los tsim binary tshawb nrhiav ntoo:
- Ib lub hauv paus integer array ntawm xya qhov tseem ceeb hauv kev txiav txim tsis raug.
- thawj tus nqi hauv qhov array yog 10, yog li thawj kauj ruam hauv kev tsim tsob ntoo yog tsim kom muaj 10 lub hauv paus, raws li qhia ntawm no.
- Nrog cov txheej txheem hauv paus, tag nrho lwm cov txiaj ntsig yuav yog menyuam yaus ntawm node. Hais txog kev cai, thawj kauj ruam yuav tsum tau muab ntxiv 7 rau tsob ntoo yog muab piv rau hauv paus node.
- Yog tus nqi 7 tsawg dua 10, nws yuav dhau los ua tus menyuam ntawm sab laug.
- Yog tus nqi 7 ntau dua lossis sib npaug rau 10, nws yuav txav mus rau sab xis. Txij li thaum 7 paub tias tsawg dua 10, nws raug xaiv los ua tus menyuam ntawm sab laug.
- Recursively ua kev sib piv rau txhua lub ntsiab.
- Ua raws li tus qauv zoo ib yam, ua tib qho kev sib piv nrog tus nqi 14 hauv qhov array.
- Muab piv tus nqi 14 rau hauv paus ntawm 10, paub tias 14 yog tus menyuam raug.
- Taug kev ntawm array,tuaj rau 20.
- Pib los ntawm kev sib piv cov array nrog 10, qhov twg ntau dua. Yog li txav mus rau sab xis thiab muab piv rau 14, nws muaj 14 xyoos thiab tsis muaj menyuam nyob sab xis.
- Tam sim no muaj ib qho nqi ntawm 1. Ua raws li tus qauv zoo ib yam li lwm tus nqi, piv 1 txog 10, txav mus rau sab laug thiab muab piv rau 7 thiab thaum kawg mus rau 1 sab laug tus me nyuam ntawm 7th node.
- Yog tus nqi yog 5, muab piv rau 10. Vim tias 5 tsawg dua 10, hla mus rau sab laug thiab muab piv rau 7.
- Xav paub tias 5 tsawg dua 7, txuas ntxiv nqes tsob ntoo thiab piv 5 nrog 1 tus nqi.
- Yog 1 tsis muaj me nyuam thiab 5 loj dua 1, ces 5 yog ib tug me nyuam siv tau ntawm 1 node.
- Kawg tso tus nqi 8 rau hauv tsob ntoo.
- Thaum 8 tsawg dua 10, txav mus rau sab laug thiab muab piv rau 7, 8 yog siab dua 7, yog li txav mus rau sab xis thiab ua tiav tsob ntoo, ua 8 tus me nyuam 7.
Tau txais thiab ntsuas qhov yooj yim elegance ntawm cov ntoo tshawb nrhiav binary zoo. Zoo li ntau lub ntsiab lus hauv kev ua haujlwm, lub zog ntawm binary tshawb nrhiav ntoo los ntawm lawv lub peev xwm los daws cov ntaub ntawv rau hauv cov khoom me me. Txij no mus, koj tuaj yeem ua haujlwm nrog cov ntaub ntawv tag nrho hauv txoj hauv kev.
Muaj peev xwm Binary Nrhiav Teeb meem
Binary tshawb nrhiav ntoo yog qhov zoo, tab sis muaj ob peb lub ntsiab lus kom nco ntsoov. Lawv feem ntau tsuas yog siv tau yog tias lawv sib npaug. Ib tsob ntoo sib npaug yog tsob ntoo uasqhov sib txawv ntawm qhov siab ntawm subtrees ntawm ib qho ntawm cov ntoo yog nyob rau ntawm ib qho. Cia peb saib ib qho piv txwv uas yuav pab qhia meej txog cov cai. Cia peb xav txog tias cov array pib li sortable.
Yog tias koj sim khiav binary tshawb nrhiav ntoo algorithm ntawm tsob ntoo no, nws yuav ua raws nraim li nws tsuas yog iterating los ntawm cov array kom txog thaum pom tus nqi xav tau. Lub zog ntawm kev tshawb nrhiav binary nyob hauv lub peev xwm los lim tawm cov txiaj ntsig tsis xav tau sai. Thaum tsob ntoo tsis sib npaug, nws yuav tsis muab cov txiaj ntsig zoo ib yam li tsob ntoo sib npaug.
Nws tseem ceeb heev rau kev tshuaj xyuas cov ntaub ntawv uas tus neeg siv ua haujlwm nrog thaum tsim binary tshawb nrhiav ntoo. Koj tuaj yeem koom ua ke cov kev niaj hnub xws li array randomization ua ntej siv binary tshawb nrhiav ntoo rau cov lej kom sib npaug.
Binary nrhiav xam piv txwv
Peb yuav tsum txiav txim siab seb tsob ntoo twg yuav tshwm sim yog tias 25 muab tso rau hauv cov ntoo tshawb nrhiav binary nram qab no:
10
/
/
5 15
/ /
/ /
2 12 20
Thaum muab x rau hauv tsob ntoo T uas tseem tsis tau muaj x, tus yuam sij x yeej ib txwm muab tso rau hauv nplooj tshiab. Nrog rau qhov no, tsob ntoo tshiab yuav zoo li:
10
/
/
5 15
/ /
/ /
2 12 20
25
tsob ntoo twg koj yuav tau txais yog tias koj tso 7 rau hauv cov ntoo tshawb nrhiav binary nram qab no?
10
/
/
5 15
/ /
/ /
2 12 20
Teb:
10
/
/
/
5 15
/ / /
/ / /
2 7 12 20
Ib tsob ntoo tshawb nrhiav binary tuaj yeem siv los khaws txhua yam khoom. Qhov kom zoo dua ntawm kev siv cov ntoo tshawb nrhiav binary es tsis yog cov npe sib txuas yog tias tsob ntoo muaj qhov sib npaug thiab ntau dua li tsob ntoo "tag nrho" dua li "linear" ib qho, ntxig, tshawb nrhiav, thiab tag nrho cov haujlwm tshem tawm tuaj yeem siv los khiav hauv. O(log N) time.