Data Structures and Algorithms1 Trees The definitions for this presentation are from from: Corman, et. al., Introduction to Algorithms (MIT Press), Chapter.

Slides:



Advertisements
Similar presentations
Trees Chapter 11.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 5: Trees Data Structures.
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.
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
Lecture 21 Trees. “ A useful data structure for many applications”
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
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.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
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.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
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.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Compiled by: Dr. Mohammad Omar Alhawarat
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Discrete Structures Trees (Ch. 11)
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
Discrete Mathematics Chapter 5 Trees.
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.
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.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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 General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and 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 : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Discrete Mathematics Chapter 10 Trees.
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.
1 CMSC 341 Introduction to Trees Textbook sections:
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.
Section10.1: Introduction to Trees
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.
Trees Saurav Karmakar
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Data Structures and Algorithms
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
12. Graphs and Trees 2 Summary
Introduction to Trees Section 11.1.
CMSC 341 Introduction to Trees.
CS223 Advanced Data Structures and Algorithms
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Trees.
Data Structures and Algorithms for Information Processing
Trees Definitions Implementation Traversals K-ary Trees
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
Binary Trees.
General Tree Concepts Binary Trees
Presentation transcript:

Data Structures and Algorithms1 Trees The definitions for this presentation are from from: Corman, et. al., Introduction to Algorithms (MIT Press), Chapter 5. Some material on binomial trees is from Hull.

Data Structures and Algorithms2 A Few Applications Arithmetic Expressions b + a * b + b * a b

Data Structures and Algorithms3 A Few Applications title #text XML Document Object Model

From Hull4 A Few Applications Binomial “Trees” Movements in Time  t Su Sd S p 1 – p Binomial trees are frequently used to approximate the movements in the price of a stock or other asset In each small interval of time the stock price is assumed to move up by a proportional amount u or to move down by a proportional amount d.

Data Structures and Algorithms5 Definitions A Free tree is a connected, acyclic undirected graph.

Data Structures and Algorithms6 If an undirected graph is acyclic but possibly disconnected, it is a forest.

Data Structures and Algorithms7 This is a graph that contains a cycle and is therefore neither a tree nor a forest.

Data Structures and Algorithms8 Theorem (Properties of Free Trees) Let G = (V, E) be an undirected graph. The following statements are equivalent: 1. G is a free tree.

Data Structures and Algorithms9 2. Any two vertices in G are connected by a unique simple path.

Data Structures and Algorithms10 3. G is connected, but if any edge is removed from E, the resulting graph is disconnected.

Data Structures and Algorithms11 4. G is connected, and |E| = |V| - 1.

Data Structures and Algorithms12 5. G is acyclic, and |E| = |V| - 1.

Data Structures and Algorithms13 6. G is acyclic, but if any edge is added to E, the resulting graph contains a cycle.

Data Structures and Algorithms14 A rooted tree is a free tree in which one of the vertices is distinguished from the others. The distinguished vertex is called the root of the tree. We often refer to a vertex of a rooted tree as a node (we may also call this a vertex) of the tree. The following figure shows a rooted tree on a set of 12 nodes with root

Data Structures and Algorithms15 Consider a node x in a rooted tree T with root r. Any node y on the unique path from r to x is called an ancestor of x. If y is an ancestor of x, then x is a descendant of y. (Every node is both an ancestor and a descendant of itself.) If y is an ancestor of x and x  y, then y is a proper ancestor of x and x is a proper descendant of y. The subtree rooted at x is the tree induced by descendants of x, rooted at x r x y

Data Structures and Algorithms16 If the last edge on the path from the root r of a tree T to a node x is (y, x), then y is the parent of x, and x is a child of y. The root is the only node in T with no parent. If two nodes have the same parent, they are siblings. A node with no children is an external node or leaf. A nonleaf node is an internal node.

Data Structures and Algorithms17 The number of children of a node x in a rooted tree T is called the degree of x. The length of the path from the root r to a node x is the depth of x in T. The largest depth of any node in T is the height of T depth 0 depth 1 depth 2 depth 3 depth 4 height = 4

Data Structures and Algorithms18 An ordered tree is a rooted tree in which the children of each node are ordered. That is, if a node has k children, then there is a first child, a second child, …, and a kth child. The two trees shown below are different when considered to be ordered trees, but the same when considered to be just rooted trees

Data Structures and Algorithms19 A binary tree T is a structure defined on a finite set of nodes that either contains no nodes, or is comprised of three disjoint sets of nodes: a root node, a binary tree called its left subtree and a binary tree called its right subtree

Data Structures and Algorithms20 Full binary tree: each node is either a leaf or has degree exactly 2. In a positional tree, the children of a node are labeled with distinct positive integers. The ith child of a node is absent if no child is labeled with integer i. A k-ary tree is a positional tree in which for every node, all children with labels greater than k are missing. Thus, a binary tree is a k-ary tree with k = 2.

Data Structures and Algorithms21 A complete k-ary tree is a k-ary tree in which all leaves have the same depth and all internal nodes have degree k. height = 3 depth 0 depth 1 depth 2 depth 3

Data Structures and Algorithms22 How many leaves L does a complete binary tree of height h have? d = 0 d = 1 d = 2 The number of leaves at depth d = 2 d If the height of the tree is h it has 2 h leaves. L = 2 h.

Data Structures and Algorithms23 What is the height h of a complete binary tree with L leaves? leaves = 1 height = 0 leaves = 2 height = 1 leaves = 4 height = 2 leaves = L height = Log 2 L Since L = 2 h Log 2 L = Log 2 2 h h = Log 2 L

Data Structures and Algorithms24 The number of internal nodes of a complete binary tree of height h is ? Internal nodes = 0 height = 0 Internal nodes = 1 height = 1 Internal nodes = height = 2 Internal nodes = height = h-1 =  2 i = 2 h Thus, a complete binary tree of height = h has 2 h -1 internal nodes. Geometric series

Data Structures and Algorithms25 The number of nodes n of a complete binary tree of height h is ? nodes = 1 height = 0 nodes = 3 height = 1 nodes = 7 height = 2 nodes = 2 h height = h Since L = 2 h and since the number of internal nodes = 2 h -1 the total number of nodes n = 2 h + 2 h -1 = 2(2 h ) – 1 = 2 h

Data Structures and Algorithms26 If the number of nodes is n then what is the height? nodes = 1 height = 0 nodes = 3 height = 1 nodes = 7 height = 2 nodes = n height = Log 2 (n+1) - 1 Since n = 2 h+1 -1 n + 1 = 2 h+1 Log 2 (n+1) = Log 2 2 h+1 Log 2 (n+1) = h+1 h = Log 2 (n+1) - 1

Data Structures and Algorithms27 = 1 (2n)! (n+1) n! (2n-n)! Catalan Numbers 1,1,2,5,14,...

Data Structures and Algorithms28 The number of distinct binary trees with n nodes N = 1N=2... N=3 N = 0

Data Structures and Algorithms29 Class for Binary Nodes public class BTNode { private Object data; private BTNode left; private BTNode right;...

Data Structures and Algorithms30 public BTNode(Object obj, BTNode l, BTNode r) { data = obj; left = l; right= r; } public boolean isLeaf() { return (left == null) && (right == null); }...

Data Structures and Algorithms31 Copying Trees public static BTNode treeCopy(BTNode t) { if (t == null) return null; else { BTNode leftCopy = treeCopy(t.left); BTNode rightCopy = treeCopy(t.right); return new BTNode(t.data, leftCopy, rightCopy); }

Data Structures and Algorithms32 Tree Traversals Preorder Inorder Postorder Levelorder

Data Structures and Algorithms33 public void preOrderPrint(){ System.out.println(data); if (left != null) left.preOrderPrint(); if (right != null) right.preOrderPrint(); } a b c d e f g a b e cdf g Root, Left, Right

Data Structures and Algorithms34 public void inOrderPrint(){ if (left != null) { left.inOrderPrint() System.out.println(data); if (right != null) right.inOrderPrint() } c b d a f e g a b e c d f g Left, Root, Right

Data Structures and Algorithms35 public void postOrder(){ if (left != null left.postOrder() if (right != null) right.postOrder() System.out.println(data); } c d b f g e a a b e cd fg Left, right, root

Data Structures and Algorithms36 levelorder (T) { Q = makeEmptyQueue() enqueue (T,Q) until isempty (Q) { p = dequeue(Q) visit (p) for each child  of P, in order, do enqueue ( , Q) } a b e c d f g a b e c d f g

Data Structures and Algorithms37 An Array Representation Suppose we are lucky enough to be working with complete binary trees. We can store the tree in an array. Let the root be at index 0 and let the left and right children of node i be at indexes 2i+1 and 2i+2 respectively. Lewis and Denemberg, Page 111

Data Structures and Algorithms38 An Array Representation isLeaf(i) : 2i + 1 >= n leftChild(i) : 2i + 1 (none if 2i+ 1 >= n) rightChild(i): 2i + 2 (none if 2i + 2 >= n) leftSibling(i): i - 1 (none if i == 0 or i is odd) rightSibling(i) : i + 1 (none if i = n-1 or i is even) parent(i) = int((i-1)/2) (none if i == 0) Lewis and Denemberg, Page 111 Works if the tree is “almost complete”, growing top to bottom and left to right.

Data Structures and Algorithms39 Binomial “Trees” Using an Array Representation S0S0 S0uS0u S0dS0d S0S0 S0S0 S0u 2S0u 2 S0d 2S0d 2 S0u 2S0u 2 S0u 3S0u 3 S0u 4S0u 4 S 0 d 2 S0uS0u S0dS0d S0d 4S0d 4 S0d 3S0d 3 The array element bt[0] will be S 0. In general, given a node with index i at depth d, its left child is located at bt[i + d+1] and its right child is located at bt[i + d+2]