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

Similar documents
Binary Trees, Binary Search Trees

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

Trees, Binary Trees, and Binary Search Trees

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

TREES. Trees - Introduction

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

CE 221 Data Structures and Algorithms

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

Binary Trees

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

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

Why Do We Need Trees?

CSCI2100B Data Structures Trees

Binary Search Trees. See Section 11.1 of the text.

CS350: Data Structures Red-Black Trees

Programming II (CS300)

Trees. Truong Tuan Anh CSE-HCMUT

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

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

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

Algorithms. Deleting from Red-Black Trees B-Trees

Trees. Trees. CSE 2011 Winter 2007

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

Data Structures and Algorithms for Engineers

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

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

Analysis of Algorithms

March 20/2003 Jayakanth Srinivasan,

Trees. CSE 373 Data Structures

Binary Trees. Height 1

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

Trees. R. J. Renka 10/14/2011. Department of Computer Science & Engineering University of North Texas. R. J. Renka Trees

Chapter 20: Binary Trees

Lecture 6: Analysis of Algorithms (CS )

CSI33 Data Structures

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

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

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

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

CS350: Data Structures Binary Search Trees

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

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

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

INF2220: algorithms and data structures Series 1

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

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

Cpt S 122 Data Structures. Data Structures Trees

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

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

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

CS 350 : Data Structures Binary Search Trees

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

Associate Professor Dr. Raed Ibraheem Hamed

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

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

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

Algorithms. AVL Tree

Friday Four Square! 4:15PM, Outside Gates

IX. Binary Trees (Chapter 10)

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

Binary Search Trees. Analysis of Algorithms

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Garbage Collection: recycling unused memory

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Fall, 2015 Prof. Jungkeun Park

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

Data Structure - Binary Tree 1 -

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

Advanced Tree Data Structures

Algorithms and Data Structures

Data Structures and Algorithms

Lecture: Analysis of Algorithms (CS )

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

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

CS102 Binary Search Trees

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

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

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


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

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

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 Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

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

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

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

BBM 201 Data structures

Trees, Part 1: Unbalanced Trees

CS24 Week 8 Lecture 1

ROOT: A node which doesn't have a parent. In the above tree. The Root is A.

Chapter 5. Binary Trees

Binary Trees. Examples:

EE 368. Weeks 5 (Notes)

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

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

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

CS302 - Data Structures using C++

CMPE 160: Introduction to Object Oriented Programming

Transcription:

Trees (Trees) Data Structures and Programming Spring 2018 1 / 28

Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T 1, T 2,, T k, each of whose roots are connected by a directed edge from r (Trees) Data Structures and Programming Spring 2018 2 / 28

Some Terminologies Siblings: nodes share the same parent Internal node: node with at least one child (A, B, C, F) External node (leaf): node without children (E, I, J, K, G, H, D) Ancestor of a node: itself, parent, grandparent, etc. Descendant of a node: itself, child, grandchild, etc. Depth of a node: length of the unique path (i.e., number of edges) from the root to that node The depth of a tree is equal to the depth of the deepest leaf Height of a node: length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree is equal to the height of the root Subtree: tree consisting of a node and its descendants (Trees) Data Structures and Programming Spring 2018 3 / 28

Example: UNIX Directory (Trees) Data Structures and Programming Spring 2018 4 / 28

Binary Trees A tree in which no node can have more than two children The depth of an average binary tree is considerably smaller than N, even though in the worst case, the depth can be as large as N - 1. (Trees) Data Structures and Programming Spring 2018 5 / 28

Differences Between A Tree and A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered Are different when viewed as binary trees. Are the same when viewed as trees. (Trees) Data Structures and Programming Spring 2018 6 / 28

Example: Expression Trees Leaves are operands (constants or variables) The other nodes (internal nodes) contain operators Will not be a binary tree if some operators are not binary (Trees) Data Structures and Programming Spring 2018 7 / 28

Pointer Representation of a Tree Node (Trees) Data Structures and Programming Spring 2018 8 / 28

Left Child, Right Sibling Representation (Trees) Data Structures and Programming Spring 2018 9 / 28

Example (Trees) Data Structures and Programming Spring 2018 10 / 28

Tree Traversal - Preorder visit the root traverse in preorder the children (subtrees) (Trees) Data Structures and Programming Spring 2018 11 / 28

Tree Traversal - Postorder traverse in postorder the children (subtrees) visit the root (Trees) Data Structures and Programming Spring 2018 12 / 28

Tree Traversal - Inorder (Binary Tree) traverse in inorder the left subtree visit the root traverse in inorder the right subtree (Trees) Data Structures and Programming Spring 2018 13 / 28

Inorder Traversal of an Expression Tree Infix expression a + b c + d e + f g (Trees) Data Structures and Programming Spring 2018 14 / 28

Full and Complete Binary Trees A full binary tree of a given height k has 2 k+1 1 nodes. Parent of node i is node i/2, unless i = 1. Right child of node i is node 2i + 1, unless 2i + 1 > n, where n is the number of nodes. Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. (Trees) Data Structures and Programming Spring 2018 15 / 28

Representing Binary Trees Using Arrays Waste spaces: in the worst case, a skewed tree of depth k requires 2 k 1 spaces, of these, only k spaces will be occupied. Good if the tree is almost full (complete). (Trees) Data Structures and Programming Spring 2018 16 / 28

Threaded Binary Trees Using left/right pointers in a node, too many null pointers in current representation of binary trees Rules for constructing the threads If ptr left child is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal If ptr right child is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal (Trees) Data Structures and Programming Spring 2018 17 / 28

Threaded Binary Trees (Cont d) Inorder traversal of a threaded binary tree By using of threads we can perform an inorder traversal without making use of a stack (simplifying the task) Now, we can follow the thread of any node, ptr, to the next node of inorder traversal 1 If ptr right thread = TRUE, the inorder successor of ptr is ptr right child by definition of the threads 2 Otherwise we obtain the inorder successor of ptr by following a path of left-child links from the right-child of ptr until we reach a node with left thread = TRUE (Trees) Data Structures and Programming Spring 2018 18 / 28

Binary Search Trees Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently. Binary search tree property For every node X, all the keys in its left subtree are smaller than the key value in X, and all the keys in its right subtree are larger than the key value in X Average depth of a node is O(logN); maximum depth of a node is O(N) (Trees) Data Structures and Programming Spring 2018 19 / 28

Binary Search Algorithm Binary Search algorithm of an array of sorted items reduces the search space by one half after each comparison (Trees) Data Structures and Programming Spring 2018 20 / 28

Searching BST If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree. (Trees) Data Structures and Programming Spring 2018 21 / 28

Example (Trees) Data Structures and Programming Spring 2018 22 / 28

findmin/ findmax Return the node containing the smallest element in the tree Start at the root and go left as long as there is a left child. The stopping point is the smallest element Similarly for findmax Time complexity = O(height of the tree) (Trees) Data Structures and Programming Spring 2018 23 / 28

Insert Proceed down the tree as you would with a find If X is found, do nothing (or update something) Otherwise, insert X at the last spot on the path traversed Time complexity = O(height of the tree) (Trees) Data Structures and Programming Spring 2018 24 / 28

Delete the node is a leaf Delete it immediately the node has one child Adjust a pointer from the parent to bypass that node (Trees) Data Structures and Programming Spring 2018 25 / 28

Delete (Cont d) the node has 2 children replace the key of that node with the minimum element at the right subtree delete the minimum element Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. Time complexity = O(height of the tree) (Trees) Data Structures and Programming Spring 2018 26 / 28

Worst Case BST tree = new BST(); for (int i = 1; i <= 8; i++) tree.insert (i); (Trees) Data Structures and Programming Spring 2018 27 / 28

Better Search Trees Prevent the degeneration of the BST : A BST can be set up to maintain balance during updating operations (insertions and removals) Types of ST which maintain the optimal performance: AVL trees splay trees 2-3 (2-3-4) Trees Red-Black trees B-trees (Trees) Data Structures and Programming Spring 2018 28 / 28