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

Similar documents
Trees and Graphs. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

CSE 214 Computer Science II Introduction to Tree

Binary Trees

Trees. CSE 373 Data Structures

Programming II (CS300)

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Algorithms. AVL Tree

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

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

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

Trees. Eric McCreath

Binary Trees, Binary Search Trees

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

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

CS 171: Introduction to Computer Science II. Binary Search Trees

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

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

V Advanced Data Structures

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

TREES. Trees - Introduction

Data Structures and Organization (p.5 Recursion. Binary Trees)

CSE 5311 Notes 4a: Priority Queues

On Integer Sequences Derived from Balanced k-ary trees

Priority Queues and Binary Heaps

B-Trees. CS321 Spring 2014 Steve Cutchin

CE 221 Data Structures and Algorithms

V Advanced Data Structures

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Recursive Data Structures and Grammars

Non-context-Free Languages. CS215, Lecture 5 c

Data Structures and Algorithms

BBM 201 Data structures

ITI Introduction to Computing II

CSE 100 Advanced Data Structures

Trees. Trees. CSE 2011 Winter 2007

CS24 Week 8 Lecture 1

Trees and Tree Traversal

Balanced Search Trees. CS 3110 Fall 2010

The priority is indicated by a number, the lower the number - the higher the priority.

ITI Introduction to Computing II

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

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

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

CS350: Data Structures B-Trees

Garbage Collection: recycling unused memory

Binary Heaps in Dynamic Arrays

March 20/2003 Jayakanth Srinivasan,

Introduction to Computers and Programming. Concept Question

Programming II (CS300)

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

18.3 Deleting a key from a B-tree

CSC148 Week 6. Larry Zhang

Algorithms. Deleting from Red-Black Trees B-Trees

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

EE 368. Weeks 5 (Notes)

Topic: Heaps and priority queues

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

Why Do We Need Trees?

CS F-11 B-Trees 1

Building Java Programs

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

Properties of red-black trees

Binary Trees Fall 2018 Margaret Reid-Miller

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Chapter 20: Binary Trees

Data Structures and Algorithms

1. Why Study Trees? Trees and Graphs. 2. Binary Trees. CITS2200 Data Structures and Algorithms. Wood... Topic 10. Trees are ubiquitous. Examples...

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

CS 350 : Data Structures B-Trees

Figure 4.1: The evolution of a rooted tree.

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

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

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

Trees. Truong Tuan Anh CSE-HCMUT

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

Unit #2: Priority Queues

Recursion and Structural Induction

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Organizing Spatial Data

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

Analysis of Algorithms

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

B-Trees and External Memory

Trees Algorhyme by Radia Perlman

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Data Structures and Algorithms

B-Trees and External Memory

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

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

COMP Analysis of Algorithms & Data Structures

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

Properties of a heap (represented by an array A)

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

Lecture 6: Analysis of Algorithms (CS )

Data and File Structures Laboratory

TREES Lecture 12 CS2110 Spring 2019

Transcription:

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

Trees Trees are a natural data structure for representing specific data. Family trees. Organizational chart of a corporation, showing who supervises who. Folder (directory) structure on a hard drive. 2

Terminology Root: 0 Path: 0-2-6, 1-3, 1-3-4 Parent vs child Ascendants vs descendants: ascendants of 3: 1,0 // descendants of 1: 5, 3,4 Internal vs external nodes (non-terminal vs terminal nodes) Leaves: 5,4,6,7 M-ary trees (Binary trees) General trees Subtree 0 1 2 5 3 6 7 4 3

Terminology If Y is the parent of X, then X is called a child of Y. The root has no parents. Every other node, except for the root, has exactly one parent. A node can have 0, 1, or more children. Nodes that have children are called internal nodes or non-terminal nodes. Nodes that have no children are called terminal nodes, external nodes, or leaves. 4

M-ary Trees - Worksheet An M-ary tree is a tree where every node is either a leaf or it has exactly M children. Example: binary trees, ternary trees,... 0 0 1 2 1 2 5 3 6 7 6 7 4 Is this a binary tree? Is this a binary tree? 5

M-ary Trees - Answers An M-ary tree is a tree where every node is either a leaf or it has exactly M children. Example: binary trees, ternary trees,... 0 0 1 2 1 2 5 3 6 7 6 7 4 This is a binary tree. This is not a binary tree, node 3 has 1 child. 6

Types of binary trees Perfect tree Perfect each internal node has exactly 2 children and all the leaves are on the same level. E.g. ancestry tree (anyone will have exactly 2 parents). Full every node has exactly 0 or 2 children. E.g. tree generated by the Fibonacci recursive calls. Binary tree. Complete tree every level, except for possibly the last one is completely filled and on the last level, all the nodes are as far on the left as possible. E.g. the heap tree. Height: lg N and it can be stored as an array. Reference: wikipedia (https://en.wikipedia.org/wiki/binary_tree) Complete tree 7

Complete Tree All levels are full, except possibly for the last level. At the last level: Nodes are on the left. Empty positions are on the right. There is no hole Good Bad Bad Bad 3 5 4 3 2 1 1 3 4 1 1 2 3 4 5 6 7 8 10 11 12 13 8

Worksheet Self study: Give examples of trees that are: Perfect Full but not complete Complete but not full Neither full not complete

Worksheet For each tree, say if it is full, complete or perfect. E2 3 5 5 5 E1 3 5 5 5 2 4 1 3 5 5 5 5 E3 3 5 5 5 5 5 1 3 5 E4 1 3 5 5 5 5 E5 8 4 3 5 5 2 1 1 3 5 5 1 4 3 6 2 1 0 4 0 10

Answers For each tree, say if it is perfect, full or complete. 5 5 E2 E4 2 1 1 3 5 5 Complete, not full 3 5 5 5 1 3 5 Neither full, nor complete 3 5 5 E1 3 5 5 5 2 4 1 3 5 5 5 5 E3 8 4 1 4 3 6 2 1 0 4 0 Perfect Full, not complete 3 5 5 5 1 3 5 5 5 5 E5 Neither full, nor complete 11

Terminology - Worksheet The level of the root is defined to be 0. The level of each node is defined to be 1+ the level of its parent. The depth of a node is the number of edges from the root to the node. (It is equal to the level of that node.) The height of a node is the number of edges from the node to the deepest leaf. (Treat that node as the root of a small tree) C A B Node level depth height A B C Practice: - Give the level, depth and height for each of the red nodes. - What kinds of tree is this (perfect/full/complete)? - How many nodes are on each level? 12

Terminology - Answers The level of the root is defined to be 0. The level of each node is defined to be 1+ the level of its parent. The depth of a node is the number of edges from the root to the node. (It is equal to the level of that node.) The height of a node is the number of edges from the node to the deepest leaf. (Treat that node as the root of a small tree) C A B Node level depth height A 0 0 3 B 2 2 1 C 3 3 0 For any node, depth + height = tree height Practice: - Give the level, depth and height for each of the red nodes. - What kinds of tree is this (perfect/full/complete)? perfect - How many nodes are on each level? 1, 2, 4, 8 13

Perfect Binary Trees A perfect binary tree with N nodes and height h, has: N / 2 leaves (half the nodes are on the last level) N / 2 internal nodes (half the nodes are internal) Height : h lg N Levels : lg N 1 (= lg(n+1) ) In the other direction: N = 2 h+1-1 1 2 3 4 5 6........................ 7 Level Nodes per level Sum of nodes from root up to this level 0 2 0 (=1) 2 1 1 (=1) 1 2 1 (=2) 2 2 1 (=3) 2 2 2 (=4) 2 3 1 (=7) i 2 i 2 i+1 1 h k=0 2 k = 2 h+1 1 h 2 h 2 h+1 1 14

Properties of Full Trees A full binary tree (0/2 children) with P internal nodes has: P+1 external nodes. 2P edges (links). height at least lg P and at most P: lg P if all external nodes are at the same level (perfect tree) P if each internal node has one external child. 15

Proof Prove that a full tree with P internal nodes has P+1 external leaves. Full tree property: What proof style will you use? 16