Algorithms and Data Structures CS-CO-412

Similar documents
Data Structures and Algorithms for Engineers

Algorithms and Data Structures CS-CO-412

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

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

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

Programming II (CS300)

Data Structures and Algorithms for Engineers

Chapter 20: Binary Trees

TREES. Trees - Introduction

Algorithms. Deleting from Red-Black Trees B-Trees

Binary Trees, Binary Search Trees

Data Structures and Algorithms for Engineers

Advanced Set Representation Methods

Data Structures and Algorithms for Engineers

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

CSE 214 Computer Science II Introduction to Tree

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

Data Structure - Binary Tree 1 -

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

Trees. CSE 373 Data Structures

COMP : Trees. COMP20012 Trees 219

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

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

Definition of Graphs and Trees. Representation of Trees.

CS24 Week 8 Lecture 1

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

CS302 - Data Structures using C++

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

1. Introduction. Lists. 2. List windows. Data Structures and Algorithms

Red-Black trees are usually described as obeying the following rules :

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

Data and File Structures Laboratory

csci 210: Data Structures Trees

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

Trees. A tree is a directed graph with the property

CS 206 Introduction to Computer Science II

Building Java Programs

B-Trees. CS321 Spring 2014 Steve Cutchin

BBM 201 Data structures

Trees, Part 1: Unbalanced Trees

ITI Introduction to Computing II

CS 151. Binary Trees. Friday, October 5, 12

INF2220: algorithms and data structures Series 1

Algorithms. AVL Tree

Data Structures and Algorithms

CS350: Data Structures Red-Black Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

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

Self-Balancing Search Trees. Chapter 11

ITI Introduction to Computing II

Topic 14. The BinaryTree ADT

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

Laboratory Module Trees

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

B-Trees and External Memory

Binary Trees

Garbage Collection: recycling unused memory

Topic Binary Trees (Non-Linear Data Structures)

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

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Algorithms and Data Structures

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

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

DATA STRUCTURES USING C

Trees. Eric McCreath

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

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

CS301 - Data Structures Glossary By

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

Why Do We Need Trees?

Binary Trees Fall 2018 Margaret Reid-Miller

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

CS 206 Introduction to Computer Science II

Data Structures in Java

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)


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

B-Trees and External Memory

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

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

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Fall, 2015 Prof. Jungkeun Park

Algorithms and Data Structures CS-CO-412

CSCI2100B Data Structures Trees

CS2 Algorithms and Data Structures Note 6

CMSC 341 Leftist Heaps

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

Trees and Binary Trees

TREES Lecture 10 CS2110 Spring2014

A red-black tree is a balanced binary search tree with the following properties:

CS2 Algorithms and Data Structures Note 6

CS102 Binary Search Trees

CSC148 Week 6. Larry Zhang

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

CmpSci 187: Programming with Data Structures Spring 2015

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

Transcription:

Algorithms and Data Structures CS-CO-412 David Vernon Professor of Informatics University of Skövde Sweden david@vernon.eu www.vernon.eu Algorithms and Data Structures 1 Copyright D. Vernon 2014

Trees Lecture 7 Algorithms and Data Structures 2 Copyright D. Vernon 2014

Topic Overview Types of trees Binary Tree ADT Binary Search Tree Optimal Code Trees Huffman s Algorithm Height Balanced Trees AVL Trees Red-Black Trees Algorithms and Data Structures 3 Copyright D. Vernon 2014

Trees Trees are everywhere Hierarchical method of structuring data Uses of trees: genealogical tree organizational tree expression tree binary search tree decision tree Algorithms and Data Structures 4 Copyright D. Vernon 2014

Uses of Trees Organiza/on Tree Algorithms and Data Structures 5 Copyright D. Vernon 2014

Uses of Trees Code Tree 0 1 a 0 1 0 1 b c d Algorithms and Data Structures 6 Copyright D. Vernon 2014

Uses of Trees Binary Seach Tree Sun Mon Tue Fri Sat Thur Wed Algorithms and Data Structures 7 Copyright D. Vernon 2014

Uses of Trees Decision Tree No Alarm? Yes No Sensors Opera/ve? No Night? Yes Override? Alert Algorithms and Data Structures 8 Copyright D. Vernon 2014

Trees Fundamentals Traversals Display Representation Abstract Data Type (ADT) approach Emphasis on binary tree Also multi-way trees, forests, orchards Algorithms and Data Structures 9 Copyright D. Vernon 2014

Tree Definitions A binary tree T of n nodes, n 0, either is empty, if n = 0 or consists of a root node u and two binary trees u(1) and u(2) of n 1 and n 2 nodes, respectively, such that n = n + n 1 + n 2 We say that u(1) is the first or left subtree of T, and u(2) is the second or right subtree of T Algorithms and Data Structures 10 Copyright D. Vernon 2014

Binary Tree Binary Tree of zero nodes Algorithms and Data Structures 11 Copyright D. Vernon 2014

Binary Tree 1 2 Binary Tree of 1 nodes Algorithms and Data Structures 12 Copyright D. Vernon 2014

Binary Tree 1 2 1 2 Binary Tree of 2 nodes Algorithms and Data Structures 13 Copyright D. Vernon 2014

Binary Tree 1 2 1 2 1 2 Binary Tree of 3 nodes Algorithms and Data Structures 14 Copyright D. Vernon 2014

Binary Tree External nodes - have no subtrees (referred to as leaf nodes) Internal nodes - always have two subtrees Algorithms and Data Structures 15 Copyright D. Vernon 2014

Binary Tree Terminology Let T be a binary tree with root u Let v be any node in T If v is the root of either u(1) or u(2), then we say u is the parent of v and that v is the child of u If w is also a child of u, and w is distinct from v, we say that v and w are siblings Algorithms and Data Structures 16 Copyright D. Vernon 2014

Algorithms and Data Structures 17 Copyright D. Vernon 2014

Binary Tree Terminology If v is the root of u(i) then v is the ith child of u; u(1) is the left child and u(2) is the right child Also have grandparents and grandchildren Algorithms and Data Structures 18 Copyright D. Vernon 2014

Algorithms and Data Structures 19 Copyright D. Vernon 2014

Binary Tree Terminology Given a binary tree T of n nodes, n 0 then v is a descendent of u if either v is equal to u or v is a child of some node w and w is a descendant of u We write v desc T u v is a proper descendent of u if v is a descendant of u and v u Algorithms and Data Structures 20 Copyright D. Vernon 2014

Algorithms and Data Structures 21 Copyright D. Vernon 2014

Binary Tree Terminology Given a binary tree T of n nodes, n 0 then v is a left descendent of u if either v is equal to u or v is a left child of some node w and w is a left descendant of u We write v ldesc T u Similarly we have v rdesc T u Algorithms and Data Structures 22 Copyright D. Vernon 2014

Algorithms and Data Structures 23 Copyright D. Vernon 2014

Binary Tree Terminology left T relates nodes across a binary tree rather than up and down a binary tree Given two nodes u and v in a binary tree T, we say that v is to the left of u if there is a new node w in T such that v is a left descendant of w and u is a right descendant of w We denote this relation by left T and write v left T u Algorithms and Data Structures 24 Copyright D. Vernon 2014

Algorithms and Data Structures 25 Copyright D. Vernon 2014

Binary Tree Terminology The external nodes of a tree define its frontier We can count the number of nodes in a binary tree in three ways: Number of internal nodes Number of external nodes Number of internal and external nodes The number of internal nodes is the size of the tree Algorithms and Data Structures 26 Copyright D. Vernon 2014

Binary Tree Terminology Let T be a binary tree of size n, n 0, Then, the number of external nodes of T is n + 1 Algorithms and Data Structures 27 Copyright D. Vernon 2014

Algorithms and Data Structures 28 Copyright D. Vernon 2014

Binary Tree Terminology The height of T is defined recursively as 0 if T is empty and 1 + max(height(t 1 ), height(t 2 )) otherwise, where T 1 and T 2 are the subtrees of the root. The height of a tree is the length of a longest chain of descendents Algorithms and Data Structures 29 Copyright D. Vernon 2014

Algorithms and Data Structures 30 Copyright D. Vernon 2014

Binary Tree Terminology Height Numbering Number all external nodes 0 Number each internal node to be one more than the maximum of the numbers of its children Then the number of the root is the height of T The height of a node u in T is the height of the subtree rooted at u Algorithms and Data Structures 31 Copyright D. Vernon 2014

Algorithms and Data Structures 32 Copyright D. Vernon 2014

Algorithms and Data Structures 33 Copyright D. Vernon 2014

Binary Tree Terminology Levels of nodes The level of a node in a binary tree is computed as follows Number the root node 0 Number every other node to be 1 more than its parent Then the number of a node v is that node s level The level of v is the number of branches on the path from to root to v Algorithms and Data Structures 34 Copyright D. Vernon 2014

Algorithms and Data Structures 35 Copyright D. Vernon 2014

Algorithms and Data Structures 36 Copyright D. Vernon 2014

Binary Tree Terminology Skinny Trees every internal node has at most one internal child Algorithms and Data Structures 37 Copyright D. Vernon 2014

Algorithms and Data Structures 38 Copyright D. Vernon 2014

Binary Tree Terminology Complete Binary Trees (Fat Trees) the external nodes appear on at most two adjacent levels Perfect Trees: complete trees having all their external nodes on one level Left-complete Trees: the internal nodes on the lowest level is in the leftmost possible position Skinny trees are the highest possible trees Complete trees are the lowest possible trees Algorithms and Data Structures 39 Copyright D. Vernon 2014

Complete Tree Algorithms and Data Structures 40 Copyright D. Vernon 2014

Perfect Tree Algorithms and Data Structures 41 Copyright D. Vernon 2014

Left-Complete Tree Algorithms and Data Structures 42 Copyright D. Vernon 2014

Binary Tree Terminology A binary tree of height h 0 has size at least h A binary tree of height at most h 0 has size at most 2 h 1 A binary tree of size n 0 has height at most n A binary tree of size n 0 has height at least log (n + 1) Algorithms and Data Structures 43 Copyright D. Vernon 2014

Multiway Trees Multiway trees are defined in a similar way to binary trees, except that the degree (the maximum number of children) is no longer restricted to the value 2 Algorithms and Data Structures 44 Copyright D. Vernon 2014

Multiway Trees A multiway tree T of n internal nodes, n 0, either is empty, if n = 0, or consists of a root node u, an integer d u 1, the degree of u, and multiway trees u(1) of n 1 nodes,..., u(d u ) of n d u nodes such that n = 1 + n 1 +... + n d u Algorithms and Data Structures 45 Copyright D. Vernon 2014

Multiway Trees A multiway tree T is a d-ary tree, for some d > 0, if d u = d, for all internal nodes u in T Algorithms and Data Structures 46 Copyright D. Vernon 2014

d-ary Tree Algorithms and Data Structures 47 Copyright D. Vernon 2014

Multiway Trees A multiway tree T is a (a, b)-tree, if 1 a d u b, for all u in T Every binary tree is a (2, 2)-tree, and vice versa Algorithms and Data Structures 48 Copyright D. Vernon 2014

BINARY_TREE & TREE Specification So far, no values associated with the nodes of a tree Now want to introduce an ADT called BINARY_TREE, which has value of type intelementtype associated with the internal nodes has value of type extelementtype associated with the external nodes These value don t have any effect on BINARY_TREE operations Algorithms and Data Structures 49 Copyright D. Vernon 2014

BINARY_TREE & TREE Specification BINARY_TREE has explicit windows and windowmanipulation operations A window allows us to see the value in a node (and to gain access to it) Windows can be positioned over any internal or external node Windows can be moved from parent to child Windows can be moved from child to parent Algorithms and Data Structures 50 Copyright D. Vernon 2014

Window Algorithms and Data Structures 51 Copyright D. Vernon 2014

BINARY_TREE & TREE Specification Let BT denote denote the set of values of BINARY_TREE of elementtype Let E denote the set of values of type elementtype Let W denote the set of values of type windowtype Let B denote the set of Boolean values true and false Algorithms and Data Structures 52 Copyright D. Vernon 2014

BINARY_TREE Operations Empty: BT BT : The function Empty(T) is an empty binary tree; if necessary, the tree is deleted IsEmpty: BT B : The function value IsEmpty(T) is true if T is empty; otherwise it is false Algorithms and Data Structures 53 Copyright D. Vernon 2014

Example Algorithms and Data Structures 54 Copyright D. Vernon 2014

BINARY_TREE Operations Root: BT W : The function value Root(T) is the window position of the single external node if T is empty; otherwise it is the window position of the root of T Algorithms and Data Structures 55 Copyright D. Vernon 2014

Example Algorithms and Data Structures 56 Copyright D. Vernon 2014

BINARY_TREE Operations IsRoot: W BT B : The function value IsRoot(w, T) is true if the window w is over the root; otherwise it is false Algorithms and Data Structures 57 Copyright D. Vernon 2014

Example Algorithms and Data Structures 58 Copyright D. Vernon 2014

BINARY_TREE Operations IsExternal: W BT B : The function value IsExternal(w, T) is true if the window w is over an external node of T; otherwise it is false Algorithms and Data Structures 59 Copyright D. Vernon 2014

Example Algorithms and Data Structures 60 Copyright D. Vernon 2014

BINARY_TREE Operations Child: N W BT W : The function value Child(i, w, T) is undefined if the node in the window W is external or the node in w is internal and i is neither 1 nor 2; otherwise it is the ith child of the node in w Algorithms and Data Structures 61 Copyright D. Vernon 2014

Example Algorithms and Data Structures 62 Copyright D. Vernon 2014

BINARY_TREE Operations Parent: W BT W : The function value Parent(w, T) is undefined if T is empty or w is over the root of T; otherwise it is the window position of the parent of the node in the window w Algorithms and Data Structures 63 Copyright D. Vernon 2014

Example Algorithms and Data Structures 64 Copyright D. Vernon 2014

BINARY_TREE Operations Examine: W BT I : The function value Examine(w, T) is undefined if w is over an external node; otherwise it is element at the internal node in the window w Algorithms and Data Structures 65 Copyright D. Vernon 2014

Example Algorithms and Data Structures 66 Copyright D. Vernon 2014

BINARY_TREE Operations Replace: E W BT BT : The function value Replace(e, w, T) is undefined if w is over an external node; otherwise it is T, with the element at the internal node in w replaced by e Algorithms and Data Structures 67 Copyright D. Vernon 2014

Example Algorithms and Data Structures 68 Copyright D. Vernon 2014

BINARY_TREE Operations Insert: E W BT W BT : The function value Insert(e, w, T) is undefined if w is over an internal node; otherwise it is T, with the external node in w replaced by a new internal node with two external children. Furthermore, the new internal node is given the value e and the window is moved over the new internal node. Algorithms and Data Structures 69 Copyright D. Vernon 2014

Example Algorithms and Data Structures 70 Copyright D. Vernon 2014

BINARY_TREE Operations Delete: W BT W BT : The function value Delete(w, T) is undefined if w is over an external node; If w is over a leaf node (both its children are external nodes), then the function value is T with the internal node to be deleted replaced by its left external node Algorithms and Data Structures 71 Copyright D. Vernon 2014

BINARY_TREE Operations Delete: W BT W BT : If w is over an internal node with just one internal node child, then the function value is T with the internal node to be deleted replaced by its child Algorithms and Data Structures 72 Copyright D. Vernon 2014

BINARY_TREE Operations Delete: W BT W BT : if w is over an internal node with two internal node children, then the function value is T with the internal node to be deleted replaced by the leftmost internal node descendent in its right sub-tree In all cases, the window is moved over the replacement node. Algorithms and Data Structures 73 Copyright D. Vernon 2014

Example Algorithms and Data Structures 74 Copyright D. Vernon 2014

BINARY_TREE Operations Left: W BT W : The function value Left(w, T) is undefined if w is over an external node; otherwise it is the window position of the left (or first) child of the node w Algorithms and Data Structures 75 Copyright D. Vernon 2014

Example Algorithms and Data Structures 76 Copyright D. Vernon 2014

BINARY_TREE Operations Right: W BT W : The function value Right(w, T) is undefined if w is over an external node; otherwise it is the window position of the right (or second) child of the node w Algorithms and Data Structures 77 Copyright D. Vernon 2014

Example Algorithms and Data Structures 78 Copyright D. Vernon 2014

TREE Operations Degree: W T I : The function value Degree(w, T) is the degree of the node in the window w Algorithms and Data Structures 79 Copyright D. Vernon 2014

d-ary Tree Algorithms and Data Structures 80 Copyright D. Vernon 2014

TREE Operations Child: N W T W : The function value Child(i, w, T) is undefined if the node in the window w is external, or if the node in w is internal and i is outside the range 1..d, where d is the degree of the node; otherwise it is the ith child of the node in w Algorithms and Data Structures 81 Copyright D. Vernon 2014

d-ary Tree Algorithms and Data Structures 82 Copyright D. Vernon 2014

BINARY_TREE Representation /* pointer implementation of BINART_TREE ADT */! #include <stdio.h>! #include <math.h>! #include <string.h>! #define FALSE 0! #define TRUE 1! typedef struct {! int number;! char *string;! } ELEMENT_TYPE;! Algorithms and Data Structures 83 Copyright D. Vernon 2014

BINARY_TREE Representation typedef struct node *NODE_TYPE;! typedef struct node{! ELEMENT_TYPE element;! NODE_TYPE left, right;! } NODE;! typedef NODE_TYPE BINARY_TREE_TYPE;! typedef NODE_TYPE WINDOW_TYPE;! Algorithms and Data Structures 84 Copyright D. Vernon 2014

BINARY_TREE Representation Algorithms and Data Structures 85 Copyright D. Vernon 2014

BINARY_TREE Representation Tree Window Algorithms and Data Structures 86 Copyright D. Vernon 2014

BINARY_TREE Representation This implementation assumes that we are going to represent external nodes as NULL links For many ADT operations, we need to know if the window is over an internal or an external node we are over an external node if the window is NULL Algorithms and Data Structures 87 Copyright D. Vernon 2014

BINARY_TREE Representation WINDOW Algorithms and Data Structures 88 Copyright D. Vernon 2014

BINARY_TREE Representations Whenever we insert an internal node (remember we can only do this if the window is over an external node) we simply make its two children NULL Algorithms and Data Structures 89 Copyright D. Vernon 2014