binary tree illustrated

Upload: milind-shah

Post on 04-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Binary Tree Illustrated

    1/56

    1

    Chapter 10:

    Binary Trees

  • 7/30/2019 Binary Tree Illustrated

    2/56

    2

    Outline

    Tree Structures

    Tree Node Level and Path Length

    Binary Tree Definition

    Binary Tree Nodes

    Binary Search Trees Locating Data in a Tree

    Removing a Binary Tree Node

    stree ADT

    Application of Binary Search Trees- Removing Duplicates

    Update Operations

    Insert & remove an item

  • 7/30/2019 Binary Tree Illustrated

    3/56

    3

    Associative containers

    Sequence containers (eg. array, vector, list)access items by position, A[i] or V[i]

    Associative containers (eg. Sets and maps)

    access items by key (or by value). Objects are added, removed, or updated by

    value rather than by position.

    The underlying data structure for STL

    associative container is the binary searchtree, which allow operations (insert, delete,find) in O(logn) time at worst case.

  • 7/30/2019 Binary Tree Illustrated

    4/56

    4

    Tree Structures

    President-CEO

    Production

    ManagerSales

    Manager

    Shipping

    Supervisor

    Personnel

    Manager

    Warehouse

    Supervisor

    Purchasing

    Supervisor

    HIERARCHICAL TREE STRUC TURE

    Arrays, vectors, lists are linearstructures, one elementfollows another. Trees are hierarchical structure.

  • 7/30/2019 Binary Tree Illustrated

    5/56

    5

    Tree Structures

    +

    e

    /

    ba

    *

    dc

    -

    BINARY EXPRESSION TREE FOR "a*b + (c-d) / e"

  • 7/30/2019 Binary Tree Illustrated

    6/56

    6

    Tree Terminology

    Tree is a set of nodes. The set may be empty. If not

    empty, then there is a distinguished node r, called

    root, all other nodes originating from it, and zero or

    more non-empty subtrees T1

    ,T2

    , ,Tk

    , each of whose

    roots are connected by a directed edge from r.

    (inductive definition)

    T1 T2 Tk

    r

  • 7/30/2019 Binary Tree Illustrated

    7/56

    7

    Tree Terminology

    A

    JI

    HGFE

    DCB

    (a)

    (b)

    A GENERAL TREE

    root

    Nodes in subtrees are called successors or

    descendents of the root node

    An immediate successor is called a child

    A leaf node is a node without any children whilean interior node has at least one child.

    The link between node describes the parent-

    child relation.

    The link between parent and child is called edge

    A path between a parent P and any node N in itssubtrees is a sequence of nodes P= X0,X1,

    ,Xk=N, where k is the length of the path. Each

    node Xi in the path is the parent of Xi+1, (0i

  • 7/30/2019 Binary Tree Illustrated

    8/56

    8

    Tree Node Level and Path Length

    A

    HG

    FE

    DCB

    Level: 0

    Level: 1

    Level: 2

    Level: 3

    The level of a node N in a tree is the length of the path from root to N.

    Root is at level 0.

    The depth of a tree is the length of the longest path from root to any

    node.

  • 7/30/2019 Binary Tree Illustrated

    9/56

    9

    Binary Tree

    In a binary tree, no node has more than two

    children, and the children are distinguished as

    left and right.

    A binary tree T is a finite set of nodes with onethe following properties:

    1. T is a tree if the set of nodes is empty.

    2. The set consists of a root R and exactly two

    distinct binary trees. The left subtree TL andthe right subtree TR, either or both subtree

    may be empty.

  • 7/30/2019 Binary Tree Illustrated

    10/56

    10

    Examples of Binary Trees

    A

    E

    D

    C

    B

    A

    F

    H

    ED

    CB

    I

    Tree A

    Size 9 Depth 3

    Tree B

    Size 5 Depth 4

    G

    Size? depth?

  • 7/30/2019 Binary Tree Illustrated

    11/56

    11

    Density of a Binary Tree

    At any level n, a binary tree may contain from

    1 to 2n nodes. The number of nodes per level

    contributes to the density of the tree.

    Degenerate tree: there is a single leaf nodeand each interior node has only one child.

    An n-node degenerate tree has depth n-1

    Equivalent to a linked list

    A complete binary tree of depth n is a tree inwhich each level from 0 to n-1 has all possible

    nodes and all leaf nodes at level n occupy the

    leftmost positions in the tree.

  • 7/30/2019 Binary Tree Illustrated

    12/56

    12

    Complete or noncomplete?

    A

    ED

    CB

    GF

    Complete Tree (Depth 2)

    Full with all possible nodes

  • 7/30/2019 Binary Tree Illustrated

    13/56

    13

    Complete or noncomplete?

    A

    ED

    CB

    IH

    Non-Complete Tree (Depth 3)

    Level 2 is missing nodes

  • 7/30/2019 Binary Tree Illustrated

    14/56

    14

    Complete or noncomplete?

    A

    ED

    CB

    GF

    KIH

    Non-CompleteTree (Depth 3)

    Nodes at level 3 do not occurpy leftmost positionsoccupy

  • 7/30/2019 Binary Tree Illustrated

    15/56

    15

    Complete or noncomplete?

    A

    E

    D

    CB

    GF

    JIH

    Complete Tree (Depth 3)

  • 7/30/2019 Binary Tree Illustrated

    16/56

    16

    Evaluating tree density

    Max density: how many maximum nodes in a

    binary tree of depth d?

    Hint: At level k can have at most 2k nodes

    Proof (by induction on depth)

  • 7/30/2019 Binary Tree Illustrated

    17/56

    17

    Find depth

    Whats the depth of a complete binary tree

    with n nodes? A perfect complete BT with depth d: aCBT with 2d+1-1

    nodes

    The smallest CBT with depth d: (2(d-1)+1-1)+1=2d

  • 7/30/2019 Binary Tree Illustrated

    18/56

    18

    Implementing of BT

    A binary tree node contains a data value,

    called nodeValue, and two pointers, left and

    right. A value of NULL indicates an empty

    subtree. Declaration of CLASS tnode, d_tnode.h

    Abstract Tree Model

    A

    E F

    H

    D

    CB

    G

    left A right

    Tree Node Model

    left B right

    left E right

    left G right

    left D right

    left C right

    left H right

    left F right

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_tnode.hhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_tnode.h
  • 7/30/2019 Binary Tree Illustrated

    19/56

    19

    Building a BT

    A binary tree consists of a collection of

    dynamically allocated tnode objects whose

    pointer values specify links to their children.

    Example 1:

    Example 2:

    4

    8

    root

    p

    10

    20

    40

    30

    q

    p

    q

    r

    Build a tree from the bottom up:

    allocate the children first and then the parent

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/Fig%2010-8.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/Example%2010-3.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/Example%2010-3.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/Fig%2010-8.rtf
  • 7/30/2019 Binary Tree Illustrated

    20/56

    20

    Tree traversals

    In a traversal, we visit each node in a tree in

    some order.

    visit means perform some operation at the

    node. (eg. print the value, change the value,remove the node)

    Four fundamental traversals

    Recursive: pre-order, in-order, post -order

    Iterative: level-order

  • 7/30/2019 Binary Tree Illustrated

    21/56

    21

    Recursive tree traversals

    In-order: (LNR)

    1. Traverse the left subtree (go left);

    2. Visit the node

    3. Traverse the right subtree (go right)

    Pre-order: (NLR)

    postorder: (LRN)

    RNL, NRL, RLN Example:

    A

    B

    E

    C

    HG

    FD

    I

    A

    B

    E

    C

    D

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/inorderOutput.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/inorderOutput.rtf
  • 7/30/2019 Binary Tree Illustrated

    22/56

    22

    Iterative tree traversals

    Level-order:

    access elements by levels, with the root

    coming first (level 0), then the children of the

    root (level 1), followed by the next generation(level 2), and so forth.

    usually done as iterative algorithm using a

    queue

    Example:

    A

    B

    E

    C

    HG

    FD

    I

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/levelorderOutput.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/levelorderOutput.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/levelorderOutput.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/levelorderOutput.rtf
  • 7/30/2019 Binary Tree Illustrated

    23/56

    23

    Count leaves of BT

    A node in a tree is a leaf if it has no children.

    Scan each node and check for the presence

    of children

    The order of the scan irrelevant

    Example: using pre-orderpattern

    (n)

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/countLeaf.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/countLeaf.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/countLeaf.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/countLeaf.rtf
  • 7/30/2019 Binary Tree Illustrated

    24/56

    24

    Depth of BT

    The depth of a tree is the length of the

    longest path from root to any node.

    1+ max{depth(TL), depth(TR)}

    use post-order traversal (n)

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/depth.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/depth.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/depth.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/depth.rtf
  • 7/30/2019 Binary Tree Illustrated

    25/56

    25

    Copy BT

    Make new nodes for the copy

    Use post-order traversal: the node is created

    after copying the subtrees.

    (n)

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/copyTree2.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/copyTree2.rtf
  • 7/30/2019 Binary Tree Illustrated

    26/56

    26

    Delete tree node & clear BT

    Delete a tree node: (Post-order) delete all the

    nodes in both subtrees, then delete the node

    Clear BT: deletes all nodes, then points the

    root to NULL (delete the root node)

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/deleteTree.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/clearTree.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/clearTree.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/deleteTree.rtf
  • 7/30/2019 Binary Tree Illustrated

    27/56

    27

    Binary Search Trees

    A BST is a binary tree

    in which, at every node,

    the data value in the left

    subtree are less than

    the value at the nodeand the value in the

    right subtree are

    greater.

    Not allow duplicatedvalue

    6530

    3710

    25

    15

    Binary Search Tree 1

    30

    59

    62

    50

    Binary Search Tree 3

    Binary Search Tree 2

    53

  • 7/30/2019 Binary Tree Illustrated

    28/56

    28

    Build a BST

    The first element entering a BST becomes the root node

    Subsequence elements entering the tree:

    If the value of the new element is less than the value of thecurrent node, proceed to the left subtree of the node

    If the left subtree is not empty, repeat the process by

    comparing item with the root of the subtree If the left subtree is empty, allocate a new node with item as its

    value, and attach the node to the tree as the left child

    If the value of the new element is greaterthan the value ofthe current node, proceed to the right subtree of the node

    If the right subtree is not empty, repeat the process bycomparing item with the root of the subtree

    If the right subtree is empty, allocate a new node with item asits value, and attach the node to the tree as the right child

    If the value of the new element is equal to the value of thecurrent node, perform no action (no duplicate)

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/insert.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/insert.rtf
  • 7/30/2019 Binary Tree Illustrated

    29/56

    29

    Examples

    Example 1: Start with empty tree, and insert35,18,25, 48,72, 60 in sequence

    Example 2: Start with empty tree, and Insert18,25,48,60,72

    Example 3: Start with empty tree, and insert72,60, 48,35,25,18

    Insert a node: O(depth)

    Depth, worst case, is O(n) Best (and average) case is O(lgn)

    Time complexity to build a BST?

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/insert.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/insert.rtf
  • 7/30/2019 Binary Tree Illustrated

    30/56

    30

    50

    62373210

    60533525

    5530

    15

    Current Node ActionRoot = 50 Compare item = 37 and 50

    37 < 50, move to the left subtree

    Node = 30 Compare item = 37 and 30

    37 > 30, move to the right subtree

    Node = 35 Compare item = 37 and 3537 > 35, move to the right subtree

    Node = 37 Compare item = 37 and 37. Item found.

    Locating data in a BST: O(depth)

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/find.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/find.rtf
  • 7/30/2019 Binary Tree Illustrated

    31/56

    31

    Removing a Binary Search Tree

    Node

    6530

    3710

    25

    15

    \\ //

    Delete node 25

    30

    6510

    37

    1565

    3710

    30

    15

    Bad Solution: 30 is out of placeGood Solution

    (a) (b)

    We must maintain the BST property

    To delete a node r of a BST

    1. if r is a leaf, just remove it

    2. otherwise, either

    a. replace r with the largestnode in its left subtree

    b. replace r with the smallestnode in its right subtree

    CLASS t C t t d t h

  • 7/30/2019 Binary Tree Illustrated

    32/56

    32

    CLASS stree Constructors d_stree.h

    stree();

    Create an empty search tree.

    stree(T *first, T *last);

    Create a search tree with the elements from thepointer range [first, last).

    CLASS stree Opertions d_stree.hvoid displayTree(int maxCharacters);

    Display the search tree. The maximum number ofcharacters needed to output a node value is

    maxCharacters.bool empty();

    Return true if the tree is empty and false otherwise.

    CLASS t O ti d t h

  • 7/30/2019 Binary Tree Illustrated

    33/56

    33

    CLASS stree Opertions d_stree.h

    int erase(const T& item);

    Search the tree and remove item, if it is present;

    otherwise, take no action. Return the number ofelements removed.

    Postcondition: If item is located in the tree, the sizeof the tree decreases by 1.

    void erase(iterator pos);Erase the item pointed to the iterator pos.

    Precondition: The tree is not empty and pos pointsto an item in the tree. If the iterator is invalid, the

    function throws the referenceError exception.Postcondition: The tree size decreases by 1.

    CLASS st O ti d t h

  • 7/30/2019 Binary Tree Illustrated

    34/56

    34

    CLASS stree Opertions d_stree.h

    void erase(iterator first, iterator last);

    Remove all items in the iterator range [first, last).

    Precondition: The tree is not empty. If empty, thefunction throws the underflowError exception.

    Postcondition: The size of the tree decreases by thenumber of items in the range.

    iteratorfind(const T& item);Search the tree by comparing item with the datavalues in a path of nodes from the root of the tree. If amatch occurs, return an iterator pointing to the

    matching value in the tree. If item is not in the tree,return the iterator value end().

    CLASS stree Opertions d stree h

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_stree.hhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_stree.h
  • 7/30/2019 Binary Tree Illustrated

    35/56

    35

    CLASS stree Opertions d_stree.h

    Pair insert(const T& item);

    If item is not in the tree, insert it and return an iterator-

    bool pair where the iterator is the location of the newelement and the Boolean value is true. If item isalready in the tree, return the pair where the iteratorlocates the existing item and the Boolean value isfalse.

    Postcondition: The size of the tree is increased by 1if item is not present in the tree.

    int size();

    Return the number of elements in the tree.

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_stree.hhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_stree.h
  • 7/30/2019 Binary Tree Illustrated

    36/56

    36

    Using Binary Search TreesApplication:Removing Duplicates

    7

    52

    37 3 2 5 3 2 9 3 2 3 5 7 9

    9v v

    Use a BST as a filter:

    1) Use vector iterator to copy vector elements to BST2) Clear the vector

    3) Use stree iterator to re-fill the vector

    Time complexity?

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/removeDuplicates.txthttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/removeDuplicates.txt
  • 7/30/2019 Binary Tree Illustrated

    37/56

    37

    Using Binary Search TreesApplication: The Video Store

    A video store maintains an inventory of movies that includes multiples

    titles. When a customer makes an inquiry, the clerk checks the

    inventory to see whether the title is available. If so, the rental

    transaction will decreases the number of copies of the title in inventory

    and increments a similar rented-film entry. When a customer returns a

    film, the clerk reverses the process by removing a copy from thecollection of rented films and adding it to the inventory.

    --- two stree objects: inventory and rentals.

    d_video.h, prg10_5

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_video.hhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/prg10_5.cpphttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/prg10_5.cpphttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/d_video.h
  • 7/30/2019 Binary Tree Illustrated

    38/56

    38

    Implementing the Stree class

    stnode object: three pointers, one value

    The stree class declaration

    public section

    private section

    findNode( ) findNodeRec()

    left parent nodeValue right

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/stnode.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/private_d_stree.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/findNode.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/findNodeRec.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/findNodeRec.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/findNode.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/private_d_stree.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/stnode.rtf
  • 7/30/2019 Binary Tree Illustrated

    39/56

    39

    Update Operations: insert()

    take a new data item, search the tree, add it in the correctlocation, and return iterator-bool pair

    if item is in the tree, return a pair whose iterator componentpoints at the existing item and whose bool component isfalse.

    if item is not in the tree, insert it and return a pair whoseiterator component points at item and whose boolcomponent is true. And the tree size increasesby1

    Iteratively traverses the path of the left and right subtress

    Maintains the current node and parent of the current node Terminates when an empty subtree is found

    New node replaces the NULL subtree as a child of theparent

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/insert_stree.rtfhttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/insert_stree.rtf
  • 7/30/2019 Binary Tree Illustrated

    40/56

    40

    Example: insert 32: 1ndof 3 steps

    1)- The function begins at the root node and compares item 32

    with the root value 25. Since 32 > 25, we traverse the right

    subtree and look at node 35.

    25

    4012

    3520

    parent

    t

    (a)

    Step 1: Compare 32 and 25.

    Traverse the right subtree.

  • 7/30/2019 Binary Tree Illustrated

    41/56

    41

    insert: 2ndof 3 steps

    2)- Considering 35 to be the root of its own subtree, wecompare item 32 with 35 and traverse the left subtree of

    35.

    (b)

    Step 2: Compare 32 and 35.

    Traverse the left subtree.

    25

    4012

    3520

    t

    parent

  • 7/30/2019 Binary Tree Illustrated

    42/56

    42

    insert: 3ndof 3 steps

    3)- Create a leaf node with data value 32. Insert thenew node as the left child of node 35.

    newNode = getSTNode(item,NULL,NULL,parent);

    parent->left = newNode;

    (c)

    Step 3: Insert 32 as left child

    of parent 35

    25

    4012

    3520 parent

    32

  • 7/30/2019 Binary Tree Illustrated

    43/56

    43

    Update Operations: erase()

    Remove a node, maintain BST property

    D = node to be removed

    P = parent of D

    R = node that will replace D

  • 7/30/2019 Binary Tree Illustrated

    44/56

    44

    Case 1: D is a leaf

    10D

    P

    Delete leaf node 10.

    pNodePtr->left is dNode

    No replacement is necessary.

    pNodePtr->left is NULL

    Before After

    40

    35

    6530

    50

    3326

    25

    3429

    28

    P

    40

    35

    6530

    50

    3326

    25

    3429

    28

    Update the parent node P to have an empty subtree

    C 2 h l f hild b

  • 7/30/2019 Binary Tree Illustrated

    45/56

    45

    Case 2: D has a left child but no

    right child

    D

    P

    Delete node 35 with only a left child:

    Node R is the left child.

    Before

    R

    40

    35

    6530

    50

    3310 26

    25

    3429

    28

    P

    Attach node R to the parent.

    After

    R

    40

    33

    6530

    50

    1026

    25

    34

    29

    28

    Attach the left subtree of D (the tree with root R) to the parent P

    C 3 D h i h hild b

  • 7/30/2019 Binary Tree Illustrated

    46/56

    46

    Case 3: D has a right child but no

    left child

    P

    Delete node 26 with only a right child:

    Node R is the right child.

    P

    Attach node R to the parent.

    Before After

    R

    R

    40

    35

    6530

    50

    3310 26

    25

    3429

    28

    40

    35

    6530

    50

    3310 29

    25

    3428

    D

    Attach the right subtree of D (the tree with root R) to the parent P

  • 7/30/2019 Binary Tree Illustrated

    47/56

    47

    Code implementing of cases 1,2, & 3

    // assign pNodePtr the address of PpNodePtr = dNodePtr->parent;

    // If D has a NULL pointer, the

    // replacement node is the other child

    if (dNodePtr->left == NULL || dNodePtr->right == NULL)

    {if (dNodePtr->right == NULL)

    rNodePtr = dNodePtr->left;

    else

    rNodePtr = dNodePtr->right;

    if (rNodePtr != NULL) // D was not a leaf

    // the parent of R is now the parent of D

    rNodePtr->parent = pNodePtr;

    }

    C 4 D h b h l f d i h

  • 7/30/2019 Binary Tree Illustrated

    48/56

    48

    Case 4: D has both left and right

    non-empty subtrees

    R = leftmost (smallest) node in DR

    (right subtree of D)

    Delete R from DR

    Replace D with R

    Tree size --

  • 7/30/2019 Binary Tree Illustrated

    49/56

    49

    Example 1 for Case 4: remove 25

    P

    D

    R

    pOfRNodePtr = dNodePtr

    rNodePtr

    pNodePtr

    40

    35

    6530

    50

    3310 26

    25

    3429

    28

    P

    R

    R

    40

    35

    6530

    50

    3310

    26

    34

    29

    28

    Before replacing D by R After replacing D by R

  • 7/30/2019 Binary Tree Illustrated

    50/56

    50

    Example 2 for Case 4: remove 30

    40

    35

    6530

    Delete node 30 with two children.

    50

    3310 26

    25

    3429

    28

    40

    35

    65

    Orphaned subtrees.

    50

    3310

    25

    34

    26

    29

    28

  • 7/30/2019 Binary Tree Illustrated

    51/56

    51

    Example 2 for Case 4: remove 30

    R = leftmost (smallest) node in DR (right subtree of D)

    Delete R from DR

    Replace D with R

    P

    40

    35

    6533

    50

    3410

    26

    29

    28

    pNodePtr

    R

    rNodePtr

    P

    40

    35

    6530

    50

    3410

    26

    29

    28

    pNodePtr

    D33R

    rNodePtr

    Before replacing D by R After replacing D by R

    S Slid 1

    http://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/erase.cpphttp://localhost/var/www/apps/conversion/tmp/Codes/chapter%2010/erase.cpp
  • 7/30/2019 Binary Tree Illustrated

    52/56

    52

    52

    Summary Slide 1

    - trees- hierarchical structures that place elements in

    nodes along branches that originate from a root.

    - Nodes in a tree are subdivided into levels in whichthe topmost level holds the root node.

    - Any node in a tree may have multiple successors atthe next level. Hence a tree is a non-linear structure.

    - Tree terminology with which you should befamiliar:

    parent | child | descendents | leaf node | interiornode | subtree.

    S Slid 2

  • 7/30/2019 Binary Tree Illustrated

    53/56

    53

    53

    Summary Slide 2- Binary Trees

    - Most effective as a storage structure if it has highdensity

    - ie: data are located on relatively short paths from theroot.

    -A complete binary tree has the highest possibledensity

    - an n-node complete binary tree has depthint(log2n).

    - At the other extreme, a degenerate binary tree isequivalent to a linked list and exhibits O(n) accesstimes.

    S Slid 3

  • 7/30/2019 Binary Tree Illustrated

    54/56

    54

    54

    Summary Slide 3- Traversing Through a Tree

    - There are six simple recursive algorithms for treetraversal.

    - The most commonly used ones are:

    1) inorder (LNR)2) postorder (LRN)

    3) preorder (NLR).

    - Another technique is to move left to right fromlevel to level.

    - This algorithm is iterative, and its implementationinvolves using a queue.

    S Slid 4

  • 7/30/2019 Binary Tree Illustrated

    55/56

    55

    55

    Summary Slide 4

    -A binary search tree stores data byvalue instead of position- It is an example of an associative container.

    - The simple rules

    == return

    < go left

    > go right

    until finding a NULL subtree make it easy to build a

    binary search tree that does not allow duplicatevalues.

    S Slid 5

  • 7/30/2019 Binary Tree Illustrated

    56/56

    56

    Summary Slide 5

    - The insertion algorithm can be used to define thepath to locate a data value in the tree.

    - The removal of an item from a binary search tree ismore difficult and involves finding a replacementnode among the remaining values.