Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Similar documents
Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

March 20/2003 Jayakanth Srinivasan,

CSE 230 Intermediate Programming in C and C++ Binary Tree

Binary Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Binary Trees, Binary Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Introduction to Computers and Programming. Concept Question

Trees. Tree Structure Binary Tree Tree Traversals

Programming II (CS300)

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Trees. Truong Tuan Anh CSE-HCMUT

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

TREES. Trees - Introduction

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Trees. CSE 373 Data Structures

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Chapter 20: Binary Trees

CSC148 Week 6. Larry Zhang

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Data Structures - Binary Trees and Operations on Binary Trees

BBM 201 Data structures

CE 221 Data Structures and Algorithms

Trees. A tree is a directed graph with the property

Binary Trees. Height 1

Data Structure - Binary Tree 1 -

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

CS302 - Data Structures using C++

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Garbage Collection: recycling unused memory

Data Structures and Algorithms

Successor/Predecessor Rules in Binary Trees

Trees. Eric McCreath

Binary Trees Fall 2018 Margaret Reid-Miller

Graph Algorithms Using Depth First Search

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Trees. Trees. CSE 2011 Winter 2007

ITEC2620 Introduction to Data Structures

Trees Algorhyme by Radia Perlman

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Algorithms and Data Structures

COMP : Trees. COMP20012 Trees 219

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

CSE 214 Computer Science II Introduction to Tree

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CSI33 Data Structures

COSC 2011 Section N. Trees: Terminology and Basic Properties

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Analysis of Algorithms

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Data Structures Question Bank Multiple Choice

Data Structure. IBPS SO (IT- Officer) Exam 2017

EE 368. Weeks 5 (Notes)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Why Do We Need Trees?

[ DATA STRUCTURES ] Fig. (1) : A Tree


(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Trees: examples (Family trees)

Data Structures and Algorithms for Engineers

CS24 Week 8 Lecture 1

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

CSI33 Data Structures

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Programming II (CS300)

Topic 14. The BinaryTree ADT

Lecture 6: Analysis of Algorithms (CS )

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

UNIT III TREES. A tree is a non-linear data structure that is used to represents hierarchical relationships between individual data items.

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

A set of nodes (or vertices) with a single starting point

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

CSC148H Week 8. Sadia Sharmin. July 12, /29

Tree traversals and binary trees

Objectives. In this session, you will learn to:

Algorithms. Deleting from Red-Black Trees B-Trees

Data Structures and Algorithms

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Principles of Computer Science

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Transcription:

Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree, (ii) If n > 0, then there exists a distinct element r, called the root, such that T {r} can be partitioned into k disjoint subsets T 1, T 2,..., T k, k 0, such that each of these subsets also forms a tree. asic Concepts: Elements of T are nodes in the tree T. Each subset T i is a subtree of r. The roots of the subtrees of r are children of r. The root r is the parent of the roots of its subtrees. Nodes with the same parent are siblings. Node, except the root, with no children is leaf. Degree of a node is the number of children of the node. 1

path of length k (in a tree) from node x to node y is a sequence of k+1 nodes x = n 0, n 1, n 2,..., n k = y such that n i is the parent of n i+1 for all i = 0, 1,..., k 1. If there is a path from x to y, then x is an ancestor of y and y is a descendant of x. For any given node x, the depth (level) of x is the length of the (unique) path from the root to x, and the height of x is the length of a longest path from x to any leaf. The height of a tree is the height of its root. The depth of a tree is the maximum depth of its nodes. Remarks: 1. There is a unique path from the root to each node in the tree. 2. There is a path of zero length from any node to itself. 3. The depth (level) of the root of a tree is 0. 4. The height of a leaf is 0. 5. Height of tree = Depth of tree. 6. For convenience, the height of an empty tree is defined to be 1. Warning: Some authors define the depth, or level, of the root to be 1. 2

Graphical Representation of Tree: Depth (Level) 0 C D 1 E F G 2 H I 3 is the root of tree T, D is the parent of F and G, F and G are children of D, Degree of F is 2,, C, D are siblings, C, E, G, H, I are leaves, D is an ancestor of I and I is a descendant of D, (,D,F,H) is a path of length 3, H is of height 0 but depth 3. Height (depth) of the tree is 3. 3

Some Important Classes of Trees: 1. k-ary tree, k 2: Each node has at most k children, k 2. 2. inary tree: n ordered k-ary tree when k = 2. Recursive Definition of inary Tree: Let T be a set with n 0 elements. T is a binary tree iff (i) T is empty, or (ii) if T is not empty, T has a root r such that T- {r} can be partitioned into two disjoint binary trees T L and T R, called the left subtree and right subtree of r. Example: binary tree. D E F G H 4

3. Complete binary tree: complete binary tree T with height h is a binary tree such that (1) Each node on levels 0 to h 2 must have exactly two children, (2) Leaves can only be found on the two highest levels (levels h 1 and h), and (2) Leaves on level h are left-justified. Warning: Some authors do not require Condition (3). Examples: (a) Incomplete binary tree: (b) Complete binary tree: 4. Full binary tree: 5

full binary tree of height h is a binary tree such that each node on levels 0 to h-1 is having exactly two children. Hence, full binary tree of height h has exactly 2 h+1 1 nodes. Example: full binary tree with h = 2. 5. Skew tree: skew tree T is a binary tree such that each nonleaf node in T has exactly one child. Example: Some skew trees. 6. alanced binary tree: binary tree T such that for each node x in the 6

tree, the height of its left subtree differs by no more than one from the height of its right subtree. Hence, h(t L (x)) h(t R (x)) 1, where h(t L (x)), and h(t L (x)), are the height of the left, and right, subtree rooted at x, respectively. Examples: alanced binary tree showing height of nodes: 3 2 1 0 1 0 Not a balanced binary tree showing height of nodes: 4 0 3 1 0 2 0 1 0 Observation: {full trees} {complete binary trees} {balanced binary trees} 7

When implementation an DT using a (binary) tree, either non-leaf or leaf-nodes can be used to hold the data objects of the DT and the performance of an DT operation will usually depend on the height of the tree. Nodes, leaves, and heights of binary trees: Given a non-empty binary tree T. 1. T with height h, h 0: Min # nodes = h+1. Max # nodes = 2 h+1 1. 2. T with height h, h 0: Min # leaves = 1. Max # leaves = 2 h. 3. T with n nodes, n 1: Min height = log 2 n. Max height = n 1. 4. T with n nodes, n 1: Min # leaves = 1. Max # leaves = n/2. 5. T with m leaves, m 1: Min height = log 2 m. Max height =. 6. T with m leaves, m 1: Min # nodes = 2m 1. Max # nodes =. 8

Traversing a inary Tree: 1. To explore the underlying hierarchical structures. 2. To retrieve information stored in nodes. 3. To store and restore the topological structure of a tree (relation). asic inary Tree Traversal lgorithms: 1. Preorder traversal: Traverse/retrieve root, Traverse left subtree recursively in preorder, Traverse right subtree recursively in preorder. 2. Postorder traversal: Traverse left subtree recursively in postorder, Traverse right subtree recursively in postorder, Traverse/retrieve root. 3. Inorder traversal: Traverse left subtree recursively in inorder, Traverse/retrieve root, Traverse right subtree recursively in inorder. 4. Level-order traversal: Starting at level 0, traverse the nodes on each level from left to right level by level. 9

Example: inary tree traversals. D E F G H Preorder: E F D G H Postorder: E F H G D Inorder: E F D H G Level-order: D E F G H More on binary tree traversals: Observe that for a given binary tree T, it is easy to traverse T. Q: If the traversal of a binary tree T is given, can we reconstruct the binary tree T? No! 10

Consider the following binary trees: Observe that preorder, postorder, and level-order traversals for both trees are given by,, and, respectively. Hence, these two binary trees are indistinguishable if only one of the above tree traversals is given! Q: How about inorder traversal? Consider the following binary trees: gain, these two binary trees are indistinguishable if only their inorder traversal is given! 11

Q: Can we reconstruct the binary tree T from its traversal(s)? Must know the root, the left, and the right subtrees. If we are given any one of the following pairs of traversals of T, T can be reconstructed if exists. 1. Preorder and inorder traversals. 2. Postorder and inorder traversals. 3. Level-order and inorder traversals. Remark: Preorder and postorder, level order and preorder, and level-order and postorder traversals will not work! HW. Can you reconstruct a binary tree T having the following pairs of traversals? lgorithms? Preorder: Inorder: Preorder: Inorder: D C E F G I H F E G C D I H C D H I E F G D I H C E G F Preorder: C D H I E F G LevelOrder: C D E F H G I Postorder: Inorder: K J G D H F I E C D K G J C F H E I 12

Consider given a pair of preorder and inorder traversals of a binary tree T. Q: How do we reconstruct T if exists? Scan preorder traversal from left to right to determine root followed by scanning inorder traversal to determine left and right subtree. Example: Let preorder = DEC and inorder = EDC. DEC EDC ED C DEC EDC C ED DEC EDC C D E DEC EDC 8/26/08 DEC EDC D E C 13