2D Segment - Quad Tree Explanation With C++ - Stack Overflow
2D Segment - Quad Tree Explanation With C++ - Stack Overflow
StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,no
registrationrequired.
login
tour
8/8/2014
help
careers2.0
Takethe2minutetour
2DSegment/QuadTreeExplanationwithC++[onhold]
P.S.Thisismaybenotaduplicate.IsearchedSOandmakesurethatIdidn'tgetwhatIamseekingfor.
IamanACMproblemsolverandRecentlyI'velearntSegmentTreeforlineararrayandSegmentTree
withlazypropagation.ButIamencounteringsomeproblemswhichneed2Dsegmentstree(whichis
referredasQuadtreeinsomewhere).ButIcan'tfindanygoodtutorialsonit.IsearchedSOandfound
alinkhttp://emaxx.ru/algo/segment_treewhichisatutorialinRussianlanguage.
Ineedsomegoodexplanationwithsourcecode(preferablyinC++)on2DsegmentTree.Itistobe
notedthat,Iknowtypicalsegmenttreeprettywell.
c++ c algorithm tree
editedAug4at15:55
KaidulIslamSazal
1,439 6 26
askedAug4at15:11
pussyUtils
3 1
putonholdasunclearwhatyou'reaskingby,David
Eisenstat,vsoftco,EdChum,Yuushi2daysago
Pleaseclarifyyourspecificproblemoraddadditionaldetailstohighlightexactlywhatyouneed.Asit'scurrently
written,itshardtotellexactlywhatyou'reasking.SeetheHowtoAskpageforhelpclarifyingthisquestion.
Ifthisquestioncanberewordedtofittherulesinthehelpcenter,pleaseeditthequestion.
Canyoushowussomeexamplesofwhatyou'vetriedsofar?robbritAug4at15:13
Also,aquickGooglesearchfor"quadtree"givesmethis:gamedevelopment.tutsplus.com/tutorials/
robbritAug4at15:14
Quadtreesaredifferent.Lookup"rangetrees."A2Drangetreeisbasicallyjustatree(onx)ofsegmenttrees
(ony).tmyklebu2daysago
addacomment
1Answer
Well,asyousaidandIhopeyouknowsegmenttreewellenough.Iamgivingsomeintuitionbehind
multidimensionalsegmenttreefrommyblog.
SupposeyouregivenatwodimensionalN*N(foraprettylargeN,largeenoughtobenothandledby
bruteforce)gridofintegervalueandyoureaskedtoperformoperationsfindtheminimum/maximum
valueorcalculatethesumofallitemsofaparticularportionofthegrid,updateanyofthegridindex
valueetc.Itseems,theproblemisnodifferentthantypicalsegmenttreeproblemunlikethedimension
ofdatacontainer.Whatcanbeachoicehereisbuildinga2DSegmentTree.
Theideaof2DsegmenttreeisnothingbuttheQuadTreeAtreedatastructureinwhicheachexternal
nodehasexactlyfourchildren.Quadtreesaremostoftenusedtopartitionatwodimensionalspaceby
recursivelysubdividingitintofourquadrantsorregions.Theregionsmaybesquareorrectangularor
mayhavearbitraryshapes.ThedatastructurewasnamedaquadtreebyRaphaelFrinkelandJ.L.
Bentleyin1974.AsimilarpartitioningisalsoknownasQtree.
Therootofthetreecontainsthefullsegmentsthrough [(0,0),(N1,N1)].Andforeach
segment [(a1,b1),(a2,b2)],wesplittheminto [(a1,b1),((a1+a2)/2,(b1+
b2)/2))], [((a1+a2)/2+1,b1),(a2,(b1+b2)/2)], [(a1,(b1+
b2)/2+1),((a1+a2)/2,b2)]and [((a1+a2)/2+1,(b1+b2)/2+
1),(a2,b2)]until a1=b1and a2=b2.Thecostofbuildingasegmenttreeis
O(nlogn)andwithsegmenttreereadyansweringanRMQ(Rangemaximum/minimumquery)canbe
donein O(logn).
Suppose,youaregivenagridwithN=4.Thenthecorrespondingtreewillbe
http://stackoverflow.com/questions/25121878/2d-segment-quad-tree-explanation-with-c/
1/4
8/8/2014
http://stackoverflow.com/questions/25121878/2d-segment-quad-tree-explanation-with-c/
2/4
8/8/2014
For,1Dsegmenttree,Wedividethearrayinto2(2^1)segmentsandthatyieldsa log2(n)complexity
forparticularoperations.Againfor2Dsegmenttree,Wesplitthe2Dgridineverystepinto4(2^2)
segmentswhichensureeveryoperationswith log2(n)cost.Soinasimilarfashion,Weexpandthis
3Dtreebysubdividingthegridinto8(2^3)segments.
P.S.:The3DsegmenttreeismyownimaginationIdon'tknowifthereisanythinglikethat.Maybe
thereisabetterwayforboth2Dand3Dsegmenttreeoutthere,ButIthinkthesecodesufficefor
programmingcontestsasIhaveusedthesemanytimes.
Edit:
Theideaof3DsegmentationisnothingbuttheOctreeatreedatastructureinwhicheachinternal
nodehasexactlyeightchildren.Octreesaremostoftenusedtopartitionathreedimensionalspaceby
recursivelysubdividingitintoeightoctants.Octreesarethethreedimensionalanalogofquadtrees.
Octreesareoftenusedin3Dgraphicsand3Dgameengines.Ithasalotofotherapplicationslike
Spatialindexing,Nearestneighborsearch,Efficientcollisiondetectioninthreedimensionsandsomany.
Hopeithelps!
editedyesterday
answeredAug4at15:20
KaidulIslamSazal
1,439 6 26
http://stackoverflow.com/questions/25121878/2d-segment-quad-tree-explanation-with-c/
3/4
8/8/2014
1 Thisisn'tright.3Dsegmentationhaslotsofhascommonuses.Octreesareusedtorepresentsolidsin
manygraphicsapplications.Givenacollectionofcirclesintheplaneandonedistinguishedcircle(P1,r1),
findthecircleinthecollection(P2,r2)minimizingdist(P1,P2)r1r2.GeneAug4at16:09
@GeneThanks.Ididn'tknowaboutOctree.Thanksforthisinformation,Iameditingmyanswer
KaidulIslamSazal2daysago
addacomment
http://stackoverflow.com/questions/25121878/2d-segment-quad-tree-explanation-with-c/
4/4