CMSC 341 Introduction to Trees CMSC 341 Tree Intro.

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.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
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 ),
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,
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees, Binary Trees, and Binary Search Trees COMP171.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
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; } 
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.
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.
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 CMSC 341 Introduction to Trees Textbook sections:
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
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
MCS680: Foundations Of Computer Science
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Data Structures & Algorithm Design
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
Introduction to Trees IT12112 Lecture 05.
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.
General Trees & Binary 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.
CS 261 – Data Structures Trees.
Tree.
Binary Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

CMSC 341 Introduction to Trees CMSC 341 Tree Intro

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 T1, T2, … Tk, 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. 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) CMSC 341 Tree Intro

A Generic Tree CMSC 341 Tree Intro

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 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) CMSC 341 Tree Intro

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. 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 CMSC 341 Tree Intro

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 V1 to V2, then V1 is an ancestor of V2 and V2 is a descendent of V1. CMSC 341 Tree Intro

A Unix directory tree CMSC 341 Tree Intro

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 { AnyType element; TreeNode firstChild; TreeNode nextSibling; } CMSC 341 Tree Intro

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 (i.e. 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? CMSC 341 Tree Intro

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 { AnyType element; KTreeNode children[ K ]; } CMSC 341 Tree Intro

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 object if( children != null ) for each child c in children array c.listAll( depth + 1 ); } public void listAll( ) listAll( 0 ); CMSC 341 Tree Intro

Binary Trees A special case of K-ary tree is a tree whose nodes have exactly two child references -- 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 with n interior nodes has n+1 exterior nodes (leaf nodes). CMSC 341 Tree Intro

The Binary Node Class CMSC 341 Tree Intro private class BinaryNode<AnyType> { // Constructors BinaryNode( AnyType theElement ) this( theElement, null, null ); } BinaryNode( AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt ) element = theElement; left = lt; right = rt; AnyType element; // The data in the node BinaryNode<AnyType> left; // Left child reference BinaryNode<AnyType> right; // Right child reference CMSC 341 Tree Intro

Full Binary Tree A full binary tree is a binary tree in which every node is a leaf or has exactly two children. CMSC 341 Tree Intro

FBT Theorem Theorem: A FBT with n internal nodes has n + 1 leaves (external 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 n internal nodes or less have n + 1 external nodes. CMSC 341 Tree Intro

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. Therefore T has n + 1 leaf 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) + 2 - 1 = n + 2 CMSC 341 Tree Intro

Perfect Binary Tree A Perfect Binary Tree is a Full Binary Tree 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 ^(h+1) -1 nodes in a PBT, where h is the height of the tree CMSC 341 Tree Intro

PBT Theorem Theorem: The number of nodes in a PBT is 2h+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 2l. (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) - 1 = 2(0 + 1) – 1 = 21 –1 = 2 – 1 = 1 = n. Inductive Assumption: Assume true for all PBTs with height h  H. CMSC 341 Tree Intro

Proof of PBT Theorem(cont) Prove true for PBT with height H+1: Consider a PBT with height H + 1. It consists of a root and two subtrees of height <= H. Since the theorem is true for the subtrees (by the inductive assumption since they have height ≤ H) the PBT with height H+1 has (2(H+1) - 1) nodes for the left subtree + (2(H+1) - 1) nodesfor the right subtree + 1 node for the root Thus, n = 2 * (2(H+1) – 1) + 1 = 2((H+1)+1) - 2 + 1 = 2((H+1)+1) - 1 CMSC 341 Tree Intro

Complete Binary Tree A Complete Binary Tree is a binary tree in which every level is completed filled, except possibly the bottom level which is filled from left to right. CMSC 341 Tree Intro

Tree Traversals Inorder Preorder Postorder Levelorder CMSC 341 Tree Intro

Constructing Trees Is it possible to reconstruct a Binary Tree 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 CMSC 341 Tree Intro

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 CMSC 341 Tree Intro

Finding an element in a Binary Tree?  Return a reference to node containing x, return null if x is not found public BinaryNode<AnyType> find(AnyType x) { return find(root, x); } private BinaryNode<AnyType> find( BinaryNode<AnyType> node, AnyType x) BinaryNode<AnyType> t = null; // in case we don’t find it if ( node.element.equals(x) ) // found it here?? return node; // not here, look in the left subtree if(node.left != null) t = find(node.left,x); // if not in the left subtree, look in the right subtree if ( t == null) t = find(node.right,x); // return reference, null if not found return t; 12/19/2006 – added “if t->data == x” CMSC 341 Tree Intro

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. CMSC 341 Tree Intro

Recursive Binary Tree Function return-type function (BinaryNode<AnyType> t) { // base case – usually empty tree if (t == null) return xxxx; // determine if the node referred to by t has the property // traverse down the tree by recursively “asking” left/right // children if their subtree has the property return theResult; } CMSC 341 Tree Intro

Is this a full binary tree? boolean isFBT (BinaryNode<AnyType> 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 == null && t.right != null) || (t.right == null && t.left != null)) 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 ); } CMSC 341 Tree Intro

Other Recursive Binary Tree Functions Count number of interior nodes int countInteriorNodes( BinaryNode<AnyType> 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<AnyType> t); Many others CMSC 341 Tree Intro

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? CMSC 341 Tree Intro