B.Ramamurthy Chapter 9 CSE116A,B

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

Recursion B.Ramamurthy Chapter 4. Ramamurthy Introduction  Recursion is one of most powerful methods of solution available to computer scientists. 
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Trees Chapter 8.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Fundamentals of Python: From First Programs Through Data Structures
Marc Smith and Jim Ten Eyck
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. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
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.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
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.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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 Chapter 15.
CSCE 210 Data Structures and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Binary Search Tree (BST)
CS 302 Data Structures Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Lecture 18. Basics and types of Trees
Binary Tree and General Tree
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
TREES General trees Binary trees Binary search trees AVL trees
8.2 Tree Traversals Chapter 8 - Trees.
Find in a linked list? first last 7  4  3  8 NULL
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Trees Chapter 10.
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
CSC 143 Java Trees.
CSC 205 – Java Programming II
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

B.Ramamurthy Chapter 9 CSE116A,B Trees B.Ramamurthy Chapter 9 CSE116A,B 5/2/2019 B. Ramamurthy

Introduction Data organizations we studied are linear in that items are one after another. Data organization can also be hierarchical whereby an item can have more than one immediate successor. Thus an ADT can be classified as linear and non-linear. We will study non-linear ADTs tree and binary trees in this discussion. 5/2/2019 B. Ramamurthy

Topics for discussion Terminology Tree and Binary Tree -- Formal definition Tree Traversal Full binary tree Complete binary tree Balanced Tree Summary 5/2/2019 B. Ramamurthy

Tree : Formal Definition A tree T is a finite, non-empty set of nodes, T = {r} U T1 U T2 U T3… U Tn with the following properties: 1. A designated node of the set r, is called the root of the tree; 2. The remaining nodes are partitioned into n >= 0 subsets T1, T2, .. Tn each of which is a tree. 5/2/2019 B. Ramamurthy

Binary Tree : formal definition A binary tree is a set T of nodes such that either T is empty, or T is parameterized into three disjoint sets: A single node r, the root Two possibly empty sets that are binary trees called left and right subtrees of r. Example : Binary trees to represent algebraic expressions. 5/2/2019 B. Ramamurthy

Terminology Trees are used to represent relationships: items in a tree are referred to as nodes and the lines connecting the nodes that express the hierarchical relationship are referred to as edges. The edges in a tree are directed. Trees are hierarchical which means that a parent-child relationship exist between the nodes in the tree. Each node has at most one parent. The node with no parents is the root node. The nodes that have no successors (no children nodes) are known as leaf nodes. 5/2/2019 B. Ramamurthy

More Definitions The degree of a node is the number of subtrees associated with that node. A node of degree 0 is the leaf node. In a binary tree the degree of each node except the leaf nodes is 2. Level of a node of tree is measured from its root, whereas height of a node is measured from its root to the leaf. Height of a leaf node is 0. 5/2/2019 B. Ramamurthy

Basic Tree Anatomy root Level 0 Level 1 Internal node Level 2 leaf 5/2/2019 B. Ramamurthy

N-nary Tree An n-nary tree T is a finite set of nodes with the following properties: 1. Either the set id empty or 2. The set consists of a root, R, and exactly N distinct N-ary trees. That is, the remaining nodes are partitioned into N>= 0 sunbsets T0, T1, .. TN-1, each of which is an N-ary tree such that T = {R, T0, T1, .. TN-1}. 5/2/2019 B. Ramamurthy

Observation An important concept used in the above definition is recursion. A tree is defined in terms of smaller trees. This concept of defining a term by smaller of the kind is called recursive definition. We will discuss recursion next class. 5/2/2019 B. Ramamurthy

External and Internal Nodes Theorem 9.1 (in your text) : An N-ary tree with n >= 0 internal nodes contains (N-1)(n+1) external nodes. Theorem 9.2 : Consider an N-ary tree T of height h >= 0. The maximum number of internal nodes in T is given by (h+1) - 1 N N - 1 5/2/2019 B. Ramamurthy

Leaf nodes Theorem 9.3 : Consider an N-ary tree of height h >= 0. The maximum number of leaf nodes in T is N h 5/2/2019 B. Ramamurthy

Binary Tree A binary tree T is a finite set of nodes with the following properties: 1. Either the set is empty, T = O. or 2. The set consists of a root, r, and exactly two distinct binary trees T L and TR T = {r, TR, TL}. TL is called the left subtree and TR is called the right subtree. 5/2/2019 B. Ramamurthy

Binary tree : example A - B / C A \ B C How do you traverse this tree? 5/2/2019 B. Ramamurthy

Recursive Solutions Recursion is an important problem solving approach that is an alternative to iteration. These are questions to answer when using recursive solution: 1. How can you define the problem in terms of a smaller problem of the same type? 2. How does each recursive call diminish the size of the problem? 3. What instance of the problem can serve as the base case? 4. As the problem size diminishes, will you reach this base case? Ramamurthy

Example 1: Factorial of N Iterative definition: Factorial(N) = N * (N-1) * (N-2) *…1 for any N > 0. Factorial(0) = 1 Recursive solution: 1. Factorial(N) = N * Factorial(N-1) 2. Problem diminishing? yes. 3. Base case: Factorial(0) = 1; base case does not have a recursive call. 4. Can reach base case as problem diminishes? yes Ramamurthy

Writing String Backwards 1. Write a string of length N backwards in terms of writing a string of length (N-1) backwards. 2. Base case : Choice 1: Writing empty string. Choice 2: Writing a string of length 1. Ramamurthy

WriteBackward(S); if string is empty do nothing; // this is the base case else { write the last character of S; WriteBackward(S - minus its last character;} Ramamurthy

Defining Languages A grammar states the rules of a language. A recursive algorithm can be written based on this grammar that determines whether a given string is a member of the language ==> recognition algorithm. For expressing a grammar we use special symbols: X | Y means X or Y X.Y or simply X Y means X followed by Y <xyz> an instance of entity xyz 5/2/2019 B. Ramamurthy

A grammar for Java identifiers <identifier> = <letter> | <identifier> <letter> | <identifier><digit> <letter> = a | b …|z| A | B | C…|Z|_ <digit> = 0 | 1 | …|9 5/2/2019 B. Ramamurthy

Recognition algorithm for identifiers boolean IsId (w) { // Returns TRUE if w is a legal Java identifier, // Otherwise returns FALSE if (w is of length 1) if (w is a letter) return true; else return false; else if (last char of w is a letter or a digit) return IsId(w minus its last char); else return false; } 5/2/2019 B. Ramamurthy

Tree Traversals Preorder, inorder and post order. Pre, In and Post refer to the order in which root is traversed. PreOder traversal: Visit root, traverse TL, Traverse TR. InOrder traversal : Traverse TL, visit root, traverse TR. PostOrder traversal: Traverse TL, Traverse TR, and visit root. 5/2/2019 B. Ramamurthy

Expression Trees + / * A B -- E D C 5/2/2019 B. Ramamurthy

Implementing a Binary Tree Examine Java API Use the existing classes. Extend existing classes. Implement necessary interfaces. 5/2/2019 B. Ramamurthy

Binary Search Tree A binary search tree is a binary tree that is in a sense sorted according to the values in its nodes. For each node n, a binary search tree satisfies the following three properties: 1. n’s value is greater than all values in its left subtree TL. 2. n’s value is less than all its values in its right subtree TR. 3. Both TL and TR are binary search tree. 5/2/2019 B. Ramamurthy

Example : Tree of names Jane Bob Tom Ellen Alan Wendy Nancy Where will you insert the name Randy? How about Ian? 5/2/2019 B. Ramamurthy