1 / 13

Exploring Path Lengths and Tree Structures in Binary Trees

This lecture covers essential concepts in binary trees (BTs), including the relationship between internal paths length (IPL) and external paths length (EPL). We will prove the relationship EPL(ni) = IPL(ni) + 2*ni using induction, exploring examples and tree construction. Key topics include the unique identification of trees from traversal sequences and essential tree class methods in object-oriented programming. Prepare for Exam 1 with upcoming review questions and project results, and note that it will be held on Monday, the 16th.

kadeem
Télécharger la présentation

Exploring Path Lengths and Tree Structures in Binary Trees

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 341Lecture 9

  2. Announcements • Proj 1 results • Next week • Exam 1 Mon 16 • review questions will appear on website

  3. Path Lengths • There is a relationship between the IPL and EPL of full BTs. If ni is the number of internal nodes in a FBT, then • EPL(ni) = IPL(ni) + 2ni • Example: • ni = • EPL(ni) = • IPL(ni) = • 2 ni =

  4. Proof of Path Lengths • Prove: EPL(ni) = IPL(ni) + 2 ni by induction • Base: ni = 0 (single node, the root) • EPL(ni) = 0 • IPL(ni) = 0; 2 ni = 0 0 = 0 + 0 • Assume for N: True for all FBT with ni < N • Prove for N+1: Let niL, niR be # of int. nodes in L, R subtrees. • niL + niR = N - 1 ==> niL < N; niR < N • EPL(niL) = IPL(niL) + 2 niL • EPL (niR) = IPL(niR) + 2 niR • EPL(ni) = EPL(niL) + EPL(niR) + niL + 1 + niR + 1 • IPL(ni) = IPL(niL) + IPL(niR) + niL + niR • EPL(ni) = IPL(ni) + 2 ni

  5. Constructing Trees • Is it possible to reconstruct a BT from just one of its pre-order, inorder, or post-order sequences?

  6. Constructing Trees (cont) • Given two sequences (say pre-order and inorder) is the tree unique?

  7. Tree Implementations • What should methods of a tree class be?

  8. Tree class • template <class Object> • class Tree { • public: • Tree(const Object &notFnd); • Tree (const Tree &rhs); • ~Tree(); • const Object &find(const Object &x) const; • bool isEmpty() const; • void printTree() const; • void makeEmpty(); • void insert (const Object &x); • void remove (const Object &x); • const Tree &operator=(const Tree &rhs);

  9. Tree class (cont) • private: • TreeNode<Object> *root; • const Object ITEM_NOT_FOUND; • const Object &elementAt(TreeNode<Object> *t) const; • void insert (const Object &x, TreeNode<Object> * &t) const; • void remove (const Object &x, TreeNode<Object> * &t) const; • TreeNode<Object> *find(const Object &x, • TreeNode<Object> *t) const; • void makeEmpty(TreeNode<Object> *&t) const; • void printTree(TreeNode<Object *t) const; • TreeNode<Object> *clone(TreeNode<Object> *t)const; • };

  10. Tree Implementations • Fixed Binary • element • left pointer • right pointer • Fixed K-ary • element • array of K child pointers • Linked Sibling/Child • element • firstChild pointer • nextSibling pointer

  11. TreeNode : Static Binary • template <class Object> • class BinaryNode { • Object element; • BinaryNode *left; • BinaryNode *right; • BinaryNode(const Object &theElement, BinaryNode *lt, BinaryNode *rt) : element (theElement), left(lt), right(rt) {} • friend class Tree<Object>; • };

  12. TreeNode : Static K-ary • template <class Object> • class KaryNode { • Object element; • KaryNode *children[MAX_CHILDREN]; • KaryNode(const Object &theElement; • friend class Tree<Object>; • };

  13. TreeNode : Sibling/Child • template <class Object> • class KTreeNode { • Object element; • KTreeNode *nextSibling; • KTreeNode *firstChild; • KTreeNode(const Object &theElement, KTreeNode *ns, KTreeNode *fc) : element (theElement), nextSibling(ns), firstChild(fc) {} • friend class Tree<Object>; • };

More Related