CMSC 341 Introduction to Trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees, Binary Trees, and Binary Search Trees COMP171.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
MCS680: Foundations Of Computer Science
Week 6 - Wednesday CS221.
CMSC 341 Introduction to Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees 3: The Binary Search Tree
Trees.
General Trees & Binary Trees
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
CMSC 341 Binary Search Trees 11/19/2018.
CMSC 341 Introduction to Trees.
Trees CSE 373 Data Structures.
Trees.
CMSC 341 Binary Search Trees.
Trees Definitions Implementation Traversals K-ary Trees
Trees CMSC 202, Version 5/02.
CMSC 341 Binary Search Trees 1/3/2019.
CMSC 202 Trees.
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
CMSC 341 Binary Search Trees 2/23/2019.
Trees.
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CMSC 341 Binary Search Trees.
Tree.
CMSC 341 Binary Search Trees 5/8/2019.
Binary Trees.
CMSC 341 Binary Search Trees 5/28/2019.
Trees CSE 373 Data Structures.
General Tree Concepts Binary Trees
CMSC 341 Binary Search Trees 2/21/2006.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

CMSC 341 Introduction to Trees

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 T1, T2, … 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 The definition is recursive for list: a list is a first element followed by a list of elements Node and edge are not really defined Edges not usually shown as directed (maybe gravity is implicit) 11/27/2018

More Tree Terminology A path from node V1 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 V1 to V2, then V1 is an ancestor of V2 and V2 is a descendent of V1. Note: Path is not a sequence of edges, it is a sequence of nodes. Suppose the sequence contains just one node. What is the path length? (zero) A path is not any old sequence of nodes. There must be a parent-child relationship between adjacent nodes on the path. Since there is a path (of zero length) from any node to itself -- it is technically true that every node is both an ancestor and descendent of itself. We sometimes say “proper ancestor” or “proper descendent” to preclude this situation 11/27/2018

Tree Storage A tree node contains: Element Links to each child to sibling and first child 11/27/2018

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). A full BT with n interior nodes has n+1 exterior nodes (leaf nodes). 11/27/2018

FBT Theorem 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. 11/27/2018

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 11/27/2018

Perfect Binary Tree A perfect BT is a full BT in which all leaves have the same depth. The number of nodes in a PBT is always one less than a power of 2. In fact, there are 2 ^(n+1) -1 nodes in a PBT, where h is the height of the tree 11/27/2018

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

Proof of PBT Theorem(cont) Assume true for all h  H Prove for H+1: 11/27/2018

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. Note: Every PBT is complete, but not vice versa. 11/27/2018

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) + 2ni Example: ni = EPL(ni) = IPL(ni) = 2 ni = Example: ni = 4 EPL(ni) = 2+2+3+3+2 = 12 IPL(ni) = 0+1+1+2 = 4 2 ni = 8 11/27/2018

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 How come? E(ni) is EPL for the whole tree with respect to its root. E(niL) is theEPL for the left subtree with respect to its root. Therefore, must add 1 for each leaf in the left subtree But FBT of n internal nodes has n+1 leaf nodes. Thus EPL(ni) = EPL(niL) + niL + 1 + EPL(niR) + niR + 1 = EPL(niL) + EPL(niR) + niL + niR + 2 Example: 11/27/2018

Traversal Inorder Preorder Postorder Levelorder 11/27/2018

Constructing Trees Is it possible to reconstruct a BT from just one of its pre-order, inorder, or post-order sequences? Ans: no. Example: B,A inorder makes two trees Example: A B preorder Example: B, A postorder 11/27/2018

Constructing Trees (cont) Given two sequences (say pre-order and inorder) is the tree unique? Ans: yes Example: preorder: ABECD inorder: BEADC 1. 1st element of preorder must be root 2. Inorder: elements to left of A are on left, to right are on right 3. Preorder: 2nd element must be root of left child of A (since it has a left child) and inorder shows no left child and E on right 4. Preorder: C is root of right subtree of A and inorder DC shows no right child and D on left How about postorder and preorder?? No, encode the same information about the tree 11/27/2018

Tree Implementations What should methods of a tree class be? 11/27/2018

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); 11/27/2018

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; }; 11/27/2018

Tree Implementations Fixed Binary element left pointer right pointer Fixed K-ary array of K child pointers Linked Sibling/Child firstChild pointer nextSibling pointer 11/27/2018

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>; }; 11/27/2018

Find : Static Binary template <class Object> BinaryNode<Object> *Tree<Object> :: find(const Object &x, BinaryNode<Object> *t) const { BinaryNode<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)); } 11/27/2018

Insert : Static Binary 11/27/2018

Remove : Static Binary 11/27/2018

TreeNode : Static K-ary template <class Object> class KaryNode { Object element; KaryNode *children[MAX_CHILDREN]; KaryNode(const Object &theElement); friend class Tree<Object>; }; 11/27/2018

Find : Static K-ary template <class Object> KaryNode<Object> *KaryTree<Object> :: find(const Object &x, KaryNode<Object> *t) const { KaryNode<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; } 11/27/2018

Insert : Static K-ary 11/27/2018

Remove : Static K-ary 11/27/2018

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>; }; 11/27/2018

Find : Sibling/Child template <class Object> KTreeNode<Object> *Tree<Object> :: find(const Object &x, KTreeNode<Object> *t) const { KTreeNode<Object> *ptr; if (t == NULL) return NULL; else if (x == t->element) return t; else if (ptr = find(x, t->firstChild)) return ptr; else return(ptr = find(x, t->nextSibling)); } 11/27/2018

Insert : Sibling/Child 11/27/2018

Remove : Sibling/Parent 11/27/2018