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

Similar documents
TREES. Trees - Introduction

Binary Trees, Binary Search Trees

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

Garbage Collection: recycling unused memory

Programming II (CS300)

Data Structures and Algorithms for Engineers

Chapter 20: Binary Trees

IX. Binary Trees (Chapter 10)

Binary Trees. Examples:

Binary Trees

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

Principles of Computer Science

Trees. CSE 373 Data Structures

Trees. Truong Tuan Anh CSE-HCMUT

CE 221 Data Structures and Algorithms

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

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

BBM 201 Data structures

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

Trees, Binary Trees, and Binary Search Trees

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

Binary Trees. Height 1

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

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

a graph is a data structure made up of nodes in graph theory the links are normally called edges

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

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

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

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

Why Do We Need Trees?

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

Algorithms. AVL Tree

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

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

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

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

CS 206 Introduction to Computer Science II

Binary Trees Fall 2018 Margaret Reid-Miller

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

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

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

CSC148 Week 6. Larry Zhang

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

Successor/Predecessor Rules in Binary Trees

INF2220: algorithms and data structures Series 1

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

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

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

Trees. A tree is a directed graph with the property

Self-Balancing Search Trees. Chapter 11

Friday Four Square! 4:15PM, Outside Gates

Topic 14. The BinaryTree ADT

COMP Analysis of Algorithms & Data Structures

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

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

DATA STRUCTURES USING C

Associate Professor Dr. Raed Ibraheem Hamed

CS302 - Data Structures using C++

COMP Analysis of Algorithms & Data Structures

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

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

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

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

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

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

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

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Algorithms and Data Structures

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

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

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

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

CS24 Week 8 Lecture 1

March 20/2003 Jayakanth Srinivasan,

Cpt S 122 Data Structures. Data Structures Trees

COMP : Trees. COMP20012 Trees 219

CS301 - Data Structures Glossary By

CSI33 Data Structures

EE 368. Weeks 5 (Notes)

Binary search trees (BST) Binary search trees (BST)

Chapter 5. Binary Trees

Binary Trees and Binary Search Trees

Programming II (CS300)

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

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

Advanced Tree Data Structures

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary trees. Binary trees. Binary trees

Algorithms. Deleting from Red-Black Trees B-Trees

tree nonlinear Examples

Binary Search Trees 1

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

Transcription:

Introduc)on Linear structures Removing this idea, treasure of applicaons Estruturas de Dados / Programação 2 Árvores Márcio Ribeiro marcio@ic.ufal.br twitter.com/marciomribeiro Hierarchical structure Companies Boeing Airbus 3 5 319 320 330 Trees CRB CSA The structure of Trees At most one predecessor but many successors Defini)ons Let T be a tree, and let n and m be nodes of T Root has no predecessor Every posion except the root has exactly one predecessor Every posion except the root has the root as a (perhaps not immediate) predecessor n and m are siblings if they have the same parent A node is a leaf of T if it has no children A node is an internal node if it is not a leaf An edge of T connects two nodes (parent and child) A path in T is a collecon of edges that join two nodes; the length of a path consists of the number of edges in the path

Defini)ons Let T be a tree, and let n and m be nodes of T The height of a node is the length of the longest path from the node to a leaf The depth of a node is the length of the path from that node to the root The height of a tree T is the height of the root, namely, the length of the longest path from the root to a leaf Exercise a and b are? h is a descendant of? What are the leaves? What are the internal nodes? height(b)? depth(b)? height(tree)? subtree(b)? a r c d e f b A subtree S, of T rooted at n is a tree that is made from T by considering n to be the root of S and including all S descendants of n g h Defini)on A binary tree consists of either: Nothing at all (empty tree) or A node (called root) of the binary tree along with a lei subtree and a right subtree, both of which are binary trees Binary Tree a b c Empty Empty Empty d Empty Empty Quiz Is the following tree a binary one? Degree of a node Each node can have the following degrees: Zero (no children) One Two 9 5 3 1 2 9 2

Strictly Binary Tree Is the following tree strictly binary?! is a tree in which every node other than the leaves has two children Each node has degree zero or two 9 Why? 9 1 2 1 2 5 3 5 3 Complete Binary Tree If all leaves of a strictly binary tree are at the same depth 9 Abstract Data Type: Binary Tree 5 3 1 2 Binary Tree ADT Struct binary_tree create_empty_binary_tree(); binary_tree create_binary_tree( int item, binary_tree left, binary_tree right); binary_tree search(binary_tree bt, int item); int is_empty(binary_tree bt); void print_in_order(binary_tree bt); void print_pre_order(binary_tree bt); void print_post_order(binary_tree bt); struct binary_tree int item; binary_tree left; binary_tree right; ; 9 5 3 1 2

Func)ons to create Trees binary_tree create_empty_binary_tree() return NULL; binary_tree create_binary_tree( int item, binary_tree left, binary_tree right) binary_tree new_binary_tree = (binary_tree) malloc(sizeof(binary_tree)); new_binary_tree->item = item; new_binary_tree->left = left; new_binary_tree->right = right; return new_binary_tree; Crea)ng a Tree binary_tree bt = create_binary_tree(, create_binary_tree(, NULL, NULL), create_binary_tree(9, create_binary_tree(1, NULL, NULL), create_binary_tree(2, NULL, NULL))); 9 1 2 Binary Tree Traversals Binary Tree Traversals Inspecng each node Inorder Preorder Postorder 3 (9 + 2) 3 + Inorder Preorder void print_in_order(binary_tree bt) if (!is_empty(bt)) print_in_order(bt->left); printf( %d, bt->item); print_in_order(bt->right); void print_pre_order(binary_tree bt) if (!is_empty(bt)) printf( %d, bt->item); print_pre_order(bt->left); print_pre_order(bt->right); 3 (9 + 2)

Postorder Poscomp 2009 void print_post_order(binary_tree bt) if (!is_empty(bt)) print_post_order(bt->left); print_post_order(bt->right); printf( %d, bt->item); 3 + Exercise Which order would you use to deallocate all nodes? Postorder! Binary Search Tree Binary search in arrays Dynamic structure O(log n) 12 23 5 99 101 212 0 1 2 3 5 Array must be sorted Insert/Delete operaons Rearrange the array Expensive So we need a dynamic data structure to add/remove nodes in constant me Linked list: Insert/Remove O(1) But where is the middle?!

Binary Search Tree We have a way of finding the middle element The root of a subtree is to the right of its lei subtree and to the lei of its right subtree 2 Binary Search Tree The item of a node is larger than any item node in its lei subtree and smaller than any item node in its right subtree 2 2 > 1 2 < 3 Intui)on Exercise Is the following tree a Binary Search one? n < n > n 9 No! 3 < 3 Searching for 3 Exercise: implement the search func)on < Recursive version Try to implement a non-recursive version at home 2 > 2 binary_tree search(binary_tree bt, int item) if ((bt == NULL) (bt->item == item)) return bt; else if (bt->item > item) return search(bt->left, item); else return search(bt->right, item);

Degenerate Tree All nodes have only one subtree What happens with our binary search? O(n) Unbalanced trees (AVLs to solve this problem; next class!) 9 n < n > n Adding nodes Now we need to keep everything sorted! binary_tree add(binary_tree bt, int item) if (bt == NULL) bt = create_binary_tree(item, NULL, NULL); else if (bt->item > item) bt->left = add(bt->left, item); else bt->right = add(bt->right, item); return bt; Using double pointer Remove 1. Removing a leaf void add(binary_tree bt, int item) if (bt == NULL) bt = create_binary_tree(item, NULL, NULL); else if ((bt)->item > item) add(&(bt)->left, item); else add(&(bt)->right, item); 2. Removing a node which contains only one subtree 3. Removing a node which contains two subtrees 2

Removing a leaf Removing leaf 3 Removing a node with one subtree Removing node 2 2 Removing a node with two subtrees Removing node 3 Removing node N Choose node R: Successor (leimost item from the right subtree) 3 9 3 9 9 Predecessor (rightmost item from the lei subtree) 1 1 1 Replace the item of N with the R (successor or predecessor) Delete R 5 Leftmost from the right subtree 5 5 Huffman coding A trie for keys "A","to", "tea", "ted", "ten", "i", "in", and "inn". Applications Morse code representation From http://apfelmus.nfshost.com/articles/fun-with-morse-code.html From http://en.wikipedia.org/wiki/trie and /Huffman_coding

References Chapter 12 Chapter