1 Trees 2 Binary trees Section 4.2. 2 Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
Transforming Infix to Postfix
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
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.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
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.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Grammars and Parsing  Recursive Definitions of Properties.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
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
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
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 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Trees Another Abstract Data Type (ADT)
Tree.
Section 8.1 Trees.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Trees Another Abstract Data Type (ADT)
Paul Tymann, Andrew Watkins,
Trees Another Abstract Data Type (ADT)
Section 9.3 by Andrew Watkins
Paul Tymann, Andrew Watkins,
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
General Tree Concepts Binary Trees
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

1 Trees 2 Binary trees Section 4.2

2 Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and right child nodes root 7

3 Complete Binary Trees Definition: A binary tree is complete iff every layer except possibly the bottom, is fully populated with vertices. In addition, all nodes at the bottom level must occupy the leftmost spots consecutively root

4 Complete Binary Trees A complete binary tree with n vertices and height H satisfies: –2 H < n < 2 H + 1 –2 2 < 7 < , 2 2 < 4 < root n = 7 H = root n = 4 H = 2

5 Complete Binary Trees A complete binary tree with n vertices and height H satisfies: –2 H < n < 2 H + 1 –H < log n < H + 1 –H = floor(log n)

6 Complete Binary Trees Theorem: In a complete binary tree with n vertices and height H –2 H < n < 2 H + 1

7 Complete Binary Trees Proof: –At level k <= H-1, there are 2 k vertices –At level k = H, there are at least 1 node, and at most 2 H vertices –Total number of vertices when all levels are fully populated (maximum level k): n = …2 k n = …2 k (Geometric Progression) n = 1(2 k + 1 – 1) / (2-1) n = 2 k

8 Complete Binary Trees n = 2 k + 1 – 1 when all levels are fully populated (maximum level k) Case 1: tree has maximum number of nodes when all levels are fully populated –Let k = H n = 2 H + 1 – 1 n < 2 H + 1 Case 2: tree has minimum number of nodes when there is only one node in the bottom level –Let k = H – 1 (considering the levels excluding the bottom) n’ = 2 H – 1 n > n’ + 1 = 2 H Combining the above two conditions we have –2 H < n < 2 H + 1

9 Vector Representation of Complete Binary Tree Tree data –Vector elements carry data Tree structure –Vector indices carry tree structure –Index order = levelorder –Tree structure is implicit –Uses integer arithmetic for tree navigation

10 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0 lr lllrrrrl root

11 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2]

12 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0l

13 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0lr

14 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0lrll

15 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0lrlllr

16 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0lrlllrrl

17 Vector Representation of Complete Binary Tree Tree navigation –Parent of v[k] = v[(k – 1)/2] –Left child of v[k] = v[2*k + 1] –Right child of v[k] = v[2*k + 2] 0lrlllrrlrr

18 Binary Tree Traversals Inorder traversal –Definition: left child, vertex, right child (recursive) –Algorithm: depth-first search (visit between children)

19 Inorder Traversal root

20 Inorder Traversal root

21 Inorder Traversal root

22 Inorder Traversal root

23 Binary Tree Traversals Other traversals apply to binary case: –Preorder traversal vertex, left subtree, right subtree –Inorder traversal left subtree, vertex, right subtree –Postorder traversal left subtree, right subtree, vertex –Levelorder traversal vertex, left children, right children

24 Example: Expression Tree How do you construct an expression tree from a postfix expression? E.g a b + c d e + * *

25 Build Expression Tree from Postfix Expression Let S be a stack while not end of the postfix expression Get next token If (token is operand) Create a new node with the operand Push new node into stack S If (token is operator) pop corresponding operands from S create a new node with the operator (and corresponding operands as left/right children) push new node into stack S end while S.top is the final binary expression tree

26 a b + c d e + * * a b

27 a b + c d e + * * + a b

28 a b + c d e + * * + a b dec

29 a b + c d e + * * + a b de c +

30 a b + c d e + * * + a b de c+ *

31 a b + c d e + * * + a b de c+ * *

Obtaining Infix Expression from Binary Expression Tree Traversing the tree using inorder traversal How to ensure correct precedence –Adding proper parentheses 32

33 Reading assignment Section 4.3