Puas muaj chaw nyob hauv Java?
Puas muaj chaw nyob hauv Java?

Video: Puas muaj chaw nyob hauv Java?

Video: Puas muaj chaw nyob hauv Java?
Video: Tsuas yog koj tseem muaj txoj sia nyob - NREES HANDS (OFFICIAL AUDIO) 2024, Kaum ib hlis
Anonim

Rau Java 8, hloov kho ntawm cov lus teb uas twb muaj lawm: Koj tuaj yeem siv Java Priority Queue li a Heap.

Ib yam li ntawd, tib neeg nug, dab tsi yog pob zeb hauv Java?

Cov pob yog qhov chaw khiav haujlwm cov ntaub ntawv los ntawm qhov chaw nco rau txhua chav kawm thiab arrays raug faib. Cov pob yog tsim los ntawm lub tshuab virtual pib. Heap cia rau cov khoom yog reclaimed los ntawm ib tug tsis siv neeg cia tswj system (lub npe hu ua ib tug khib nyiab collector); cov khoom yeej tsis tau hais meej meej.

Tom qab, lo lus nug yog, binary tsob ntoo yog heap? A binary pob yog a pob cov ntaub ntawv qauv uas siv daim ntawv ntawm a tsob ntoo binary . Binary heaps yog ib txoj hauv kev los ua cov txheej txheem ua ntej. Heap Cov cuab yeej: tus yuam sij khaws cia nyob rau hauv txhua lub node yog ntau dua lossis sib npaug rau (≧) lossis tsawg dua lossis sib npaug rau (≦) cov yuam sij hauv cov node cov menyuam, raws li qee qhov kev txiav txim tag nrho.

Tom qab, lo lus nug yog, yuav ua li cas heap yog siv nyob rau hauv Java?

Peb siv chav PriorityQueue rau siv Heaps hauv Java . Los ntawm default Min Heap yog siv los ntawm chav kawm no.

Rau ib qho ith node, i.e., Arr:

  • Arr[(i -1) / 2] rov qab nws niam txiv node.
  • Arr[(2 * i) + 1] rov qab nws tus me nyuam ntawm sab laug.
  • Arr[(2 * i) + 2] rov qab nws txoj cai me nyuam node.

Puas yog lub heap?

Cov Heap . Cov pob yog thaj tsam ntawm koj lub khoos phis tawj lub cim xeeb uas tsis tswj hwm rau koj, thiab tsis yog tswj hwm nruj heev los ntawm CPU. Nws yog thaj chaw dawb-floating ntau dua ntawm kev nco (thiab loj dua). Txhawm rau faib lub cim xeeb rau ntawm pob , koj yuav tsum siv malloc() lossis calloc(), uas yog built-in C ua haujlwm.

Pom zoo: