Binary Trees (and Big “O” notation). CS2303 System Programming Concepts (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Definitions. Linked List
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
CS2303System Programming Concepts
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
Binary Trees
Binary Trees
struct treeItem {type payload;treeItem *left; treeItem *right;
};
payload
payload
payload
payload
payload
payload
payload
left
left
left
left
left
left
left
right
right
right
right
right
right
right
Binary TreeBinary Trees
struct treeItem {type payload;treeItem *left; treeItem *right;
};
struct treeItem *root;
Binary Trees
See Deitel & Deitel, §12.7K & R, §6.5
Binary Trees
};
struct treeItem *root;
payload
payload
payload
payload
payload
payload
payload
left
left
left
left
left
left
left
right
right
right
right
right
right
right
Binary TreeBinary Trees
Binary Trees
Desired node is found; or
Null branch is encountered
Replace with ptr to new item
Decide which branch to follow based on payload
payload
payload
payload
payload
payload
payload
payload
left
left
left
left
left
left
left
right
right
right
right
right
right
right
Searching and Adding to a Binary TreeBinary Trees
typedef struct _treeItem {char *word; // part of payloadint count; // part of payload_treeItem *left, *right;} treeItem;
treeItem *findItem(treeItem *p, char *w) {if (p == NULL) return NULL; // item not foundint c = strcmp(w, p>word);if (c == 0) return p;else if (c < 0) return findItem(p>left, w);else return findItem(p>right, w);
}
Binary Trees
treeItem *addItem(treeItem *p, char *w) {if (p == NULL){ p = malloc(sizeof(treeItem)); char *c = malloc(strlen(w)+1); p>word = strcpy(c, w); p>count = 1; p>left = p>right = NULL; return p;};int c = strcmp(w, p>word);if (c == 0) p>count++;else if (c < 0) p>left = addItem(p>left, w);else p>right = addItem(p>right, w);return p;
}
Why do this?
Binary Trees
Assume balanced
I.e., approx same number of nodes on each subtree
payload
payload
payload
payload
payload
payload
payload
left
left
left
left
left
left
left
right
right
right
right
right
right
right
Binary TreeBinary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees
“BigO” notation:– means “order of”
Binary Trees (continued)Binary Trees
Binary Trees
Binary Trees
x = (a.real*b.imag  b.real*a.imag) / sqrt(a.real*b.real – a.imag*b.imag)
=
x
/
sqrt


*
*
…
.
.
.
.
a
real
b
imag
b
real
a
imag
Binary Trees
Binary Trees
Note: Deitel & Deitel, Ch 12 exercises,
contain a series on building a compiler
Binary Trees
Binary Trees
I.e., proportional to n, the number of items in the list
Linked Lists AgainBinary Trees
I.e., proportional to log of number of items in the list
Binary Trees AgainBinary Trees
Binary Trees
Binary Trees
Binary Trees
Binary Trees