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

DTS Assignment 2

The document outlines a C program for implementing a doubly linked list that displays numbers in both forward and backward order. It includes source code snippets for creating nodes, inserting data, and displaying the list. Additionally, it discusses AVL trees and threaded binary trees, explaining their properties and advantages.

Uploaded by

giteshzope19
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 views14 pages

DTS Assignment 2

The document outlines a C program for implementing a doubly linked list that displays numbers in both forward and backward order. It includes source code snippets for creating nodes, inserting data, and displaying the list. Additionally, it discusses AVL trees and threaded binary trees, explaining their properties and advantages.

Uploaded by

giteshzope19
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/ 14
Assignment .No..02 Censidew a stucture with data % 2 pointers (next 2 prev) Write a program in. ‘c’ +o implement Doubly Linked Iist that will display bumbers in forward % backward orders Source code: # include #_fnclude < malloc. h> struct node ; | int dabe: Struct node * next; Struct node * prev; Struct node * stoxt = NULL) | Struct node * end: NULL; | Void disBockward (); int_main () § printf (~ Let's create a doubly linked list \n”): create () 5 printf ("\n Dota th forward order s\n”) > disforward (); printf (“\n Dota tg Backward axdesn”); dis Backward (); weturn 0 5 I § Ilvoid create ( ) § tnt num } Struct Node * new-node *ptr : printf ("\n Enter -4 to end xn”); = printf ("Enter the data: !")5 __scanf ("sed"), our); void create ( ) 3 2 | veid disForwaxd (); ——————————— © DATTAMEGHE COLLEGE OF ENGINEERING race no.: — while (pum! jf (start == NULL) E - Fe: new-node = (steuct node +) maljoc (siveof (struct node )) + new. node > prey = NULLS __new- node > data = num; J iho __new- node > next = NVLE; * Stost = new-node ; i riod end = new-node ; a pe BY 1 5. iiicise 5 4 ptr: stort new-node _____neW- pode _—» data = num 7 while (ptr » next We muri) pia = ptr > next) ee ae _pty 9 next = new.node 5 _new.node > pre ___new- node > next « new. node + 3 ed Bmum); 4 3 ||vord disforword C9 OF HMHOTS we struct node * pty; iin piv: start + while (pta!= NULL) § | printf ("\t led", pty » dota); | pit: ptr > next; j ‘void disBackwasd () { | struct node * pty; ptr = end ; while (ptt != NULL) § printf ("\t pd", ptr » data): DATTA MEGHE COLLEGE OF ENGINEERING ptr. ptr» prev 4 4 {Outputs er Lets create a doubly linked list Ente -1 +0 end Enter +he data : 90 a. Enter the data : 5° ce ny Entex the data ¢ 45 || Enter _+he data # 78 Entes +he data 64 | Enter the data : -1 | Data _in Forward oxdes ¢ {| TOs OM mea DLO 2 64 Data fn Bockword ovder : Il 64. 78 45 Bo qo & Date x tS 4) DATIAMEGHE COLLEGE OF ENGINEENANG: acene a Explain AVE. toes geasch t7eek 634 Insert the following elements ino AvL 2,49, 83, 92, 29) 23, 54s 13,99 Binary Search bts of left & aight an_AVL tree defined as_a_self-balancin “Tree (BST) wheae the difference b/w_he! subtrees for any node cannot | be more then _one-——— |hefghb - balanced +re@. ______— ii) Due to thi's _propesty. the AvL tree ‘fs also known as — ‘The difference b/w the heights of the ‘left subtree & ___ the sight subtree for any node is known as the _ balance factor of the node cS Le ie Balance facto® = Height (left sub-tzee) - Hetght (aight sub aN tree) jv) Example of AVL __+ree_=> of i | | Ly we dea single left ectation pe See : Dan AVL Tree ave : a) Insextio |<) Searching ete ” vid An AVL tree may rotate in one of the Following four C -b) Deletion f ways to keep itself bolonced ‘ a) teft' Rotation : When a node is added into +he eight left Subtree of left subtree, if the tree gets out of balance, pb) Right Rotation: The new node is inserted in the afght | sub-tree of the wight sub-tree of the caitical node \c) LR Rotation: The new node fs inserted fn the sight sub-+xee of left-sub- tree of +the critical node 4 4) RL Rotation: The new node is insezted in the left sub of the Right Sub-tree of +he critical node ceca vii) Elements axe given + 63, 52, 49 4 E Z s 183, 92, 249 +42, 123, 54, pte, _——tneemster Ut rl ee as 52<63 fpsevt* left sub _tree Pp Ui) 3 Ingest 44 Step (iv) 2 Balance AVL by 49< 52.<63 so ingest at RR Rotation left sub +xee e)) ha > Step (v): insert 92 Step (vi): insext 92 83 > 63> 52 inser t ot 92>83>63> 52 go insert at Bight sub +zge right sub | by 23<29¢49<52 50 jot eft Sub +zee Pos @ | Step (vii): Balance AVL tree_|_ step (ix): fares tito Umut insert _ OStep Wii): lincent 29) | 29 < 49 <52 60 ingest at Lleft subtree | a Step (4) Balance Avi tree bys RR Rekation © DATTA MEGHE COLLEGE OF ENGINEERING 2aeno Dataset step (xii): insert 13 13, £23 €29< 52 $0 insert Step (xi)? fnsext 54 54> sub-tzee according/; tree according /y Btep (xii) + insert 99 99> 92 >83> 52 $0 ingert at tight sub-tree accosdingly _ me i Explain Threaded. (Binary Tree tA threaded ‘binary 4ree isa type of binary tree dota G _|structure where _the_empty left wight child pointess fn — a binary tree axe weplaced with threads that tink nodes divectly ‘|e thefts in- order predecessor or successor, thereby paeviding lla way +o traverse the _tree without using wecuxsfan oF a stack Fe eed ee oe yA ny tareaded binary. trees can be useful when space isa cenctrn, ‘as they can eliminate the need for a stack during traversal i) These ave Mmapy ways of threading a binary tree 2 each +ype may vary according to the woy the tree is_traversed- iv) One - way threading : In this a thread will appeor leither In the aight field o* the left field of +he node. ~ Tt fs also called a single - +heeaded_txee V7 Pen = —— fn the left field, then the left E Ie fhe thread appears — to the fn-omder paedecassor of || Geld will be made to_poin: breaded tree ig called a left - the node. Such a one-way + threaded binaxy tree. H F _If the thuead appears_in_ “the. right “field . then it will point to the tn-arder successor of the node. Such-a _one - way threaded tree fs called a aight - thecaded binary teree- wi Two = way Threading +f In this threads will appear in both. the left % the aight one of the node. os ~ I} also called a d: = threaded tree. ot fully threaded ss binary tree- . iD ie = while the left ffeld will _point +o_ the_in ‘order. predecessor of +the node, the right field iwill_pofnt. she tg. guccescor. ___ i) The example of threaded bfnaxy tree > HSS | | dotted tines represent threads. BALA _|lvit) Advantages of threaded bibary tree lla) T+ enables 1 x traversal of elements. Linuhe dee. i j b) Lineax traversal eliminates dine Wee verte aval Si ¥, kurn consume_a_lot of memory space_% computer for. _ c) T+ enables to find the parent of a_givep element without explicit use of parent _pointers. ie “lla) since nodes contain pointers +o fn-oxder paedecesyox [successor the threaded +xee enables forward backward |lgraversal of the _noder_ac given_b eae ace S DATTAMEGHE COLLEGE OF ENGINEERING ave ne Date Consider a dota structure with oll no, pescentage — g next pointe? « waite a _poograny in tc? to Implement Single finked Ist +0 count punobers— of students getting distinction (pescentage— chav $21 vee Source Geode 5 SUSE Cee | # Ynclude < stdio. t # fnclude < conio-h> z Include < malloc-h> i he struct student } vyeel oO oe is int _sall_nos = e978 ‘i pbs float perg bh tk struct sbddent * next> a fs Va “| struct péddent * Elo env ee Lyoid store. tht ib a; eee SA) Pomes ~~ ai |aerd count Oj _ void main ( ) i int nc 3 a8): oli store (rds cs count (3504 ee \n_Nunober of students setting albsnchions nd") 7 _ogetehO: a 3. i | void store Lint bo iy SBE 8 1 EALare } bol sink apr re ||) sketoat ps a” fat) “student “* pir, 7 new-node G pint Enter 3 sll na a percen’ at Sa qed taf”, 25 ,8P); otage of “sinident DATTA MEGHE COLLEGE OF ENGINEERING pase vo __Date me new. node = (struct student *) malloc (siveof ( struct ss Student )) 5} new - node =» Goll-Mo = w 5 New- node + per = ip; Meeeit ( Stoxt == NULL) § new. node —> next. = __staxt = new. node NULLS —else } —___ptt= stast wr" ea apa babile (pia ~neket = NULL) —___pir > ptr nents. [ptt _— next = new node ; = new. Node ~» next DLL Student * pty 3. pits starts _ Ht pty— perp. pir Sp L while (ptrd= Nutt) 3 _ 1 GF) (pte — per > 65.00); ptr = ptt next 5 py tok cht eo ee ae P i} 4 e t Sea 4 5 6 a Rs | return cj 4 Hitia! ; F a ‘ L Output 2 f a | Enter the number of students : 5 F —iillenteraoll no i. lpercentage of students 21 90.15 S i .

You might also like