Description

Binary Trees (and Big “O” notation). CS-2303 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

Transcripts

Paired Trees (and Big "O" documentation) CS-2303 System Programming Concepts (Slides incorporate materials from The C Programming Language , 2 nd version, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th releases, by Deitel and Deitel) Binary Trees

Definitions Linked List An information structure in which every component is progressively assigned and in which components indicate each other to characterize a straight relationship Singly-or doubly-connected Stack, line, round rundown Tree An information structure in which every component is powerfully distributed and in which every component has more than one potential successor Defines a halfway request Binary Trees

A connected rundown however with two connections for every thing struct treeItem { sort payload; treeItem *left; treeItem *right; }; payload left right Binary Tree Binary Trees

Binary Tree (proceeded with) Binary tree needs a root struct treeItem { sort payload; treeItem *left; treeItem *right; }; struct treeItem *root; Binary trees frequently drawn with root at top! Not at all like conventional trees in the timberland More like the root frameworks of a tree Binary Trees

Definitions (proceeded with) See Deitel & Deitel, §12.7 K & R, §6.5 Binary Tree A tree in which every component has two potential successors Subtree The arrangement of hubs that are successors to a particular hub, either specifically or by implication Root of a tree The hub of the tree that is not the successor to some other hub, every other hub are (straightforwardly or in a roundabout way) successors to it Binary Trees

struct treeItem { sort payload; treeItem *left; treeItem *right; }; struct treeItem *root; payload left right Binary Tree Binary Trees

Purpose of a Tree (Potentially) an extensive information structure Capable of putting away a lot of things In a methodical route Need to discover things by esteem I.e., need to seek through the information structure to check whether it contains a thing with the esteem we Need to include new things If esteem is not as of now in the tree, include another thing … so it can be effortlessly found in future Why not utilize a connected rundown? Parallel Trees

Look recursively down succession of branches until either Desired hub is found; or Null branch is experienced Replace with ptr to new thing Decide which branch to take after in light of payload left right Searching and Adding to a Binary Tree Binary Trees

Example — Searching a Tree typedef struct _treeItem { burn *word; //a portion of payload int count; //some portion of payload _treeItem *left, *right; } treeItem; treeItem *findItem(treeItem *p, scorch *w) { if (p == NULL) return NULL; //thing not discovered int 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

Example — Adding an Item treeItem *addItem(treeItem *p, roast *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? Parallel Trees

Question:– what number of calls to addItem for a tree with 10 6 hubs? Accept adjusted I.e., approx same number of hubs on each subtree payload left right Binary Tree Binary Trees

Answer Approximately 20 calls to addItem Note:– 2 10 = 1024 10 3 Therefore 10 6 2 20 Therefore it takes around 20 two-path branches to cover 10 6 things! What number of examinations would it take to look a connected rundown of 10 6 things? Twofold Trees

Observation Problems like this happen, all things considered, all the time Need to keep up a ton of information Usually arbitrary Need to seek through it immediately Need to include (or erase) things progressively Need to sort "on the fly" I.e., as you are including and additionally erasing things Binary Trees

Questions? Paired Trees

Binary Trees (proceeded with) Binary tree does not should be "adjusted" i.e., with estimated same # of hubs swinging from right or left However, it frequently assists with execution Multiply-extended trees Like parallel trees, yet with more than two connections for each hub Binary Trees

"Huge O" notation:– signifies "request of" Binary Trees (proceeded with) Binary tree does not should be "adjusted" i.e., with surmised same # of hubs dangling from right or left However, it encourages with execution Time to achieve a leaf hub is O( log 2 n ), where n is number of hubs in tree Multiply-stretched trees Like twofold trees, yet with more than two connections for every hub Binary Trees

Order of Traversing Binary Trees all together Traverse left sub-tree (all together) Visit hub itself Traverse right sub-tree (all together) Pre-arrange Visit hub first Traverse left sub-tree Traverse right sub-tree Post-arrange Traverse left sub-tree Traverse right sub-tree Visit hub last Binary Trees

Question Suppose we wish to print out the strings put away in the tree of the past case in order arrange? What traversal request of the tree would it be a good idea for us to utilize? Double Trees

Another Example of Binary Tree x = (a.real*b.imag - b.real*a.imag)/sqrt(a.real*b.real – a.imag*b.imag) = x/sqrt - * … . . . . a genuine b imag b genuine an imag Binary Trees

Question What sort of traversal request is required for this expression? All together? Pre-arrange? Post-arrange? Paired Trees

Binary Trees in Compilers Used to speak to the structure of the ordered program Optimizations Common sub-expression location Code disentanglement Loop unrolling Parallelization Reductions in quality – e.g., substituting increments for duplications, and so forth. Numerous others Note: Deitel & Deitel, Ch 12 works out, contain an arrangement on building a compiler Binary Trees

Questions? Parallel Trees

"Enormous O" documentation New Topic Binary Trees

I.e., relative to n , the quantity of things in the rundown Linked Lists Again Linear information structure Easy to develop and recoil Easy to add and erase things Time to hunt down a thing – O ( n ) Binary Trees

I.e., corresponding to log of number of things in the rundown Binary Trees Again Non-straight information structure Easy to develop and contract Easy to add and erase things Time to scan for a thing – O (log n ) Binary Trees

Definition — Big-O "Of the request of … " A portrayal of the quantity of operations in a calculation as far as a numerical capacity of the quantity of information things included O ( n ) implies that the quantity of operations to finish the calculation is relative to n E.g., looking a rundown with n things requires, by and large, n/2 correlations with payloads Binary Trees

Big-O (proceeded with) O ( n ): relative to n – i.e., direct O ( n 2 ): relative to n 2 – i.e., quadratic O ( k n ) – relative to k n – i.e., exponential … O (log n ) – corresponding to log n – i.e., sublinear O ( n log n ) Worse than O ( n ), superior to O ( n 2 ) O ( 1 ) – free of n ; i.e., steady Binary Trees

Anecdote & Questions:– In the plan of electronic adders, what is the request of the convey spread? What is the request of drifting point separate? What is the request of gliding point square root? What program have we contemplated in this course is O ( 2 n )? i.e., exponential? Double Trees

Questions on Big-O ? Twofold Trees