CMSC 341 Introduction to Trees Tree ADT Tree

  • Slides: 32
Download presentation
CMSC 341 Introduction to Trees

CMSC 341 Introduction to Trees

Tree ADT Tree definition – A tree is a set of nodes. – The

Tree ADT Tree definition – A tree is a set of nodes. – The set may be empty – If not empty, then there is a distinguished node r, called root and zero or more non-empty subtrees T 1, T 2, … Tk, each of whose roots are connected by a directed edge from r. Basic Terminology – Root of a subtree is a child of r. R is the parent. – All children of a given node are called siblings. – A leaf (or external) node has no children. – An internal node is a node with one or more children 10/15/2021 2

More Tree Terminology A path from node V 1 to node Vk is a

More Tree Terminology A path from node V 1 to node Vk is a sequence of nodes such that Vi is the parent of Vi+1 for 1 i k. The length of this path is the number of edges encountered. The length of the path is one less than the number of nodes on the path ( k – 1 in this example) The depth of any node in a tree is the length of the path from root to the node. All nodes of the same depth are at the same level. The depth of a tree is the depth of its deepest leaf. The height of any node in a tree is the length of the longest path from the node to a leaf. The height of a tree is the height of its root. If there is a path from V 1 to V 2, then V 1 is an ancestor of V 2 and V 2 is a descendent of V 1. 10/15/2021 3

Tree Storage A tree node contains: – Element – Links • to each child

Tree Storage A tree node contains: – Element – Links • to each child • to sibling and first child 10/15/2021 4

Binary Trees A binary tree is a rooted tree in which no node can

Binary Trees A binary tree is a rooted tree in which no node can have more than two children AND the children are distinguished as left and right. A full BT is a BT in which every node either has two children or is a leaf (every interior node has two children). 10/15/2021 5

FBT Theorem: A FBT with n internal nodes has n+1 leaf nodes. Proof by

FBT Theorem: A FBT with n internal nodes has n+1 leaf nodes. Proof by induction: Base case: BT of one node (the root) has: zero internal nodes one external node (the root) Inductive Assumption (assume for n): – All FBT of up to and including n internal nodes have n+1 external nodes. 10/15/2021 6

Proof (cont) Inductive Step (prove for n+1): – Let T be a FBT of

Proof (cont) Inductive Step (prove for n+1): – Let T be a FBT of n internal nodes. – It therefore has n+1 external nodes. – Enlarge T by adding two nodes to some leaf. These are therefore leaf nodes. – Number of leaf nodes increases by 2, but the former leaf becomes internal. – So, • # internal nodes becomes n+1, • # leaves becomes (n+1)+1 = n+2 10/15/2021 7

Perfect Binary Tree A perfect BT is a full BT in which all leaves

Perfect Binary Tree A perfect BT is a full BT in which all leaves have the same depth. 10/15/2021 8

PBT Theorem: The number of nodes in a PBT is 2 h+1 -1, where

PBT Theorem: The number of nodes in a PBT is 2 h+1 -1, where h is height. Proof: Notice that the number of nodes at each level is 2 l. So the total number of nodes is: Prove this by induction: Base case: 10/15/2021 9

Proof of PBT Theorem(cont) Assume true for all h H Prove for H+1: 10/15/2021

Proof of PBT Theorem(cont) Assume true for all h H Prove for H+1: 10/15/2021 10

Other Binary Trees Complete Binary Tree A complete BT is a perfect BT except

Other Binary Trees Complete Binary Tree A complete BT is a perfect BT except that the lowest level may not be full. If not, it is filled from left to right. Augmented Binary Tree An augmented binary tree is a BT in which every unoccupied child position is filled by an additional “augmenting” node. 10/15/2021 11

Path Lengths The internal path length of a rooted tree is the sum of

Path Lengths The internal path length of a rooted tree is the sum of the depths of all of its nodes. The external path length of a rooted tree is the sum of the depths of all the null pointers. There is a relationship between the IPL and EPL of Full Binary Trees. If ni is the number of internal nodes in a FBT, then EPL(ni) = IPL(ni) + 2 ni Example: ni = EPL(ni) = IPL(ni) = 2 ni = 10/15/2021 12

Proof of Path Lengths Prove: EPL(ni) = IPL(ni) + 2 ni by induction Base:

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 ni. L, ni. R be # of int. nodes in L, R subtrees. ni. L + ni. R = N - 1 ==> ni. L < N; ni. R < N EPL(ni. L) = IPL(ni. L) + 2 ni. L EPL (ni. R) = IPL(ni. R) + 2 ni. R EPL(ni) = EPL(ni. L) + EPL(ni. R) + ni. L + 1 + ni. R + 1 IPL(ni) = IPL(ni. L) + IPL(ni. R) + ni. L + ni. R EPL(ni) = IPL(ni) + 2 ni 10/15/2021 13

Traversal Inorder Preorder Postorder Levelorder 10/15/2021 14

Traversal Inorder Preorder Postorder Levelorder 10/15/2021 14

Constructing Trees Is it possible to reconstruct a BT from just one of its

Constructing Trees Is it possible to reconstruct a BT from just one of its preorder, inorder, or post-order sequences? 10/15/2021 15

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

Constructing Trees (cont) Given two sequences (say pre-order and inorder) is the tree unique? 10/15/2021 16

Tree Implementations What should methods of a tree class be? 10/15/2021 17

Tree Implementations What should methods of a tree class be? 10/15/2021 17

Tree class template <class Object> class Tree { public: Tree(const Object &not. Fnd); Tree

Tree class template <class Object> class Tree { public: Tree(const Object &not. Fnd); Tree (const Tree &rhs); ~Tree(); const Object &find(const Object &x) const; bool is. Empty() const; void print. Tree() const; void make. Empty(); void insert (const Object &x); void remove (const Object &x); const Tree &operator=(const Tree &rhs); 10/15/2021 18

Tree class (cont) private: Tree. Node<Object> *root; const Object ITEM_NOT_FOUND; const Object &element. At(Tree.

Tree class (cont) private: Tree. Node<Object> *root; const Object ITEM_NOT_FOUND; const Object &element. At(Tree. Node<Object> *t) const; void insert (const Object &x, Tree. Node<Object> * &t) const; void remove (const Object &x, Tree. Node<Object> * &t) const; Tree. Node<Object> *find(const Object &x, Tree. Node<Object> *t) const; void make. Empty(Tree. Node<Object> *&t) const; void print. Tree(Tree. Node<Object *t) const; Tree. Node<Object> *clone(Tree. Node<Object> *t)const; }; 10/15/2021 19

Tree Implementations Fixed Binary – element – left pointer – right pointer Fixed K-ary

Tree Implementations Fixed Binary – element – left pointer – right pointer Fixed K-ary – element – array of K child pointers Linked Sibling/Child – element – first. Child pointer – next. Sibling pointer 10/15/2021 20

Tree. Node : Static Binary template <class Object> class Binary. Node { Object element;

Tree. Node : Static Binary template <class Object> class Binary. Node { Object element; Binary. Node *left; Binary. Node *right; Binary. Node(const Object &the. Element, Binary. Node *lt, Binary. Node *rt) : element (the. Element), left(lt), right(rt) {} friend class Tree<Object>; }; 10/15/2021 21

Find : Static Binary template <class Object> Binary. Node<Object> *Tree<Object> : : find(const Object

Find : Static Binary template <class Object> Binary. Node<Object> *Tree<Object> : : find(const Object &x, Binary. Node<Object> *t) const { Binary. Node<Object> *ptr; if (t == NULL) return NULL; else if (x == t->element) return t; else if (ptr = find(x, t->left)) return ptr; else return(ptr = find(x, t->right)); } 10/15/2021 22

Insert : Static Binary 10/15/2021 23

Insert : Static Binary 10/15/2021 23

Remove : Static Binary 10/15/2021 24

Remove : Static Binary 10/15/2021 24

Tree. Node : Static K-ary template <class Object> class Kary. Node { Object element;

Tree. Node : Static K-ary template <class Object> class Kary. Node { Object element; Kary. Node *children[MAX_CHILDREN]; Kary. Node(const Object &the. Element); friend class Tree<Object>; }; 10/15/2021 25

Find : Static K-ary template <class Object> Kary. Node<Object> *Kary. Tree<Object> : : find(const

Find : Static K-ary template <class Object> Kary. Node<Object> *Kary. Tree<Object> : : find(const Object &x, Kary. Node<Object> *t) const { Kary. Node<Object> *ptr; if (t == NULL) return NULL; else if (x == t->element) return t; else { i =0; while ((i < MAX_CHILDREN) && !(ptr = find(x, t->children[i])) i++; return ptr; } } 10/15/2021 26

Insert : Static K-ary 10/15/2021 27

Insert : Static K-ary 10/15/2021 27

Remove : Static K-ary 10/15/2021 28

Remove : Static K-ary 10/15/2021 28

Tree. Node : Sibling/Child template <class Object> class KTree. Node { Object element; KTree.

Tree. Node : Sibling/Child template <class Object> class KTree. Node { Object element; KTree. Node *next. Sibling; KTree. Node *first. Child; KTree. Node(const Object &the. Element, KTree. Node *ns, KTree. Node *fc) : element (the. Element), next. Sibling(ns), first. Child(fc) {} friend class Tree<Object>; }; 10/15/2021 29

Find : Sibling/Child template <class Object> KTree. Node<Object> *Tree<Object> : : find(const Object &x,

Find : Sibling/Child template <class Object> KTree. Node<Object> *Tree<Object> : : find(const Object &x, KTree. Node<Object> *t) const { KTree. Node<Object> *ptr; if (t == NULL) return NULL; else if (x == t->element) return t; else if (ptr = find(x, t->first. Child)) return ptr; else return(ptr = find(x, t->next. Sibling)); } 10/15/2021 30

Insert : Sibling/Child 10/15/2021 31

Insert : Sibling/Child 10/15/2021 31

Remove : Sibling/Parent 10/15/2021 32

Remove : Sibling/Parent 10/15/2021 32