1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation.

Slides:



Advertisements
Similar presentations
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.
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.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 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.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations. Insertion into an AVL tree Deletion from an AVL tree.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
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.
COSC2007 Data Structures II
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.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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,
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Binary Search Trees (BSTs) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
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.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
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 What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
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.
CSE 373 Data Structures Lecture 7
Unit 7 Trees, Tree Traversals and Binary Search Trees
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
Binary Search Tree Chapter 10.
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Trees CSE 373 Data Structures.
Trees.
Binary Search Trees (BST)
Binary Trees, Binary Search Trees
Trees.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Presentation transcript:

1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation and Applications Binary Search Trees –Definition –Importance –Implementation –Insertion –Deletion

2 Definition of 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 Definition of a Tree (Cont.) 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.) Degree: The number of subtrees of a node –Each of node D and B has degree 1. –Each of node A and E has degree 2. –Node C has degree 3. –Each of node F,G,H,I,J has degree 0. 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 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. –The height of a tree is the height of its root node. –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 Importance of 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. Decision tree. –Efficient algorithmic solutions, e.g., Search trees. Efficient priority queues via heaps.

10 Implementation of Trees 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. –Each element of the linked list is a subtree of the current node public class GeneralTree extends AbstractContainer { protected Object key ; protected int degree ; protected MyLinkedList list ; //... }

11 Implementation of Trees (Cont.) Definition: 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

12 Implementation of Trees (Cont.) public class NaryTree extends AbstractContainer { protected Object key ; protected int degree ; protected NaryTree[ ] subtree ; public NaryTree(int degree){ key = null ; this.degree = degree ; subtree = null ; } public NaryTree(int degree, Object key){ this.key = key ; this.degree = degree ; subtree = new NaryTree[degree] ; for(int i = 0; i < degree; i++) subtree[i] = new NaryTree(degree); } //... }

13 Binary Trees Definition: 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:

14 Binary Trees (Contd.) Definition: A full binary tree is either an empty binary tree or a binary tree in which every node is either a leaf node or an internal node with two children. Definition: 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.

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

16 Binary Trees (Contd.) What is the maximum height of a binary tree with n elements? n – 1 What is the minimum height of a binary tree with n elements?  log(n)  What is the minimum and maximum heights of a complete binary tree? Both are  log(n)  What is the minimum and maximum heights of a full binary tree?  log(n)  and  n/2 

17 Binary Trees Implementation rightkeyleft + a* d- bc public class BinaryTree extends AbstractContainer{ protected Object key ; protected BinaryTree left, right ; public BinaryTree(Object key, BinaryTree left, BinaryTree right){ this.key = key ; this.left = left ; this.right = right ; } public BinaryTree( ) { this(null, null, null) ; } public BinaryTree(Object key){ this(key, new BinaryTree( ), new BinaryTree( )); } //... } Example: A binary tree representing a + (b - c) * d

18 Binary Trees Implementation (Contd.) public boolean isEmpty( ){ return key == null ; } public boolean isLeaf( ){ return ! isEmpty( ) && left.isEmpty( ) && right.isEmpty( ) ; } public Object getKey( ){ if(isEmpty( )) throw new InvalidOperationException( ) ; else return key ; } public int getHeight( ){ if(isEmpty( )) return -1 ; else return 1 + Math.max(left.getHeight( ), right.getHeight( )) ; } public void attachKey(Object obj){ if(! isEmpty( )) throw new InvalidOperationException( ) ; else{ key = obj ; left = new BinaryTree( ) ; right = new BinaryTree( ) ; } What is the complexity of each of these operations?

19 Binary Trees Implementation (Contd.) public Object detachKey( ){ if(! isLeaf( )) throw new InvalidOperationException( ) ; else { Object obj = key ; key = null ; left = null ; right = null ; return obj ; } public BinaryTree getLeft( ){ if(isEmpty( )) throw new InvalidOperationException( ) ; else return left ; } public BinaryTree getRight( ){ if(isEmpty( )) throw new InvalidOperationException( ) ; else return right ; } What is the complexity of each of these operations?

20 Applications 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 Trees Condition1 Condition2 Condition3 decision1 decision2 decision3 decision4 false True + a* d- bc

21 Binary Search Trees Definition: A binary search tree (BST) is a binary tree that is empty or that satisfies the BST ordering property: 1.The key of each node is greater than each key in the left subtree, if any, of the node. 2.The key of each node is less than each key in the right subtree, if any, of the node. Thus, each key in a BST is unique. Examples: AA B C D

22 Importance of BSTs DeletionInsertionRetrievalData Structure O(log n) FAST O(log n) FAST O(log n) FAST BST O(n) SLOW O(n) SLOW O(log n) FAST* Sorted Array O(n) SLOW O(n) SLOW O(n) SLOW Sorted Linked List BSTs provide good logarithmic time performance in the best and average cases. Average case complexities of using linear data structures compared to BSTs: *using binary search

23 Implementation of BSTs The BinarySearchTree class inherits the instance variables key, left, and right of the BinaryTree class: public class BinarySearchTree extends BinaryTree implements SearchableContainer { private BinarySearchTree getLeftBST(){ return (BinarySearchTree) getLeft( ) ; } private BinarySearchTree getRightBST( ){ return (BinarySearchTree) getRight( ) ; } //... } What is the complexity of each of these operations?

24 Implementation of BSTs (Cont.) The find method of the BinarySearchTree class: public Comparable find(Comparable comparable) { if(isEmpty()) return null; Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) return key; else if (comparable.compareTo(key)<0) return getLeftBST().find(comparable); else return getRightBST().find(comparable); } What is the complexity of each of find?

25 Implementation of BSTs (Cont.) The findMin method of the BinarySearchTree class: By the BST ordering property, the minimum key is the key of the left-most node that has an empty left-subtree. public Comparable findMin() { if(isEmpty()) return null; if(getLeftBST().isEmpty()) return (Comparable)getKey(); else return getLeftBST().findMin(); } What is the complexity of each of findMin?

26 Implementation of BSTs (Cont.) The findMax method of the BinarySearchTree class: By the BST ordering property, the maximum key is the key of the right-most node that has an empty right-subtree public Comparable findMax() { if(isEmpty()) return null; if(getRightBST().isEmpty()) return (Comparable)getKey(); else return getRightBST().findMax(); } What is the complexity of each of findMax?

27 Insertion in BSTs By the BST ordering property, a new node is always inserted as a leaf node. The insert method, given in the next page, recursively finds an appropriate empty subtree to insert the new key. It then transforms this empty subtree into a leaf node by invoking the attachKey method: public void attachKey(Object obj) { if(!isEmpty()) throw new InvalidOperationException(); else { key = obj; left = new BinarySearchTree(); right = new BinarySearchTree(); }

28 Insertion in BSTs (Cont.) public void insert(Comparable comparable){ if(isEmpty()) attachKey(comparable); else { Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) throw new IllegalArgumentException("duplicate key"); else if (comparable.compareTo(key)<0) getLeftBST().insert(comparable); else getRightBST().insert(comparable); } What is the complexity of each of insert?

29 Deletion in BSTs There are three cases: 1.The node to be deleted is a leaf node. 2.The node to be deleted has one non-empty child. 3.The node to be deleted has two non-empty children. CASE 1: DELETING A LEAF NODE Convert the leaf node into an empty tree by using the detachKey method: // In Binary Tree class public Object detachKey( ){ if(! isLeaf( )) throw new InvalidOperationException( ) ; else { Object obj = key ; key = null ; left = null ; right = null ; return obj ; }

30 Deleting a leaf node (cont’d) Example: Delete 5 in the tree below: Delete

31 Deleting a one-child node CASE 2: THE NODE TO BE DELETED HAS ONE NON-EMPTY CHILD (a) The right subtree of the node x to be deleted is empty. Example: Delete target temp 6 target // Let target be a reference to the node x. BinarySearchTree temp = target.getLeftBST(); target.key = temp.key; target.left = temp.left; target.right = temp.right; temp = null;

32 Deleting a one-child node (cont’d) (b) The left subtree of the node x to be deleted is empty. Example: Delete target temp target 14 9 // Let target be a reference to the node x. BinarySearchTree temp = target.getRightBST(); target.key = temp.key; target.left = temp.left; target.right = temp.right; temp = null;

33 CASE 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#1 Copy the minimum key in the right subtree of x to the node x, then delete the one-child or leaf-node with this minimum key. Example: Delete

34 DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#2 Copy the maximum key in the left subtree of x to the node x, then delete the one-child or leaf-node with this maximum key. Example: Delete

35 Two-child deletion method#1 code // find the minimum key in the right subtree of the target node Comparable min = target.getRightBST().findMin(); // copy the minimum value to the target target.key = min; // delete the one-child or leaf node having the min target.getRightBST().withdraw(min); All the different cases for deleting a node are handled in the withdraw (Comparable key) method of BinarySearchTree class What is the complexity of each of the delete method?