c programming –application of pointers to linked list and binary tree in this lecture we learn...

25
C Programming – Application of Pointers to Linked List and Binary Tree • In this lecture we learn about: – Linked Lists – Binary Trees

Upload: francine-thompson

Post on 23-Dec-2015

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

C Programming –Application of Pointers to Linked List and Binary

Tree

• In this lecture we learn about:– Linked Lists– Binary Trees

Page 2: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Structs which contain themselves• Sometimes programmers want structs in C

to contain themselves.

• For example, we might design an electronic dictionary which has a struct for each word and we might want to refer to synonyms which are also word structures.

word1:runsynonym1synonym2

word2: sprintsynonym1synonym2

word3: jogsynonym1synonym2

Page 3: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

How structs can contain themselves

• Clearly a struct cannot literally contain itself.

• But it can contain a pointer to the same type of struct

struct silly_struct { /* This doesn't work */ struct silly_struct s1;};

struct good_struct { /* This does work */ struct *good_struct s2;};

Page 4: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

The linked list - a common use of structs which contain themselves• Imagine we are reading lines from a file but

don't know how many lines will be read.

• We need a structure which can extend itself.head_of_list

information

nextptr

information

nextptr

information

nextptr= NULL

This is known as a linked list. By passing the value of thehead of the list to a function we can pass ALL the information.

Page 5: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

How to set up a linked listtypedef struct list_item { information in each item struct list_item *nextptr;} LIST_ITEM;

This structure (where information is what you want each"node" of your linked list to contain). It is important thatthe nextptr of the last bit of the list contains NULL so thatyou know when to stop.

NULLhead oflist

Page 6: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Address book with linked lists

typedef struct list { char name[MAXLEN]; char address[MAXLEN]; char phone[MAXLEN]; struct list *next;} ADDRESS;

ADDRESS *hol= NULL; /* Set the head of the list */

Page 7: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Linked list conceptsNULL

head oflist

Adding an item to the middle of the list

NULLhead oflist

new itempoints at next item

movethislink

Deleting an item from the middle of the list

NULLhead oflist

move this link

delete this node

Page 8: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Creating a New Node

• Allocate space for a new node.

• Set the next pointer to the value of NULL

• Set the data value for the node.

Page 9: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Adding Nodes to the List

• If the start node is null then the start node becomes the new node.

• If start is not null then start becomes the new node’s next and the start becomes the new node.

Page 10: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

START

Data Next

Page 11: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

START

Data Next

Page 12: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

START

Data Next

Page 13: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

START

Data Next

Page 14: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Adding to our address bookvoid add_to_list (void) /* Add a new name to our address book */{ ADDRESS *new_name; new_name= (ADDRESS *)malloc (sizeof (ADDRESS));

/* CHECK THE MEMORY! */ printf ("Name> "); fgets (new_name->name, MAXLEN, stdin); printf ("Address> "); fgets (new_name->address, MAXLEN, stdin); printf ("Tel> "); fgets (new_name->phone, MAXLEN, stdin); /* Chain the new item into the list */ new_name->next= hol; hol= new_name;}

Page 15: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Search in the Address BookADDRESS * search (char *name) /* look for a particular name in our address book */{ ADDRESS *p = hol; while(p != NULL && !strcmp(p->name, name)) { p = p->next; } return p;}

Page 16: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Delete in the Address Bookvoid delete(char *name) /* delete a particular name in our address book */{ ADDRESS *p = hol; ADDRESS *q = hol; if(hol != NULL && strcmp(hol->name, name)) { hol = hol->next; p->next = NULL;

free(p); } else {

while(p != NULL && !strcmp(p->name, name)) { q = p; p = p->next; } q->next = p->next; p->next = NULL; free(p);}

}

Page 17: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

The Binary Tree• A binary tree is a method for storing

ordered data (for example a dictionary of words) where we wish to easily be able to add items and find items

• Each element in the tree can lead to two further elements – to the left and to the right, representing elements which are earlier in the alphabet and later in the alphabet respectively

Page 18: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

The binary tree

NULL NULL

NULL NULL NULL

NULLNULL

typedef struct tree { char word[100]; struct tree *left; struct tree *right;} TREE;

Page 19: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Binary Tree Pros & Cons

• Finding an element is O(log n)

• Adding an element is O(log n) – O(1) if we already know where to add it.

• Deleting an element may be complex

• Programming complexity is higher than a linked list (just about)

Page 20: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Deleting an entire binary tree

• I think this code is elegant and worth looking at: void delete_tree (TREE *ptr)

{ if (ptr == NULL) return; delete_tree(ptr->left); delete_tree(ptr->right); free (ptr);}

We can delete the whole tree with: delete_tree(root_of_tree);

Page 21: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Traversal

• Preorder Traversal

• Inorder Traversal

• Postorder Traversal

Page 22: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Traversaltypedef struct node{ char data; struct node *lchild,*rchild; }BinTNode; typedef BinTNode *BinTree;

BinTree CreatBinTree(void) { BinTree T; char ch; if((ch=getchar())==‘ ’) return(NULL); else{ T=(BinTNode *)malloc(sizeof(BinTNode)); T->data=ch; T->lchild=CreatBinTree(); T->rchild=CreatBinTree(); return(T); } }

Page 23: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Traversal

void Preorder(BinTree T) { if(T) { printf("%c",T->data); Preorder(T->lchild); Preorder(T->rchild); } }

Page 24: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Traversal

void Inorder(BinTree T) { if(T) { Inorder(T->lchild); printf(“%c”,T->data); Inorder(T->rchild); }}

Page 25: C Programming –Application of Pointers to Linked List and Binary Tree In this lecture we learn about: –Linked Lists –Binary Trees

Traversal

void Postorder(BinTree T) { if(T) { Postorder(T->lchild); Postorder(T->rchild); printf(“%c”,T->data); } }