Data Structures and Algorithms for Engineers

Similar documents
Binary Trees, Binary Search Trees

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

TREES. Trees - Introduction

Algorithms and Data Structures CS-CO-412

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

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

Analysis of Algorithms

Associate Professor Dr. Raed Ibraheem Hamed

Programming II (CS300)

March 20/2003 Jayakanth Srinivasan,

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

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

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

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

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

Binary Trees

Binary Search Trees. See Section 11.1 of the text.

Advanced Tree Data Structures

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

IX. Binary Trees (Chapter 10)

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

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

Friday Four Square! 4:15PM, Outside Gates

Trees. Truong Tuan Anh CSE-HCMUT

Trees, Binary Trees, and Binary Search Trees

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

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

Trees. Tree Structure Binary Tree Tree Traversals

Algorithms. Deleting from Red-Black Trees B-Trees

Garbage Collection: recycling unused memory

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

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

Algorithms. AVL Tree

INF2220: algorithms and data structures Series 1

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

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

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

Recall: Properties of B-Trees

Introduction to Computers and Programming. Concept Question

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

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

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Cpt S 122 Data Structures. Data Structures Trees

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

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

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

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

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

CSI33 Data Structures

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

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

Trees. CSE 373 Data Structures

Chapter 20: Binary Trees

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Balanced Search Trees. CS 3110 Fall 2010

Algorithms and Data Structures CS-CO-412

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

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

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

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Lecture 23: Binary Search Trees

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

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

Trees. Trees. CSE 2011 Winter 2007

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Lecture 6: Analysis of Algorithms (CS )

Data Structures and Algorithms

Why Do We Need Trees?

CS Transform-and-Conquer

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

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

CE 221 Data Structures and Algorithms

Data Structures and Algorithms

Multi-Way Search Tree

12 Abstract Data Types

Self-Balancing Search Trees. Chapter 11

Binary Trees and Huffman Encoding Binary Search Trees

Data Structures in Java

Lecture 26. Introduction to Trees. Trees

EE 368. Weeks 5 (Notes)

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

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

Binary Search Trees Treesort

Search Trees: BSTs and B-Trees

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

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

Binary Trees Fall 2018 Margaret Reid-Miller

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

3 Trees: traversal and analysis of standard search trees

CIS265/ Trees Red-Black Trees. Some of the following material is from:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Partha Sarathi Mandal

CSCI2100B Data Structures Trees

CSI33 Data Structures

Binary Search Trees 1

INF2220: algorithms and data structures Series 1

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Binary search trees (chapters )

Transcription:

04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University Africa

Lecture 13 Trees I Types of trees Binary Tree ADT Binary Search Tree Height Balanced Trees AVL Trees Red-Black Trees Optimal Code Trees Huffman s Algorithm Data Structures and Algorithms for Engineers 2 Carnegie Mellon University Africa

Binary Search Trees A Binary Search Tree (BST) is a special type of binary tree it represents information is an ordered format A binary tree is binary search tree if for every node w, all keys in the left subtree of i have values less than the key of w and all keys in the right subtree have values greater than key of w. Data Structures and Algorithms for Engineers 3 Carnegie Mellon University Africa

Binary Search Trees Definition: A binary search tree T is a binary tree; either it is empty or each node in the tree contains an identifier and: all keys in the left subtree of T are less (numerically or alphabetically) than the identifier in the root node T; all identifers in the right subtree of T are greater than the identifier in the root node T; The left and right subtrees of T are also binary search trees. Data Structures and Algorithms for Engineers 4 Carnegie Mellon University Africa

Binary Search Trees Sun Mon Tue Fri Sat Thur Wed Data Structures and Algorithms for Engineers 5 Carnegie Mellon University Africa

Binary Search Trees The main point to notice about such a tree is that, if traversed inorder, the keys of the tree (i.e. its data elements) will be encountered in a sorted fashion Furthermore, efficient searching is possible using the binary search technique search time is O(log 2 n) Data Structures and Algorithms for Engineers 6 Carnegie Mellon University Africa

Binary Search Trees It should be noted that several binary search trees are possible for a given data set, e.g, consider the following tree: Data Structures and Algorithms for Engineers 7 Carnegie Mellon University Africa

Binary Search Trees Mon Fri Sat Sun Tue Thur Wed Data Structures and Algorithms for Engineers 8 Carnegie Mellon University Africa

Fri Binary Search Trees Mon Sat Sun Thur Tue Wed Data Structures and Algorithms for Engineers 9 Carnegie Mellon University Africa

Binary Search Trees Let us consider how such a situation might arise To do so, we need to address how a binary search tree is constructed Assume we are building a binary search tree of words Initially, the tree is null, i.e. there are no nodes in the tree The first word is inserted as a node in the tree as the root, with no children Data Structures and Algorithms for Engineers 10 Carnegie Mellon University Africa

Binary Search Trees On insertion of the second word, we check to see if it is the same as the key in the root, less than it, or greater than it If it is the same, no further action is required (duplicates are not allowed) If it is less than the key in the current node, move to the left subtree and compare again If the left subtree does not exist, then the word does not exist and it is inserted as a new node on the left Data Structures and Algorithms for Engineers 11 Carnegie Mellon University Africa

Binary Search Trees If, on the other hand, the word was greater than the key in the current node, move to the right subtree and compare again If the right subtree does not exist, then the word does not exist and it is inserted as a new node on the right This insertion can most easily be effected in a recursive manner Data Structures and Algorithms for Engineers 12 Carnegie Mellon University Africa

Binary Search Trees The point here is that the structure of the tree depends on the order in which the data is inserted in the list If the words are entered in sorted order, then the tree will degenerate to a 1-D list Data Structures and Algorithms for Engineers 13 Carnegie Mellon University Africa

BST Operations Insert: E x BST BST : The function value Insert(e,T) is the BST T with the element e inserted as a leaf node; if the element already exists, no action is taken NO WINDOW!!! Data Structures and Algorithms for Engineers 14 Carnegie Mellon University Africa

BST Operations Delete: E x BST BST : The function value Delete(e, T) is the BST T with the element e deleted; if the element is not in the BST exists, no action is taken. NO WINDOW!!! Data Structures and Algorithms for Engineers 15 Carnegie Mellon University Africa

Implementation of Insert(e, T) If T is empty (i.e. T is NULL) create a new node for e make T point to it If T is not empty if e < element at root of T Insert e in left child of T: Insert(e, T(1)) if e > element at root of T Insert e in right child of T: Insert(e, T(2)) Data Structures and Algorithms for Engineers 16 Carnegie Mellon University Africa

Implementation of Delete(e, T) First, we must locate the element e to be deleted in the tree if e is at a leaf node we can delete that node and be done if e is at an interior node at w we can t simply delete the node at w as that would disconnect its children if the node at w has only one child we can replace that node with its child Data Structures and Algorithms for Engineers 17 Carnegie Mellon University Africa

Implementation of Delete(e, T) if the node at w has two children we replace the node at w with the lowest-valued element among the descendents of its right child this is the left-most node of the right tree It is useful to have a function DeleteMin with removes the smallest element from a non-empty tree and returns the value of the element removed Data Structures and Algorithms for Engineers 18 Carnegie Mellon University Africa

Implementation of Delete(e, T) If T is not empty if e < element at root of T Delete e from left child of T: Delete(e, T(1)) if e > element at root of T Delete e from right child of T: Delete(e, T(2)) if e = element at root of T and both children are empty Remove T if e = element at root of T and left child is empty Replace T with T(2) Data Structures and Algorithms for Engineers 19 Carnegie Mellon University Africa

Implementation of Delete(e, T) if e = element at root of T and right child is empty Replace T with T(1) if e = element at root of T and neither child is empty Replace T with left-most node of T(2) Data Structures and Algorithms for Engineers 20 Carnegie Mellon University Africa

Implementation of Delete(e, T) Sun Mon Tue Fri Sat Thur Wed Data Structures and Algorithms for Engineers 21 Carnegie Mellon University Africa

Implementation of Delete(e, T) Mon Fri Sat Sun Tue Thur Wed Data Structures and Algorithms for Engineers 22 Carnegie Mellon University Africa

Tree Traversals To perform a traversal of a data structure, we use a method of visiting every node in some predetermined order Traversals can be used to test data structures for equality to display a data structure to construct a data structure of a give size to copy a data structure Data Structures and Algorithms for Engineers 23 Carnegie Mellon University Africa

Depth-First Traversals There are 3 depth-first traversals Inorder Postorder Preorder For example, consider the expression tree: Data Structures and Algorithms for Engineers 24 Carnegie Mellon University Africa

Example: Expression Tree + - + - C A B D E F G Data Structures and Algorithms for Engineers 25 Carnegie Mellon University Africa

Depth-First Traversals Inorder traversal A - B + C x D + E x F - G Postorder traversal A B - C + D E + F G - x x Preorder traversal x + -A B C x + D E - F G Data Structures and Algorithms for Engineers 26 Carnegie Mellon University Africa

Depth-First Traversals The parenthesised Inorder traversal ((A - B) + C) x ((D + E) x (F - G)) This is the infix expression corresponding to the expression tree Postorder traversal gives a postfix expression Preorder traversal gives a prefix expression Data Structures and Algorithms for Engineers 27 Carnegie Mellon University Africa

Depth-First Traversals Recursive definition of inorder traversal Given a binary tree T if T is empty visit the external node otherwise perform an inorder traversal of Left(T) visit the root of T perform an inorder traversal of Right(T) Data Structures and Algorithms for Engineers 28 Carnegie Mellon University Africa

Example: Inorder Traversal + - + - C A B D E F G Data Structures and Algorithms for Engineers 29 Carnegie Mellon University Africa

Example: Inorder Traversal + - + - C A B D E F G Data Structures and Algorithms for Engineers 30 Carnegie Mellon University Africa

Example: Inorder Traversal Sun Mon Tue Fri Sat Thur Wed Data Structures and Algorithms for Engineers 31 Carnegie Mellon University Africa

Example: Inorder Traversal Mon Fri Sat Sun Tue Thur Wed Data Structures and Algorithms for Engineers 32 Carnegie Mellon University Africa

Depth-First Traversals Recursive definition of postorder traversal Given a binary tree T if T is empty visit the external node otherwise perform an postorder traversal of Left(T) perform an postorder traversal of Right(T) visit the root of T Data Structures and Algorithms for Engineers 33 Carnegie Mellon University Africa

Example: Postorder Traversal + - + - C A B D E F G Data Structures and Algorithms for Engineers 34 Carnegie Mellon University Africa

Example: Postorder Traversal + - + - C A B D E F G Data Structures and Algorithms for Engineers 35 Carnegie Mellon University Africa

Depth-First Traversals Recursive definition of preorder traversal Given a binary tree T if T is empty visit the external node otherwise visit the root of T perform an preorder traversal of Left(T) perform an preorder traversal of Right(T) Data Structures and Algorithms for Engineers 36 Carnegie Mellon University Africa

Example: Preorder Traversal + - + - C A B D E F G Data Structures and Algorithms for Engineers 37 Carnegie Mellon University Africa

Example: Preorder Traversal + - + - C A B D E F G Data Structures and Algorithms for Engineers 38 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 39 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 40 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers 41 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 42 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 43 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 44 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 45 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers 46 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers 47 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers 48 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers 49 Carnegie Mellon University Africa

Data Structures and Algorithms for Engineers 50 Carnegie Mellon University Africa

BST Implementation Data Structures and Algorithms for Engineers 51 Carnegie Mellon University Africa

BINARY_TREE Implementation 3 Data Structures and Algorithms for Engineers 52 Carnegie Mellon University Africa

BINARY_TREE Implementation 3 1 Data Structures and Algorithms for Engineers 53 Carnegie Mellon University Africa

BINARY_TREE Implementation 3 1 5 Data Structures and Algorithms for Engineers 54 Carnegie Mellon University Africa

BINARY_TREE Implementation 3 1 5 2 Data Structures and Algorithms for Engineers 55 Carnegie Mellon University Africa

BINARY_TREE Implementation 3 1 5 2 4 Data Structures and Algorithms for Engineers 56 Carnegie Mellon University Africa

BINARY_TREE Implementation 3 1 5 2 4 6 Data Structures and Algorithms for Engineers 57 Carnegie Mellon University Africa

BINARY_TREE Implementation 4 1 5 2 6 Data Structures and Algorithms for Engineers 58 Carnegie Mellon University Africa