Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Binary Search Trees1 Part-F1 Binary Search Trees   
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Lecture 06: Tree Structures Topics: Trees in general Binary Search Trees Application: Huffman Coding Other types of Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
COSC2007 Data Structures II
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Chapter 8 Data Abstractions. © 2005 Pearson Addison-Wesley. All rights reserved 8-2 Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2 Implementing.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
CSC 213 Lecture 8: (2,4) Trees. Review of Last Lecture Binary Search Tree – plain and tall No balancing, no splaying, no speed AVL Tree – liberté, égalité,
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Trees Chapter 15.
Fundamentals of Programming II Introduction to Trees
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Tree.
Tree data structure.
i206: Lecture 13: Recursion, continued Trees
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Tree data structure.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear

John Chuang2 Outline  What is a data structure  Basic building blocks: arrays and linked lists  Data structures (uses, methods, performance): -List, stack, queue -Dictionary -Tree -Graph

John Chuang3 Tree Data Structure  Tree = a collection of data whose entries have a hierarchical organization  Example:  What are some examples of trees in computer systems? Brookshear Figure 8.1

John Chuang4 Tree Terminology  Node = an entry in a tree  Nodes are linked by edges  Root node = the node at the top  Terminal or leaf node = a node at the bottom  Parent of a node = the node immediately above the specified node  Child of a node = a node immediately below the specified node  Ancestor = parent, parent of parent, etc.  Descendent = child, child of child, etc.  Siblings = nodes sharing a common parent Brookshear Figure 8.2

John Chuang5 Tree Terminology  Depth of a node -The depth of a node v in T is the number of ancestors of v, excluding v itself. -More formally: -If v is the root, the depth of v is 0 -Else the depth of v is one plus the depth of the parent of v  Height (or depth) of a tree -The depth of a tree is the maximum depth of any of its leaves Depth(x) = 1 Depth(y) = 3 Depth(T) = 3 x y

John Chuang6 Types of Trees  General tree – a node can have any number of children  Binary tree – a node can have at most two children

John Chuang7 Implementing a Binary Tree  Linked list -Each node = data cell + two child pointers -Accessed through a pointer to root node Brookshear Figure 8.13

John Chuang8 Implementing a Binary Tree  Array -A[1] = root node -A[2],A[3] = children of A[1] -A[4],A[5],A[6],A[7] = children of A[2] and A[3] -… Brookshear Figure 8.14

John Chuang9 A sparse, unbalanced tree Brookshear Figure 8.15

John Chuang10 Tree Methods  search (or get) – Return a reference to a node in a tree  insert (or put) – Put a node into the tree; if the tree is empty the node becomes the root of the tree  delete (or remove) – Remove a node from the tree  traversal – access all nodes in the tree

John Chuang11 Application: Binary Search  Recall that binary search algorithm is O(log n)  One way of implementing the algorithm is to use a binary search tree (BST) data structure Brookshear Figure 8.17

John Chuang12 Binary Search Tree  Binary search tree (BST) data structure -The left subtree of a node contains only nodes with keys less than the node's key. -The right subtree of a node contains only nodes with keys greater than the node's key. -Both the left and right subtrees must also be binary search trees. Brookshear Figure 8.17

John Chuang13 Binary Tree Search Brookshear Figure 8.18 Example: Search (Tree, J) Brookshear Figure 8.19

John Chuang14 Node Insertion Brookshear Figure 8.23 Example: Insert (Tree, M) Brookshear Figure 8.22

John Chuang15 Node Deletion  When you delete a node that has children, you need to re-link the tree  Three deletion cases: -Delete node with no children -Delete node with one child (which may be a subtree) -Delete node with more than one child  Last case is a challenge with ordered trees -You want to preserve the ordering

John Chuang16 Delete Nodes – No children delete parent delete parent parent.left = null;

John Chuang17 Delete Nodes – One child parent.left = delete.right; delete parent delete parent

John Chuang18 Deleting Nodes – Multiple children  Assuming you want to preserve the sort order, deletion is not straight forward  Replace the deleted node with a node that preserves the sort order – the in-order successor node for in-order trees  The successor node will be in the right subtree, left most node (or left most parent node if there is no left most node) delete successor parent successor parent

John Chuang19 Tree Traversal  A systematic way of accessing (or visiting) all the nodes of a tree  Example: printing binary search tree in alphabetical order using “in-order” tree traversal Brookshear Figure 8.20 Brookshear Figure 8.21

John Chuang20 Tree Traversal  Different types of traversal In-order Pre-orderPost-order level numbering

John Chuang21 Efficiency of Binary Search Trees  Average Case -Search: O(logN) -Insertion: O(logN) -Deletion: O(logN) -Traversal: O(N)  Worst Case -Search: O(N) -Insertion: O(N) -Deletion: O(N) -Traversal: O(N)  AVL trees and Red-Black trees  Self-balancing trees with tree height O(logN)  Worst case O(logN) searches, insertions, and deletions  Many other variations: Splay trees, B-trees, etc.  Search, insert, and delete operations: O(height of tree)