0% found this document useful (0 votes)
18 views46 pages

Unit 3 Data Structures Notes

The document provides an overview of tree data structures, including definitions, terminology, types of trees, and traversal methods. It explains concepts such as root nodes, child nodes, depth, height, and degree of nodes, along with specific types of trees like binary trees and AVL trees. Additionally, it covers algorithms for tree operations such as insertion, deletion, and traversal techniques including inorder, preorder, and postorder traversals.

Uploaded by

24f2003733
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
18 views46 pages

Unit 3 Data Structures Notes

The document provides an overview of tree data structures, including definitions, terminology, types of trees, and traversal methods. It explains concepts such as root nodes, child nodes, depth, height, and degree of nodes, along with specific types of trees like binary trees and AVL trees. Additionally, it covers algorithms for tree operations such as insertion, deletion, and traversal techniques including inorder, preorder, and postorder traversals.

Uploaded by

24f2003733
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 46
UNIT-3 TREES Definition : xA tree B x A tee node and more than one ner {%. Ta: = ZN T2 a Collection of nodes — nits of node'R' whith soot arnpty Aubticcs tk) wohote iR-» is the poaent OF TTT Ti, T2 2+ Te > Lathe child oF For n’ nodes thu is (n-1) edge Basics Torrdinologi cs of Trees : nelght Depth * Rook nodes 3 A) a Child & heanes > By © x sthilengs ‘6) © ND ee % poth Oe) & Aength 0G) ¥ Depth % herght x Anwator aund decendants # Deguee Root node : EO, a eee ber ae node as the top of thenade “no” onto & called as root node. x There vs only one yoot nocle ¢y the tree eq: A'S the wot node. (B callod a Ssanet op Gaps Sibi lings nodes : Ps, x A node pith the sonra parents ov AibilingS og: Bamd C asa Aibilings Oy, E, Ae gibilingS a a pot? for pode Pita 0? i defined as of node nv 02" 03 push that a sapere < wg tte parent OF iy eg path hom A-P > Axe Ect path from AF Jeng the Length of the node ts ‘the ho. of cdaah * oo the pa Lat a aaee E-H, Longth=3 A) pate path row 0 op ay AMCar! Longth = 2 Depth (1) * x for amy 0 node nr, +the depth of pb? ce the St of a re sth Frere ‘the woot depth of oot Spaces olwasss *° ae at TAI See pepth of Fis 3 th of TH 153 Helgi (Y): hodght of ni 1S, the Length of longest path from 0 : heaves Tho height of the [gt Leaves FS ‘0! eq' Height BPA Height of Eis) Height of Toa $3 and Be Adank oe Ancesto mee é oath bem a ™ ‘ 4 ubtaces of the node 0 Colle Degree: A The no- of . * the hofghest degree clegree af the tree se THe eae typo Brake Pea binaty tee & Complete binary Hee x Joft Akoweal bi nee Aree x Right skewed binase mae Fi ub Tree: Ww aS © othounese 2 Called a4 Pp bee ot ds a tree fo which Nad nade hat o. 2 chile oy TH of peeght *P expect binasy up Mae eran Tre! sghts A ayy skewed Bia TE oy the MF 3 A tee whic ie & aad ight Stove binany Expression Tree + (a-ble)* (vle-F) ABc|-DE 4 1h i a B- thas tn which Lead nodes cus optands and totevlor nodes ase opesstess * EX yossion Bree Can be Constructed using postfix expression. Algoxithm for expression Sie: step! » Read one 4ymbol ata re frm the postfix ern be top 2: k phether the Ayrobe! baw pexanc ai: the 4 bol £8 2 © sand , men ee IM painter ate the Atak step 2-2 * afte Ayre 4 on operator, pop TY fom the Akuk ta amet frre a now bee. pith the yoot ab 2 opuator one 7, ab loft child and T2 as right child. A pater bo thi? new free ub pusheet on the le dette nt bfrets Tree es ° tation of bi Tree: c anny a ae Called Lincan hepsesertatton- of "1 elements In the position . ian raat] Bio the position “2i", “the rank L ca 13 tw the position “ai 41" anal the pasen cl js tn the position “fF ja", Cc (lB Te] O]E|F Ta] 42 3 4 5b + : Robt child ren, att)=2 Wis2, Ql2)=y4 sf 7-3,203)-2b Right child »24 +1" fal, 201)4+1=3 7 f22, 20) 4) =5 linked list im Plomentaton:! =3 . 2(3)¢! =F Each node as 3 field * pointer to the Jolt ecbibrco * Datta field x porete bo ‘the right Sub Declaration : tree &hact Tnede int data; Shuct Node * Shuck Tacos visht Example : (a-Ble ) * (ple-F)-Infix [a-ple)=A-bel = ABC! (p/e-F) = E/-F - DE/F- Apc | ~ * DETF- apel-DE/F-* slop 1: Shep? ’ i 5 —g0u A® step3 : step: fhoT1 4 Gilt a aD ap 5 ® © step 5 im stop b: “ta © © @ é 0 : O° Ge Slop: step 8 : QA O08 emo. A) 0) A (AGB © © gS Q S T > @) to:2 arbac+ (at att ExXGmp abe * ydowt t get g of & ; CJ (hal) oe 66 :3 i skip’ a )*q Tree Traversal: xX Tra vessing mean3 * ‘Tyee traversal 5a ty the tee exacting visiting @ach no do onte methoct for all the node One Thee ate 3 types roo traversal ¥ Inordey Travel x preorder Traversal # postordor Travel Tnorder Traversal : i X The inorder traversal of binary" tree us ymed. bi traverse the Loft treo IN *n order amd visit ‘the yoot and travers? to right Aubirec Routine dor §nordor franasal : Noid novos (Tree T ) tee (7} = Null) preorder Travel 13 x The preorder eter troworsol of binary tree i‘ paternnt [ visit the yoot + and traverse Ahe Jopt subtree nnd dravesse the 19” het gubhiee in reordet Routine for pre bye order baweisol: Void preorelet Cree 7) i f (The = NULL) i) preorder tl Sotf Od" 1-5 data); Frerdes (> oft): com Lape i‘ PREOREDER FBA pce uth hoor traversal: : P # The poe ord Bee ot binary Bee paxjormed by be ostorder ond tae asso The ie publice in postoreler and visi Routine fox postorder Traversal : : void postorder (treet J { of (7) -Noll) T stonde (1 bt 7 astordes (Te vighb) Oe Ua cate 5 j EXormple: P Routine of binaty peaseh tee: Creation of node [ peclaration of BST ’ ghact Tree node tL fnt clement } shuct free node Left; shact tree node Hight ; Make empty routine : xit us Used to ate eropty te clemer ct ¥Itu sed. for # talizetion Search Tree makeEmpty (Search Te 1) vitae Noll Step 2 : otheusise check volun’ *! With - vont node Volue Step 211 Ss pea TE Sloe ' ep ba i ship 2-3 : Hh xorg clam ively Eq: chp tf mnt T oe MBN sat (b. 10) i g ae top = Loft) (0) 7 © 8 OMe sips Aint O67) ({0) TO) (2) @ (9) @ & Fiat nex PE edb Fel Bie posttos. ob en EE bse 1h the Hee x To foot aes NS facts ey 0 jan as es age chile : Py Ne up the Jargest 4 clomest. Rocusrsive R ik on sition eis a t if Gene, ae nul alse if (1 nt ee yetwo Th els + Fda aght jj (13" 17 ToV7 bye right oti Snsert | Flin fer Tl ik. seh MT i ef (te=nult) = malloc (size of ( #f (pez NULL) S bruct treonode Md; 4 tnsert a ate hemen ts INT The Ree 16,5) F, 10, 12 Shp: a © tt os a xoot lemert slep 2 : o y 428, Traverse loft shep 3 ensert O ys Trave3e eft shop | insart fran? vigh™ 1a 2l t) TS Gs ! Ise $f as noe ces tbe #8 - else an (To right ) _¢indmin * emp cal : tt); ts xigh alemer ee 2 ene does alement * dolate To aa T— vi?! right J { dap dele 0 GRE Step a: “Aftes deletion of | (8) é a " step 3 Delete vp pes one child Aclele 1g oe sas child position ops, Baverse 6 ® ” step: Alas deletion 0F © (8) @ & © ® ee gteps: delete ° a a3 y has tw0 chi |dten finding “Y (2 es min Value tn ots pubttec ® and roving thot node fo $4 ts position 4 1G, traverse @ @ Loft Steps . Ad tes deletion of y 5 (3) Q ' using rnsal a delat the BST Lasing 3,1, eb 9 27 the cloment | 3 and 13-8 Insertion: step! | insert 3 bis - @3 sa i aroot ee ase Stop2: ro Right side. vy Less theo slaps: sort 7 9", So traverse bop Side step 3: fnsert } _ B greakeor +han* *3/,S0 een Right 9 Sido step b: onsext 2 def i : A 19" 46 greater thao; So "10 3 reat travese right side skp 1) :fogert 12. +" Lesser thar "3" So Traverse Lope si 2. Consider the Binary Search Thee givee b alow find the results oF inordas , pestordes and pseordor fravortals - chow that deletion af amd tnsext H 22, 39/44, SF! fb and #4 /7 the Ror. Lnorder=> 10-12 - 32-34 24> roprder > 45-34-12 10-34-39 = 5b 5a #8 bt BF postordar—> Ip— 32-34 -12-39-4 Deletion of rot node: step & + insert 22 skp 3: jnsut 33 21 Gu) fi NL Tree > y Adelson velaxi. Lemcis k Hn AVL Ttee a ®) ADs A fe Before rotation Abts rotation ; Phy pight Routine * Single Rotation WI Rolation \with ight (posite ki) Sirgle : positisn k2; ko eki—> righ ki right i? ; vo —> Aaft okt : F eo eget = t yeight (K2 isght ) viclgh Cen epa « pt), Heit ky > Hough _ max ( Heeglt (ung Met (xi eet yobeun Ke; ae) Fg step! : ot aaa) (3) Bre! 120 fo ship a: Tpsert ° - @ The Balancing fester for Ome root nede ck -£. TE Aol gn AVL Tees, TO Comet thy Aen AN Ts@ perform! : Gingle: votion Coast) O” Routine for double yolation with Lopt 93 pate rotation With Left ( posttisn xa) ka > hop = Single polation With Right (xa bt); return Single yolabim With goft (K3); ~ th hile 12 07 jg the Avi btee mos Tn balers so porforny fo Case3 : LR Routine & the insertion Soto tHe loft Abbree op ww right child of node o% vmod by the Aingla aetations x This Gn be peifo &) (Kk rN A (e) sh ®B rl sa AAs Atte single Before Rebation pian Airgle —_potadion tebe) (Right) t Lebt ) peuble Retadion jufth right (position ki J kis vight = Singko yolatfon vi rh Loft CK vighh) yotusm ingle xdetton with vight (ki)5 ohio out ney tig aul Tree 2Romg chip 3 : insert 3 \ s o oO . a a -2 $b & not on anita 50 exforr! RR pata 7 the BF for 5 is -2 tt && net cw Avi Tsee SO, paper RR e The BF forg?., 9 ot not Gn AVI Ree 50 - nel ul Tee 2p t J jo Insert Io | (4) 7 —3t (0) The pF for bis — Td ck not an Avl Tra: S0 : efor RR Rétestfon Tra Routine : AvU Tse tnsert (AVE t ‘ (t==Nutl) + emallec ($17 af (Shack AIT nebo); it (t== Nutt) Enor (* out © else { q5 dale rose 75 Loft = NUL tT wight = nth; TS heught < 03 4 j alsatt (X47? date J spree 7, 50 *) f spote")/ { T> Loft = Insert (ros loft x8): tt (Hetght (> Loft )— Heeght (T-> right) = = 7)) Tsp fy sty Hy lade) T= Siinghe youtine yotation with rfght (1 i else T= Double notation fight (1); ; Ts hegght _mtax (Height (T= Loft), naught (1>7%ght d41)5 yotum T; eee xiow Need for pronty Quel: KA ene & auton. te run only fora PRUE * rh on algorithm Leses Aimple Auoue, jobs st fob 00 the nts ether 'S ee> po model for, priority Ouse x It Gn be overtome by sore prety ef Axousds tn Which Ahost ye bs aid alasigredl a x sorte fobs that are not Ahovd as AtLL every emportamt amel Ahowld a row profane KA psiostty Aucus ub a - apettal Lend of Queue dala Abuctiwe robich wll howe pactodonts Over fobs : * Thue o Qke foo baste epocdtor in prtority Quote, ae Insert apes ‘ > Delete min operation aa se Opapetten as “ielas. ta En@ucue “ Delole min” 4 Airobles fo Debus. Operation who the delete minima? alecent fiom the priovity Cures oe nee tet Trphamertation of priority ae ¥ linked List Tropheenes abo * Birauy 3 Tree Papeete Aa Bias im Jormentt Linked List st bp Pitin: taken for Insortion — oll) Tho tre “ama token) = 008) for deletion \ Ya Ineay Seareh 1 dation y The Hme taken tor Insertion - olw) the time Laken fox deletton -o(n) Binary H iroplormontot Tn: Tho time Jaken for Insertion -o(n) The Lino taken for deletion _0(N) Binaay OT is also Sa eeuruapi deere Pet 4) shuctwe properties 3) Heap ovdet propertics ea ye ase | Le} appols pe ele Ts Ata a Re : ail GQ ® 0 ® HOO Hho size =O; _ Elornoets [ % yetuan Hf Ae pain Date ; J Binary Heap operat ons : 1) Inset opaatton: cq Void onger t Cint x priovty uous 4) int sarap se tts Full (Hd) Evy (" prfowty Busia 1s Ful") yetus ; 4 for ier) 2 ne Splmeny eo i[=2) J]. HO alements fr fod; @ B@ © : the ho yo co lobe vi peep ee bb satya | | Orders Delete min operat fons : Q ‘nt Deletemin (priority Auewe Ht) i tot 1, Child; sat minElemest, Lasté lement ; if (Ls Empty (H)Y enor (" pronty Quaus empty?’ yotusn > Elements lo). i ‘ Min Element =H} Elements [IJ : bast Element =H OF CH Die for (tai iw 2220 2Sh 22: 3 child) t child 1c, i if (child P= Ho size 2 3 ElomentL ehgld 1) GH > Elernents fe ba), Childers lerwats [ebild J) if last Element >> E H > Element (rch Eternals (child) s pacolte down the pols ee she hap Or8e" statement (or) Algor’thtg for Pelekornin : on 1 bod to dedebe The Y, i“ Dolota min operatio minima element from “Ihe hoop. : * In binary hroap the minimus cloment oh ound in The root - % Wheo thes minimu eloment vomnoved, hole 23 Crectteal oh ° the hohe withouk

You might also like