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,

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.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search 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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
1 CMSC 341 Introduction to Trees Textbook sections:
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
General Trees & Binary Trees
CMSC 341 Introduction to Trees.
CMSC 341 Introduction to Trees.
Trees CSE 373 Data Structures.
Trees.
Trees CMSC 202, Version 5/02.
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
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Tree.
Binary Trees.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

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, then there is a distinguished node r, called root and zero or more non-empty subtrees T 1, T 2, … T k, each of whose roots are connected by a directed edge from r. This recursive definition leads to recursive tree algorithms and tree properties being proved by induction. Every node in a tree is the root of a subtree.

8/3/2007 UMBC CMSC 341 TreeIntro 3 A Generic Tree

8/3/2007 UMBC CMSC 341 TreeIntro 4 Tree 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

8/3/2007 UMBC CMSC 341 TreeIntro 5 More Tree Terminology A path from node V 1 to node V k is a sequence of nodes such that V i is the parent of V i+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.

8/3/2007 UMBC CMSC 341 TreeIntro 6 More Tree Terminology (cont.) 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.

8/3/2007 UMBC CMSC 341 TreeIntro 7 A Unix directory tree

8/3/2007 UMBC CMSC 341 TreeIntro 8 Tree Storage A tree node contains:  Data Element  Links to other nodes Any tree can be represented with the “first- child, next-sibling” implementation. class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; }

8/3/2007 UMBC CMSC 341 TreeIntro 9 Printing a Child/Sibling Tree // depth equals the number of tabs to indent name private void listAll( int depth ) { printName( depth ); // Print the name of the object if( isDirectory( ) ) for each file c in this directory (for each child) c.listAll( depth + 1 ); } public void listAll( ) { listAll( 0 ); } What is the output when listAll( ) is used for the Unix directory tree?

8/3/2007 UMBC CMSC 341 TreeIntro 10 K-ary Tree If we know the maximum number of children each node will have, K, we can use an array of children references in each node. class KTreeNode { Object element; KTreeNode children[ K ]; }

8/3/2007 UMBC CMSC 341 TreeIntro 11 Pseudocode for Printing a K-ary Tree // depth equals the number of tabs to indent name private void listAll( int depth ) { printElement( depth ); // Print the value of the object if( children != null ) for each child c in children array c.listAll( depth + 1 ); } public void listAll( ) { listAll( 0 ); }

8/3/2007 UMBC CMSC 341 TreeIntro 12 Binary Trees A special case of K-ary tree is a tree whose nodes have exactly two children pointers -- 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.

8/3/2007 UMBC CMSC 341 TreeIntro 13 The Binary Node Class private static class BinaryNode { // Constructors BinaryNode( AnyType theElement ) { this( theElement, null, null ); } BinaryNode( AnyType theElement, BinaryNode lt, BinaryNode rt ) { element = theElement; left = lt; right = rt; } AnyType element; // The data in the node BinaryNode left; // Left child BinaryNode right; // Right child }

8/3/2007 UMBC CMSC 341 TreeIntro 14 Full Binary Tree A full Binary Tree is a Binary Tree in which every node either has two children or is a leaf (every interior node has two children).

8/3/2007 UMBC CMSC 341 TreeIntro 15 FBT Theorem Theorem: A FBT with n internal nodes has n + 1 leaf nodes. Proof by strong induction on the number of internal nodes, n: Base case:  Binary Tree of one node (the root) has: zero internal nodes one external node (the root) Inductive Assumption:  Assume all FBTs with up to and including n internal nodes have n + 1 external nodes.

8/3/2007 UMBC CMSC 341 TreeIntro 16 FBT Proof (cont’d) Inductive Step - prove true for a tree with n + 1 internal nodes (i.e. a tree with n + 1 internal nodes has (n + 1) + 1 = n + 2 leaves)  Let T be a FBT of n internal nodes.  It therefore has n + 1 external nodes. (Inductive Assumption)  Enlarge T so it has n+1 internal nodes by adding two nodes to some leaf. These new nodes 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

8/3/2007 UMBC CMSC 341 TreeIntro 17 Perfect Binary Tree A Perfect Binary Tree is a full Binary Tree in which all leaves have the same depth.

8/3/2007 UMBC CMSC 341 TreeIntro 18 PBT Theorem Theorem: The number of nodes in a PBT is 2 h+1 -1, where h is height. Proof by strong induction on h, the height of the PBT:  Notice that the number of nodes at each level is 2 l. (Proof of this is a simple induction - left to student as exercise). Recall that the height of the root is 0.  Base Case: The tree has one node; then h = 0 and n = 1 and 2 (h + 1) = 2 (0 + 1) – 1 = 2 1 –1 = 2 – 1 = 1 = n.  Inductive Assumption: Assume true for all PBTs with height h  H.

8/3/2007 UMBC CMSC 341 TreeIntro 19 Proof of PBT Theorem(cont) Prove true for PBT with height H+1:  Consider a PBT with height H + 1. It consists of a rootand two subtrees of height H. Therefore, since the theorem is true for the subtrees (by the inductive assumption since they have height = H)  (2 (H+1) - 1)for the left subtree  (2 (H+1) - 1)for the right subtree  1for the root  Thus,n = 2 * (2 (H+1) – 1) + 1 = 2 ((H+1)+1) = 2 ((H+1)+1) - 1

8/3/2007 UMBC CMSC 341 TreeIntro 20 Complete Binary Trees Complete Binary Tree A complete Binary Tree is a perfect Binary Tree except that the lowest level may not be full. If not, it is filled from left to right.

8/3/2007 UMBC CMSC 341 TreeIntro 21 Tree Traversals Inorder Preorder Postorder Levelorder

8/3/2007 UMBC CMSC 341 TreeIntro 22 Constructing Trees Is it possible to reconstruct a Binary Tree from just one of its pre-order, inorder, or post- order sequences?

8/3/2007 UMBC CMSC 341 TreeIntro 23 Constructing Trees (cont) Given two sequences (say pre-order and inorder) is the tree unique?

8/3/2007 UMBC CMSC 341 TreeIntro 24 How do we find something in a Binary Tree? We must recursively search the entire tree. Return a reference to node containing x, return NULL if x is not found BinaryNode find( Object x) { BinaryNode t = null; // found it here if ( element.equals(x) ) return element; // not here, look in the left subtree if(left != null) t = left.find(x); // if not in the left subtree, look in the right subtree if ( t == null) t = right.find(x); // return pointer, NULL if not found return t; }

8/3/2007 UMBC CMSC 341 TreeIntro 25 Binary Trees and Recursion A Binary Tree can have many properties  Number of leaves  Number of interior nodes  Is it a full binary tree?  Is it a perfect binary tree?  Height of the tree Each of these properties can be determined using a recursive function.

8/3/2007 UMBC CMSC 341 TreeIntro 26 Recursive Binary Tree Function return-type function (BinaryNode t) { // base case – usually empty tree if (t == null ) return xxxx; // determine if the node pointed to by t has the property // traverse down the tree by recursively “asking” left/right children // if their subtree has the property return theResult; }

8/3/2007 UMBC CMSC 341 TreeIntro 27 Is this a full binary tree? boolean isFBT (BinaryNode t) { // base case – an empty tee is a FBT if (t == null) return true; // determine if this node is “full” // if just one child, return – the tree is not full if ((t.left && !t.right) || (t.right && !t.left)) return false; // if this node is full, “ask” its subtrees if they are full // if both are FBTs, then the entire tree is an FBT // if either of the subtrees is not FBT, then the tree is not return isFBT( t.right ) && isFBT( t.left ); }

8/3/2007 UMBC CMSC 341 TreeIntro 28 Other Recursive Binary Tree Functions Count number of interior nodes int countInteriorNodes( BinaryNode t); Determine the height of a binary tree. By convention (and for ease of coding) the height of an empty tree is -1 int height( BinaryNode t); Many others

8/3/2007 UMBC CMSC 341 TreeIntro 29 Other Binary Tree Operations How do we insert a new element into a binary tree? How do we remove an element from a binary tree?