PDS UNIT 4.doc

56
UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES 1 AVL trees – B-Trees – Red-Black trees – Splay trees - Binomial Heaps – Fibonacci Heaps – Disjoint Sets – Amortized Analysis – accounting method – potential method – aggregate analysis. TREES Tree is a Non- Linear datastructure in which data are stored in a hierarchal manner. It is also defined as a collection of nodes. The collection can be empty. Otherwise, a tree consists of a distinguished node r, called the root, and zero or more (sub) trees T1, T2, . . . , Tk, each of whose roots are connected by a directed edge to r. The root of each subtree is said to be a child of r, and r is the parent of each subtree root. A tree is a collection of n nodes, one of which is the root, and n - 1 edges. That there are n - 1 edges follows from the fact that each edge connects some node to its parent and every node except the root has one parent A tree Terms in Tree In the tree above figure, the root is A. Node F has A as a parent and K, L, and M as children. Each node may have an arbitrary number of children, possibly zero. Nodes with no children are known as leaves; The leaves in the tree above are B, C, H, I, P, Q, K, L, M, and N.

Transcript of PDS UNIT 4.doc

UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES 1AVLtreesB-TreesRed-BlacktreesSplaytrees-Binomial HeapsFibonacci HeapsDisjoint Sets Amortied Analysis acco!ntin" met#od potential met#od a""re"ate analysis$TREESTreeisa%on-Lineardatastr!ct!reinic#dataarestoredina#ierarc#al manner$'tisalsode(inedasacollectiono(nodes$ T#ecollectioncanbeempty$ )t#er&ise* atreeconsistso(adistin"!is#ed node r* called t#e root* and ero or more +s!b, trees T1* T-* $ $ $ * Tk* eac# o( oseroots are connected by a directed ed"e to r$T#e root o( eac# s!btree is said to be a c#ild o( r* and r is t#e parent o( eac# s!btree root$ Atree is a collection o( n nodes* one o( ic# is t#e root* and n - 1 ed"es$ T#at t#ere are n - 1 ed"es(ollo&s (rom t#e (act t#at eac# ed"e connects some node to its parent and e.ery node e/cept t#eroot #as one parent A treeTerms in Tree'n t#e tree abo.e (i"!re* t#e root is A$ %ode F #as A as a parent and 0* L* and 1 as c#ildren$ 2ac# node may #a.e an arbitrary n!mber o( c#ildren* possibly ero$ %odes &it# no c#ildren are kno&n as leaves3 T#e leaves in t#e tree abo.e are B* 4* H* '* 5* 6* 0* L* 1* and %$ %odes &it# t#e same parent are siblings3 t#!s 0* L* and 1 are all siblin"s$Grandparentand grand!ild relations can be de(ined in a similar manner$ A pat! (rom node n1 to nk is de(ined as a se7!ence o( nodes n1* n-* $ $ $ * nk s!c# t#at ni ist#e parent o( ni81 (or 1 i 9 k$ T#e lengt! o" t!is pat! is t#e n!mber o( ed"es on t#e pat#* namely k -1$ T#ere is a pat# o( len"t# ero (rom e.ery node to itsel($ For any node ni* t#e dept! o" ni is t#e len"t# o( t#e !ni7!e pat# (rom t#e rootto ni$ T#!s* t#e root is at dept# :$ T#e !eig!t o" ni is t#e lon"est pat# (rom ni to a lea($ T#!s all lea.es are at #ei"#t :$ UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES - T#e #ei"#t o( a tree is e7!al to t#e #ei"#t o( t#e root$ E#ample$ %or t!e above tree& 2 is at dept# 1 and #ei"#t -3 F is at dept# 1 and #ei"#t 13t#e #ei"#t o( t#e tree is ;$ TNote$ T!e dept! o" a tree is e'(al to t!e dept! o" t!e deepest lea") t!is is al*a+s e'(al to t!e!eig!t o" t!e tree, I" t!ere is a pat! "rom n- to n.& t!en n- is an anestor o" n. and n. is a desendant o" n-,I" n- n.& t!en n- is a proper anestor o" n. and n. is a proper desendant o" n-, A tree t!ere is e#atl+ one pat! "rom t!e root to ea! node,T+pes o" t!e Tree$Based on t#e no$ o( c#ildren (or eac# node in t#e tree* it is classi(ied into t&oto types$1$ Binary tree -$ $ 1ainly !sed in cac#e memory and "arba"e collectionAdvantages$Simpler 1ore space e((icient1ore (le/ibleFasterDisadvantages$ Borst case iss!es o( splay tree al"orit#m is* all t#e elements are se7!entiallyaccessed in sorted order$'t leads splay tree into !nbalanced tree$UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES --UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES -;UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES ->Red bloA tree$ ; )nlynodesont#epat#(rom ptot#erooto(T-canbein.iolationo(t#ele(tist #eapproperty3 t#ese can be (i/ed by s&appin" c#ildren$ Since t#e ma/im!m ri"#t pat# len"t# #as at most lo"+n 8 1, nodes* &e only need to c#eckt#e (irst lo"+n 8 1, nodes on t#e pat# (rom p to t#e root o( T-$ LaJ+ Gerging T#esecondideat#at is!sedbyFibonacci #eapsislaymer"in"$ Be&ill applyt#isideatobinomial 7!e!es ands#o&t#at t#eamortiedtimetoper(ormamer"eoperation+as &ell asinsertion* ic# is a special case, is )+1,$ T#e amortied time (or delete\min &ill still be )+lo" n,$T!e idea is as "ollo*s$ To mer"e t&o binomial 7!e!es* merely concatenate t#e t&o lists o( binomial trees* creatin" a ne&binomial 7!e!e$ T#is ne& 7!e!e may #a.e se.eral trees o( t#e same sie* so it .iolates t#e binomial7!e!e property$ Be &ill call t#is a lay binomial 7!e!e in order to maintain consistency$ T#is is a(ast operation* ic# al&ays takes constant +&orst-case, time$ As be(ore* aninsertion is done by creatin" a one-node binomial 7!e!e and mer"in"$ T#e di((erence is t#at t#emer"eislay$ T#edelete\minoperationism!c#morepain(!l* beca!seit isere&e(inallycon.ert t#e lay binomial 7!e!e back into a standard binomial 7!e!e* b!t* as &e &ill s#o&* it is still) +lo" n, amortied time-b!t not )+lo" n, &orst-case time* as be(ore$ To per(orm adelete\min* &e (ind +and e.ent!ally ret!rn, t#e minim!m element$As be(ore* &edelete it (rom t#e 7!e!e* makin" eac# o( its c#ildren ne& trees$ Be t#en mer"e all t#e trees into abinomial 7!e!e by mer"in" t&o e7!al-sied trees !ntil it is no lon"er possible$UNIT IV - ADVANCED NON-LINEAR DATA STRUCTURES >>