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
Introduction to Trees Chapter 6 Objectives
Advertisements

Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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 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 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 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Binary Trees Chapter 6.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
COSC2007 Data Structures II
CS261 Data Structures Trees Introduction and Applications.
Saturday, 04 Apr 2010 University of Palestine Computer Science II 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,
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.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
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.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
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.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
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.
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.
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:
Trees A non-linear implementation for collection classes.
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Unit 7 Trees, Tree Traversals and Binary Search Trees
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.
MCS680: Foundations Of Computer Science
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees.
CMSC 341 Introduction to Trees.
Trees What is a Tree? Tree terminology Why trees?
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Tree.
Tree and its terminologies
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
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.) 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 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 The following height function of a Binary Tree in java and C++ int treeHeight(tree *p) { if (p == NULL) { return 0 ; } int left = treeHeight(p->left) ; int right = treeHeight(p->right) ; return 1 + std::max(left, right) ; } public int heightOfBinaryTree(Node node) { if (node == null) { return 0; } else { return 1 + Math.max(heightOfBinaryTree(node.left), heightOfBinaryTree(node.right)); }

10 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. Decision tree. –Efficient algorithmic solutions, e.g., Search trees. Efficient priority queues via heaps.

11 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

12 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:

13 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. Thus, every full binary tree is a complete binary tree, but the opposite is not true.

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

15 Binary Trees Implementation rightkeyleft huxvmnz h u x m z v i n v

16 Binary Trees Implementation (Contd.) WE HAVE SEEN how objects can be linked into lists. When an object contains two pointers to objects of the same type, structures can be created that are much more complicated than linked lists. In this section, we'll look at one of the most basic and useful structures of this type: binary trees. Each of the objects in a binary tree contains two pointers, typically called left and right. In addition to these pointers, of course, the nodes can contain other types of data. For example, a binary tree of integers could be made up of objects of the following type:

17 Binary Trees Implementation (Contd.) struct TreeNode { int item; // The data in this node. TreeNode *left; // Pointer to the left subtree. TreeNode *right; // Pointer to the right subtree. }

18 Binary Trees Implementation (Contd.) This gives the total number of nodes in the tree. Written out in C++: int countNodes( TreeNode *root ) { // Count the nodes in the binary tree to which // root points, and return the answer. if ( root == NULL ) return 0 ; // The tree is empty. It contains no nodes. Else { int count = 1 ; // Start by counting the root. count += countNodes(root->left); // Add the number of nodes in the left subtree. count += countNodes(root->right); // Add the number of nodes in the right subtree. return count; // Return the total. } } // end countNodes()

19 Binary Trees Implementation (Contd.) Here is a function that prints all the items on one line of output: void preorderPrint( TreeNode *root ) { // Print all the items in the tree to which root points. //The item in the root is printed first, followed by the items in the left subtree and then the items in the right subtree. if ( root != NULL ) { // (Otherwise, there's nothing to print.) cout item << " "; // Print the root item. preorderPrint( root->left ); // Print items in left subtree. preorderPrint( root->right ); // Print items in right subtree. } } // end preorderPrint()