Binary trees and big o notation l.jpg
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Binary Trees (and Big “O” notation) PowerPoint PPT Presentation


  • 744 Views
  • Updated On :
  • Presentation posted in: General

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

Related searches for Binary Trees (and Big “O” notation)

Download Presentation

Binary Trees (and Big “O” notation)

An Image/Link below is provided (as is) to download presentation

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.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Binary trees and big o notation l.jpg

Binary Trees(and Big “O” notation)

CS-2303System 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


Definitions l.jpg

Definitions

  • Linked List

    • A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship

    • Singly- or doubly-linked

    • Stack, queue, circular list

  • Tree

    • A data structure in which each element is dynamically allocated and in which each element has more than one potential successor

    • Defines a partial order

  • Binary Trees


    Binary tree l.jpg

    A linked list but with two links per item

    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 Tree

    Binary Trees


    Binary tree continued l.jpg

    Binary Tree (continued)

    • Binary tree needs a root

      struct treeItem {type payload;treeItem *left; treeItem *right;

      };

      struct treeItem *root;

    • Binary trees often drawn with root at top!

      • Unlike ordinary trees in the forest

      • More like the root systems of a tree

    Binary Trees


    Definitions continued l.jpg

    Definitions (continued)

    See Deitel & Deitel, §12.7K & R, §6.5

    • Binary Tree

      • A tree in which each element has two potential successors

  • Subtree

    • The set of nodes that are successors to a specific node, either directly or indirectly

  • Root of a tree

    • The node of the tree that is not the successor to any other node, all other nodes are (directly or indirectly) successors to it

  • Binary Trees


    Binary tree6 l.jpg

    struct treeItem {type payload;treeItem *left; treeItem *right;

    };

    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 Tree

    Binary Trees


    Purpose of a tree l.jpg

    Purpose of a Tree

    • (Potentially) a very large data structure

      • Capable of storing very many items

      • In an orderly way

  • Need to find items by value

    • I.e., need to search through the data structure to see if it contains an item with the value we want

  • Need to add new items

    • If value is not already in the tree, add a new item …

    • …so that it can be easily found in future

  • Why not use a linked list?

  • Binary Trees


    Searching and adding to a binary tree l.jpg

    Look recursively down sequence of branches until either

    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 Tree

    Binary Trees


    Example searching a tree l.jpg

    Example — Searching a Tree

    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);elsereturn findItem(p->right, w);

    }

    Binary Trees


    Example adding an item l.jpg

    Example — Adding an Item

    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);elsep->right = addItem(p->right, w);return p;

    }

    Why do this?

    Binary Trees


    Binary tree11 l.jpg

    Question:– how many calls to addItem for a tree with 106 nodes?

    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 Tree

    Binary Trees


    Answer l.jpg

    Answer

    • Approximately 20 calls to addItem

    • Note:–

      • 210 = 1024  103

      • Therefore 106  220

      • Therefore it takes approximately 20 two-way branches to cover 106 items!

    • How many comparisons would it take to search a linked list of 106 items?

    Binary Trees


    Observation l.jpg

    Observation

    • Problems like this occur in real life all the time

    • Need to maintain a lot of data

      • Usually random

  • Need to search through it quickly

  • Need to add (or delete) items dynamically

  • Need to sort “on the fly”

    • I.e., as you are adding and/or deleting items

  • Binary Trees


    Questions l.jpg

    Questions?

    Binary Trees


    Binary trees continued l.jpg

    Binary Trees (continued)

    • Binary tree does not need to be “balanced”

      • i.e., with approximate same # of nodes hanging from right or left

  • However, it often helps with performance

  • Multiply-branched trees

    • Like binary trees, but with more than two links per node

  • Binary Trees


    Binary trees continued16 l.jpg

    “Big-O” notation:– means “order of”

    Binary Trees (continued)

    • Binary tree does not need to be “balanced”

      • i.e., with approximate same # of nodes hanging from right or left

  • However, it helps with performance

    • Time to reach a leaf node is O(log2 n), where n is number of nodes in tree

  • Multiply-branched trees

    • Like binary trees, but with more than two links per node

  • Binary Trees


    Order of traversing binary trees l.jpg

    Order of Traversing Binary Trees

    • In-order

      • Traverse left sub-tree (in-order)

      • Visit node itself

      • Traverse right sub-tree (in-order)

  • Pre-order

    • Visit node first

    • Traverse left sub-tree

    • Traverse right sub-tree

  • Post-order

    • Traverse left sub-tree

    • Traverse right sub-tree

    • Visit node last

  • Binary Trees


    Question l.jpg

    Question

    • Suppose we wish to print out the strings stored in the tree of the previous example in alphabetical order?

    • What traversal order of the tree should we use?

    Binary Trees


    Another example of binary tree l.jpg

    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

    real

    b

    imag

    b

    real

    a

    imag

    Binary Trees


    Question20 l.jpg

    Question

    • What kind of traversal order is required for this expression?

      • In-order?

      • Pre-order?

      • Post-order?

    Binary Trees


    Binary trees in compilers l.jpg

    Binary Trees in Compilers

    • Used to represent the structure of the compiled program

    • Optimizations

      • Common sub-expression detection

      • Code simplification

      • Loop unrolling

      • Parallelization

      • Reductions in strength – e.g., substituting additions for multiplications, etc.

      • Many others

    Note: Deitel & Deitel, Ch 12 exercises,

    contain a series on building a compiler

    Binary Trees


    Questions22 l.jpg

    Questions?

    Binary Trees


    Big o notation l.jpg

    “Big O” notation

    New Topic

    Binary Trees


    Linked lists again l.jpg

    I.e., proportional to n, the number of items in the list

    Linked Lists Again

    • Linear data structure

    • Easy to grow and shrink

    • Easy to add and delete items

    • Time to search for an item – O(n)

    Binary Trees


    Binary trees again l.jpg

    I.e., proportional to log of number of items in the list

    Binary Trees Again

    • Non-linear data structure

    • Easy to grow and shrink

    • Easy to add and delete items

    • Time to search for an item – O(log n)

    Binary Trees


    Definition big o of the order of l.jpg

    Definition — Big-O“Of the order of …”

    • A characterization of the number of operations in an algorithm in terms of a mathematical function of the number of data items involved

    • O(n) means that the number of operations to complete the algorithm is proportional to n

    • E.g., searching a list with n items requires, on average, n/2 comparisons with payloads

    Binary Trees


    Big o continued l.jpg

    Big-O(continued)

    • O(n): proportional to n – i.e., linear

    • O(n2): proportional to n2 – i.e., quadratic

    • O(kn) – proportional to kn – i.e., exponential

    • O(log n) – proportional to log n – i.e., sublinear

    • O(n log n)

      • Worse than O(n), better than O(n2)

  • O(1) – independent of n; i.e., constant

  • Binary Trees


    Anecdote questions l.jpg

    Anecdote & Questions:–

    • In the design of electronic adders, what is the order of the carry-propagation?

    • What is the order of floating point divide?

    • What is the order of floating point square root?

    • What program have we studied in this course that is O(2n)? i.e., exponential?

    Binary Trees


    Questions on big o l.jpg

    Questions on Big-O?

    Binary Trees


  • Login