Introduction to Trees. When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only.

Slides:



Advertisements
Similar presentations
Trees Chapter 11.
Advertisements

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.1 MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson,
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
1 Chapter 10 Trees. Tree Definition 1. A tree is a connected undirected graph with no simple circuits. Theorem 1. An undirected graph is a tree if and.
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Rooted Trees. More definitions parent of d child of c sibling of d ancestor of d descendants of g leaf internal vertex subtree root.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
Binary Trees Chapter 6.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
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
Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications.
CS261 Data Structures Trees Introduction and Applications.
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.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Multiway Trees. Trees with possibly more than two branches at each node are know as Multiway trees. 1. Orchards, Trees, and Binary Trees 2. Lexicographic.
Foundations of Discrete Mathematics
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
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.
Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Module #19: Graph Theory: part II Rosen 5 th ed., chs. 8-9.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Trees – Chapter 9 Slides courtesy of Dr. Michael P. Frank University of Florida Dept. of Computer & Information Science & Engineering.
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.
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.
Data Structures TREES.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
CHAPTER 11 TREES INTRODUCTION TO TREES ► A tree is a connected undirected graph with no simple circuit. ► An undirected graph is a tree if and only.
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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
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.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Section10.1: Introduction to Trees
CSCE 210 Data Structures and Algorithms
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Introduction to Trees Section 11.1.
B+ Tree.
Lecture 18. Basics and types of Trees
Data Structures and Database Applications Binary Trees in C#
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Trees (Part 1, Theoretical)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Trees.
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Introduction to Trees

When at a node in a singly linked list, there is no choice as to which node can be visited next: Motivation when here we can only go here In a doubly linked list we have a choice of 2 nodes: if here then here or here If decisions or multiple possibilities are inherent in an application, then more than one or two nodes should be able to be reached from any given node. Data structures allowing this include trees and graphs: Trees can be regarded as falling between linear lists and graphs in the hierarchy of structural complexity. Tree Graph Trees

Tree Definitions There are many ways of defining a tree. We will look at 4 definitions, and as we do, we will define many other aspects of trees, which MUST be understood and remembered for successful mastery: 1) Definition in terms of Graphs:A tree is a connected, undirected graph that contains no circuit. In other words, there is a unique path between any 2 nodes. Not a tree, because there is a circuit: We use the adjective connected since one graph can be considered as consisting of two disconnected parts: considered as one graph The adjective undirected allows the definition to be more general: (restricted, since you can’t go against an arrow)(general, since you can go both ways between nodes) directed graph or digraph : undirected graph:

Trees Because computer applications of trees require that we start at one designated node in the tree in order to reach the other nodes, we drop the undirected requirement from the definition: Using directed edges allows us to designate the node where we should start as the root node. root Definition: The root node has indegree of 0. Indegree ≡ The number of directed edges pointing at a node. Outdegree ≡ The number of directed edges pointing away from a node. For Example: This root has: indegree = 0 and outdegree = 4 This internal node has: indegree = 1 and outdegree = 3 Leaf nodes have outdegree of 0.

Trees To impose some standardization so that we can visually pick out the root more easily, computer scientists require that trees be rooted: When we root a directed tree, it is no longer necessary to draw the arrows on the directed edges. They are understood to be there: bottom rootedtop rooted side rooted (European)(American)(compromise) (From now on, we will usually draw trees as top-rooted, with all edges understood to be directed downwards.)

Trees 2) Definition in terms of Number of Edges and Nodes: A tree is a finite connected graph such that: For Example: 3 nodes 2 edges 5 nodes 4 edges 8 nodes 7 edges no. of Nodes = no. of Edges + 1 3) Definition in terms of Connectivity: A graph is a tree iff the removal of any edge would disconnect the graph: Removing this, or any other edge, disconnects the graph. Definition: A collection of disconnected trees is called a forest: A forest of 5 trees. For Example:

Trees 4) Recursive Definition(Defining trees in terms of trees.) (This is the definition that computer scientists prefer.) A tree is a finite set of nodes which is either empty (root=NULL) or there is one node called the root node and the remaining nodes can be partitioned into disjoint sets each of which is also a tree. The Recursion This recursive definition for trees gives us some easily defined search algorithms, which we will study later. These 3 disjoint partitions are also trees – and with respect to the larger structure, they are called subtrees. 12 3

Trees Defining Parts of a Tree: Many scientific and medical terms seem obscure to us because these disciplines originated millennia ago within ancient languages. Many medical terms derive from ancient Greek. Even though the terminology came from common, everyday words, it is now foreign to us because of the language difference. More recent scientific disciplines kept Latin as their common language of discourse. Fortunately for us, computer science did not make the mistake of obscuring its definitions in ancient Latin or Greek. It uses current, common everyday English words like mouse, bugs, chips, etc. (More obscure terms mainly come from abbreviations and acronyms.) However, there is a drawback. For some concepts within our discipline, there may be more than one common word, and the one which will predominate from long historical use remains to be seen. stetho · scope Greek: lungs to watch ophthalmo · scope eye osteo · myel · itis bonemarrowinflamation BotanicalGeneric Familial Relations brothers or siblings ancestor – parent – father → descendent – child – son → Generic predecessor successor ← root start node ← branch node ---- internal node ← branch edge or arc ← leaf node ---- external node ↑ vertex is another name for node

Trees Kinds of Trees – in terms of branching factor (outdegree). Unary Trees – outdegree for every node ≤ 1. (a singly linked list) Binary Trees – outdegree for every node ≤ 2. Ternary Trees – outdegree for every node ≤ 3. Note: The indegree for every node in a directed tree, except the root, is m - ary Trees – outdegree for every node ≤ m. m

Trees Mathematical Properties of Trees LevelBinary TreeTernary Treem-ary Tree m 49 m2m2 827 m3m3 In general: h 2h2h 3h3h mhmh Total max no. of nodes: Max no. of Nodes per Level: These formulas may be proved by induction:

Trees Definition The path length of a node in a tree is the no. of edges from the root to that node. height (or depth) = 4 The height or depth of a tree is the max over all the path lengths of its nodes. path length = 2 path length = 3 path length = 4 path length = 1 path length = 0

Trees Definition An m-ary tree is regular if every one of its internal nodes has exactly m sons. TernaryBinary For each of the following kinds of trees, state which are regular and which are not regular: regular not regular  

Trees Definition Question: For a regular binary tree of height h, what is the maximum no. of leaves it can have? Answer: 2 h Question: What is the max no. of leaves for a regular m-ary tree of height h ? Answer: m h Such trees are called full trees. For example: Full Binary Trees non-Full Binary Trees

Trees Definition The min no. of leaves that a regular m-ary tree of height h can have is: 1 + h(m-1) An example, for binary trees: m = 2 h = 4 So for a binary tree of height 4, the min no. of leaves for such a tree is: 1 + 4(2-1) = = 5 An example, for ternary trees: m = 3 h = 5 So for a ternary tree of height 5, the min no. of leaves for such a tree is: 1 + 5(3-1) = = 11

Trees Definition Complete Binary Tree – Such a tree of height h is a full tree up to level h-1 and is filled in from left-to-right at level h. For example: Complete Binary Trees Not Complete......

Trees More Mathematical Properties Question: How is the height h related to the total number of nodes, n, in a: 1) full binary tree? 2) full m-ary tree? Recall that the no. of nodes for a full binary tree of height h is: 2 h+1 −1 So, to get h in terms of n just solve the following equation for h: 2 h+1 −1 = n 2 h+1 = n + 1 h + 1 = log2 (n + 1) h = log2 (n + 1) − 1 Similarly, since m h+1 −1 m −1 = n for full m-ary trees, the solution is easily determined to be: h = logm [n(m−1) + 1] − 1 Questions: Will these log functions always return integer values? Why, or why not?

Trees Mathematical Properties Question: How do we determine h in terms of n for complete binary trees? Hint: Derive the formula by looking at the trees below, and filling in and observing the number pattern in the table: For complete binary trees: h = ⎿ log2(n) ⏌ For complete m-ary trees: h = ⎿ logm(n) ⏌ The pair of delimiters ⎿ ⏌ is called the floor function, and is defined as: The ceiling function ⎾ ⏋ is the complement of the floor function, and is defined as: ⎿ x ⏌ ≡ the largest integer ≤ x ⎾ x ⏋ ≡ the smallest integer ≥ x height h nodes n Note that the height h increases by 1 at every power of 2, and is simply related as the logarithm of that power of 2: h = log2(n) But the log2 of the intervening integers results in decimal numbers, not integers. How can we denote the truncation of their decimal parts? – e.g. ⎿ ⏌ = 2 ⎿ ⏌ = 2 – e.g. ⎾ ⏋ = 3 ⎾ ⏋ = 3 1 node h = 0 8 nodes h = 3 3 nodes h = 1 2 nodes h = 1 4 nodes h = 2 5 nodes h =2 7 nodes h = 2 6 nodes h = ⋮⋮