CS350 CH06 Non Binary Trees

download CS350 CH06 Non Binary Trees

of 18

Transcript of CS350 CH06 Non Binary Trees

  • 8/8/2019 CS350 CH06 Non Binary Trees

    1/18

    1

    Chapter 06 Non-Binary Trees

    Chaminade University of Honolulu

    Department of Computer Science

    Text by Clifford ShafferModified by Dr. Martins

    CS350-Data Structures

  • 8/8/2019 CS350 CH06 Non Binary Trees

    2/18

    2

    General Trees

  • 8/8/2019 CS350 CH06 Non Binary Trees

    3/18

    3

    General Tree Node

    // General tree node ADTtemplate class GTNode {public:GTNode(const Elem&); // Constructor

    ~GTNode(); // DestructorElem value(); // Return valuebool isLeaf(); // TRUE if is a leafGTNode* parent(); // Return parentGTNode* leftmost_child(); // First childGTNode* right_sibling(); // Right siblingvoid setValue(Elem&); // Set valuevoid insert_first(GTNode* n);void insert_next(GTNode* n);void remove_first(); // Remove first childvoid remove_next(); // Remove sibling

    };

  • 8/8/2019 CS350 CH06 Non Binary Trees

    4/18

    4

    General Tree Traversal

    template void GenTree::printhelp(GTNode* subroot) {

    if (subroot->isLeaf()) cout

  • 8/8/2019 CS350 CH06 Non Binary Trees

    5/18

  • 8/8/2019 CS350 CH06 Non Binary Trees

    6/18

    6

    Equivalence Class Problem

    The parent pointer representation is good foranswering:

    Are two elements in the same tree?

    // Return TRUE if nodes in different treesbool Gentree::differ(int a, int b) {int root1 = FIND(a); // Find root for aint root2 = FIND(b); // Find root for breturn root1 != root2; // Compare roots

    }

  • 8/8/2019 CS350 CH06 Non Binary Trees

    7/18

    7

    Union/Find

    void Gentree::UNION(int a, int b) {int root1 = FIND(a); // Find root for aint root2 = FIND(b); // Find root for bif (root1 != root2) array[root2] = root1;

    }int Gentree::FIND(int curr) const {while (array[curr]!=ROOT) curr = array[curr];return curr; // At root

    }

    Want to keep the depth small.Weighted union rule: Join the tree with fewer nodes

    to the tree with more nodes.

  • 8/8/2019 CS350 CH06 Non Binary Trees

    8/18

    8

    Equiv Class Processing (1)

  • 8/8/2019 CS350 CH06 Non Binary Trees

    9/18

    9

    Equiv Class Processing (2)

  • 8/8/2019 CS350 CH06 Non Binary Trees

    10/18

    10

    Path Compression

    int Gentree::FIND(int curr) const {if (array[curr] == ROOT) return curr;return array[curr] = FIND(array[curr]);

    }

  • 8/8/2019 CS350 CH06 Non Binary Trees

    11/18

    11

    Lists of Children

  • 8/8/2019 CS350 CH06 Non Binary Trees

    12/18

    12

    Leftmost Child/Right Sibling (1)

  • 8/8/2019 CS350 CH06 Non Binary Trees

    13/18

    13

    Leftmost Child/Right Sibling (2)

  • 8/8/2019 CS350 CH06 Non Binary Trees

    14/18

    14

    Linked Implementations (1)

  • 8/8/2019 CS350 CH06 Non Binary Trees

    15/18

    15

    Linked Implementations (2)

  • 8/8/2019 CS350 CH06 Non Binary Trees

    16/18

    16

    Sequential Implementations (1)

    List node values in the order they would be visitedby a preorder traversal.

    Saves space, but allows only sequential access.

    Need to retain tree structure for reconstruction.

    Example: For binary trees, us a symbol to marknull links.

    AB/D//CEG///FH//I//

  • 8/8/2019 CS350 CH06 Non Binary Trees

    17/18

    17

    Sequential Implementations (2)

    Example: For full binary trees, mark nodesas leaf or internal.

    AB/DCEG/FHI

    Example: For general trees, mark the end ofeach subtree.

    RAC)D)E))BF)))

  • 8/8/2019 CS350 CH06 Non Binary Trees

    18/18

    18

    Converting to a Binary Tree

    Left child/right sibling representationessentially stores a binary tree.

    Use this process to convert any general treeto a binary tree.

    A forest is a collection of one or more

    general trees.