cs261 data structures
play

CS261 Data Structures Binary Search Trees III Generic - PowerPoint PPT Presentation

CS261 Data Structures Binary Search Trees III Generic Container Goals void * compare func?on Generic Storage #ifndef __DYNARR_H #define __DYNARR_H # define


  1. CS261 ¡Data ¡Structures ¡ Binary ¡Search ¡Trees ¡III ¡ Generic ¡Container ¡

  2. Goals ¡ ¡ • void ¡* ¡ • compare ¡func?on ¡

  3. Generic ¡Storage ¡ #ifndef __DYNARR_H #define __DYNARR_H # define TYPE int # define TYPE_SIZE sizeof(TYPE) # define LT(a, b) ((a) < (b) # define EQ(a, b) ((a) == (b)) ... /* Rest of dynarr.h (on next slide). */ #endif

  4. struct Node *_addNode(struct Node *cur, TYPE val){ struct Node *newnode; if (cur == NULL){ newnode = malloc(sizeof(struct Node)); assert(newnode != 0); newnode->val = val; newnode->left = newnode->right = 0; return newnode; } if (LT(val, cur->val)) cur->left = _addNode(cur->left,val); else if (GT(val, cur->val) || EQ(val, cur->val)) cur->right = _addNode(cur->right, val); return cur; } Problem: ¡ ¡must ¡ ¡recompile ¡our ¡data ¡structure ¡if ¡we ¡change ¡the ¡type! ¡ ¡

  5. Generic ¡Storage ¡Alterna?ve ¡ • Write ¡your ¡structure ¡to ¡store ¡void ¡* ¡ – generic ¡pointer ¡type, ¡indicates ¡absence ¡of ¡ ¡a ¡type ¡ – any ¡pointer ¡can ¡be ¡cast ¡to ¡ ¡void* ¡and ¡back ¡again ¡ without ¡loss ¡of ¡informa?on ¡ • Require ¡that ¡the ¡user ¡tell ¡us ¡how ¡to ¡‘compare’ ¡ the ¡elements ¡that ¡they ¡are ¡actually ¡storing! ¡ – compare ¡works ¡on ¡void ¡* ¡ – casts ¡them ¡to ¡actual ¡types! ¡

  6. Generic ¡Storage ¡ #ifndef __DYNARR_H #define __DYNARR_H # define TYPE void * ¡ ¡ ¡ ¡ ¡/* ¡func?on ¡used ¡to ¡compare ¡two ¡TYPE ¡values ¡to ¡each ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡other, ¡define ¡this ¡in ¡your ¡compare.c ¡file ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡Iif ¡ ¡l ¡=r ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑1 ¡if ¡l ¡< ¡r ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡1 ¡if ¡l ¡> ¡r ¡*/ ¡ ¡ int ¡compare(TYPE ¡le2, ¡TYPE ¡right);

  7. Add ¡with ¡Void ¡Pointers ¡ struct Node *_addNode(struct Node *cur, TYPE val){ struct Node *newnode; if (cur == NULL){ newnode = malloc(sizeof(struct Node)); assert(newnode != 0); newnode->val = val; newnode->left = newnode->right = 0; return newnode; } if (compare(val,cur->val) == -1) cur->left = _addNode(cur->left,val); else if (compare(val, cur->val) == 1) cur->right = _addNode(cur->right, val); return cur; }

  8. BST ¡Add: ¡ ¡Usage ¡ void addBSTree(struct BSTree *tree, TYPE val) { tree->root = _addNode(tree->root, val); tree->cnt++; } … /* somewhere in main*/ struct data { int val; char *name; } struct data myData; myData.val = 4; myData.name = “dataName”; … add(myTree, &myData);

  9. Usage ¡with ¡Void ¡Pointers ¡ /* somewhere in main…or a header */ int compare(TYPE left, TYPE right) { struct data* data1; struct data* data2; data1=(struct data*)left; data2=(struct data*)right; if (data1->number < data2->number) return -1; else if (data1->number > data2->number) return 1; else return 0; }

  10. Summary ¡– ¡Generalizing ¡your ¡Data ¡ Structures ¡ • Use ¡void* ¡as ¡the ¡type ¡of ¡stored ¡elements ¡ • Require ¡that ¡the ¡user ¡provide ¡a ¡compare ¡ func?on ¡ • Even ¡Be\er: ¡ ¡Pass ¡the ¡compare ¡func?on ¡in ¡ with ¡a ¡pointer ¡to ¡a ¡func?on!! ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend