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.

Slides:



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

Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees-part1. Objectives Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search.
Trees. 2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
COSC2007 Data Structures II
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
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.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 16: Trees Announcements 1.Programming project.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 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 What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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 From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Unit 7 Trees, Tree Traversals and Binary Search Trees
Data Structures and Design in Java © Rick Mercer
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Tree.
Binary Trees, Binary Search Trees
Presentation transcript:

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 trees

2 What is a Tree? A tree, is a finite set of nodes together with a finite set of directed edges that define parent-child relationships. Each directed edge connects a parent to its child. Example: Nodes={A,B,C,D,E,f,G,H} Edges={(A,B),(A,E),(B,F),(B,G),(B,H), (E,C),(E,D)} A directed path from node m1 to node m k is a list of nodes m 1, m 2,..., m k such that each is the parent of the next node in the list. The length of such a path is k - 1. Example: A, E, C is a directed path of length 2. A B E CD FHG

3 What is a Tree? (contd.) A tree satisfies the following properties: 1.It has one designated node, called the root, that has no parent. 2.Every node, except the root, has exactly one parent. 3.A node may have zero or more children. 4.There is a unique directed path from the root to each node treeNot a tree

4 Tree Terminology Ordered tree: A tree in which the children of each node are linearly ordered (usually from left to right). Ancestor of a node v: Any node, including v itself, on the path from the root to the node. Proper ancestor of a node v: Any node, excluding v, on the path from the root to the node. A C B ED F G Ancestors of G proper ancestors of E An Ordered Tree

5 Tree Terminology (Contd.) Descendant of a node v: Any node, including v itself, on any path from the node to a leaf node (i.e., a node with no children). Proper descendant of a node v: Any node, excluding v, on any path from the node to a leaf node. Subtree of a node v: A tree rooted at a child of v. Descendants of a node C A C B ED F G Proper descendants of node B A C B ED F G subtrees of node A

6 Tree Terminology (Contd.) AA B D H C E F G J I proper ancestors of node H proper descendants of node C subtrees of A AA B D H C E F G J I parent of node D child of node D grandfather of nodes I,J grandchildren of node C

7 Tree Terminology (Contd.) Each of node D and B has degree 1. Each of node A and E has degree 2. Leaf: A node with degree 0. Internal or interior node: a node with degree greater than 0. Siblings: Nodes that have the same parent. Size: The number of nodes in a tree. AA B D H C E F G J I node C has degree 3. Each of node F,G,H,I,J has degree 0. An Ordered Tree with size of 10 Siblings of E Siblings of A

8 Tree Terminology (Contd.) Level (or depth) of a node v: The length of the path from the root to v. Height of a node v: The length of the longest path from v to a leaf node. –Height of a node v: The length of the longest path from v to a leaf node. –The height of a tree is the height of its root mode. –By definition the height of an empty tree is -1. AA B D H C E FG J I k Level 0 Level 1 Level 2 Level 3 Level 4 The height of the tree is 4. The height of node C is 3.

9 Why Trees? Trees are very important data structures in computing. They are suitable for: –Hierarchical structure representation, e.g., File directory. Organizational structure of an institution. Class inheritance tree. –Problem representation, e.g., Expression tree. Game tree. Decision tree. –Efficient algorithmic solutions, e.g., Search trees. Efficient priority queues via heaps.

10 Implementing Trees The various classes of trees are viewed as classes of containers as shown below: MyComparable Container Tree AbstractObject AbstractContainer AbstractTree GeneralTree NaryTree BinaryTree

11 Implementing Trees (Contd.) The tree interface is defined as: 1 public interface Tree extends Container 2 { 3 Object getKey( ) ; 4 Tree getSubtree(int i) ; 5 boolean isEmpty( ) ; 6 boolean isLeaf( ) ; 7 int getDegree( ) ; 8 int getHeight( ) ; 9 void depthFirstTraversal(PrePostVisitor visitor) ; 10 void breadthFirstTraversal(Visitor visitor) ; 11 }

12 Implementing Trees (Contd.) The AbstractTree class skeleton is: 1 public abstract class AbstractTree extends AbstractContainer implements Tree 2 { 3 public void depthFirstTraversal(PrePostVisitor visitor){ 4 //... 5 } 6 public void breadthFirstTraversal(Visitor visitor){ 7 //... 8 } 9 public void accept(Visitor visitor){ 10 // } 12 public Enumeration getEnumeration( ){ 13 // } 15 // }

13 General Trees A general tree is an ordered tree whose set of nodes cannot be empty. In a general tree, there is no limit to the number of children that a node can have. Representing a general tree by linked lists: –Each node has a linked list of the subtrees of that node. 1 public class GeneralTree extends AbstractTree 2 { 3 protected Object key ; 4 protected int degree ; 5 protected LinkedList list ; 6 //... 7 }

14 General Trees (Contd.) An example of a general tree: D E F H G J KL M K

15 N-ary Trees An N-ary tree is an ordered tree that is either: 1.Empty, or 2.It consists of a root node and at most N non-empty N-ary subtrees. It follows that the degree of each node in an N-ary tree is at most N. Example of N-ary trees: ary (binary) tree B F J DD C GAEB 3-ary (tertiary)tree

16 N-ary Trees (Contd.) 1 public class NaryTree extends AbstractTree{ 2 protected Object key ; 3 protected int degree ; 4 protected NaryTree[ ] subtree ; 5 public NaryTree(int degree){ 6 key = null ; this.degree = degree ; 8 subtree = null ; 9 } 10 public NaryTree(int degree, Object key){ 11 this.key = key ; 12 this.degree = degree ; 13 subtree = new NaryTree[degree] ; 14 for(int i = 0; i < degree; i++) subtree[i] = new NaryTree(degree); 15 } 16 // }

17 Binary Trees A binary tree is an N-ary tree for which N = 2. Thus, a binary tree is either: 1.An empty tree, or 2.A tree consisting of a root node and at most two non-empty binary subtrees Example:

18 Binary Trees (Contd.) A full binary tree is either an empty binary tree or a binary tree in which each level k, k > 0, has 2 k nodes. A complete binary tree is either an empty binary tree or a binary tree in which: 1.Each level k, k > 0, other than the last level contains the maximum number of nodes for that level, that is 2 k. 2.The last level may or may not contain the maximum number of nodes. 3.If a slot with a missing node is encountered when scanning the last level in a left to right direction, then all remaining slots in the level must be empty. Thus, every full binary tree is a complete binary tree, but the opposite is not true.

19 Binary Trees (Contd.) Example showing the growth of a complete binary tree:

20 Binary Trees Implementation A binary tree can be represented using a linked list. 1 public class BinaryTree extends AbstractTree{ 2 protected Object key ; 3 protected BinaryTree left, 4 protected BinaryTree right ; 5 public BinaryTree(Object key, BinaryTree left, BinaryTree right){ 6 this.key = key ; 7 this.left = left ; this.right = right ; 8 } 9 public BinaryTree( ) 10 { this(null, null, null) ;} 11 12public BinaryTree(Object key){ 13 this(key, new BinaryTree( ), new BinaryTree( )); } 14 // } Example: A binary tree representing a + (b - c) * d rightkeyleft + a* d- bc

21 Binary Trees Implementation (Contd.) Some methods of BinaryTree class: 1 public boolean isEmpty( ) 2 { return key == null ; } 3 public boolean isLeaf( ) 4 { return ! isEmpty( ) && left.isEmpty( ) && right.isEmpty( ) ; } 5 public Object getKey( ){ 6 if(isEmpty( )) 7 throw new InvalidOperationException( ) ; 8 else 9 return key ;} public int getHeight( ){ 12 if(isEmpty( )) 13 return -1 ; 14 else 15 return 1 + Math.max(left.getHeight( ), right.getHeight( )) ; 16 } 17 public void attachKey(Object obj){ 18 if(! isEmpty( )) 19 throw new InvalidOperationException( ) ; 20 else{ 21 key = obj ; 22 left = new BinaryTree( ) ; 23 right = new BinaryTree( ) ; 24 } 25 }

22 Binary Trees Implementation (Contd.) 26 public Object detachKey( ) 27 { 28 if(! isLeaf( )) 29 throw new InvalidOperationException( ) ; 30 else 31 { 32 Object obj = key ; 33 key = null ; 34 left = null ; 35 right = null ; 36 return obj ; 37 } 38 } 39 public BinaryTree getLeft( ) 40 { 41 if(isEmpty( )) 42 throw new InvalidOperationException( ) ; 43 else 44 return left ; 45 } 46 public BinaryTree getRight( ) 47 { 48 if(isEmpty( )) 49 throw new InvalidOperationException( ) ; 50 else 51 return right ; 52 }

23 Application of Binary Trees Binary trees have many important uses. Two examples are: 1.Binary decision trees. Internal nodes are conditions. Leaf nodes denote decisions. Expression Tree Condition1 Condition2 Condition3 decision1 decision2 decision3 decision4 false True + a* d- bc