Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Binary Trees Binary Trees Binary Search Trees Binary Search Trees CSE Lecture 13 –Trees.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
Trees Briana B. Morrison Adapted from Alan Eugenio.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
Lecture 9 Binary Trees Trees General Definition Terminology
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
Binary Search Trees Chapter 7 Objectives
Binary Search Tree (BST)
Tree.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Data Structures Binary Trees.
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 10: Binary Trees.
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and Path Len. (5 slides) (5 slides) Binary Tree Definition Binary Tree Definition Selected Samples / Binary Trees Selected Samples / Binary Trees Binary Tree Nodes Binary Tree Nodes Binary Search Trees Binary Search Trees Locating Data in a Tree Locating Data in a Tree Removing a Binary Tree Node Removing a Binary Tree Node stree ADT (4 slides) stree ADT (4 slides) Using Binary Search Trees Using Binary Search Trees - Removing Duplicates Update OperationsUpdate Operations (3 slides) Update Operations Removing an Item From a Binary TreeRemoving an Item From a Binary Tree (7 slides) Removing an Item From a Binary Tree Chapter 10 – Binary Trees Summary SlidesSummary Slides (5 slides) Summary Slides

Main Index Contents 22 Main Index Contents Tree Structures

Main Index Contents 33 Main Index Contents Tree Structures

Main Index Contents 44 Main Index Contents Tree Structures

Main Index Contents 55 Main Index Contents Tree Node Level and Path Length

Main Index Contents 66 Main Index Contents Tree Node Level and Path Length – Depth Discussion

Main Index Contents 77 Main Index Contents Tree Node Level and Path Length – Depth Discussion

Main Index Contents 88 Main Index Contents Tree Node Level and Path Length – Depth Discussion

Main Index Contents 99 Main Index Contents Tree Node Level and Path Length – Depth Discussion

Main Index Contents 10 Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: – (a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) – (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree T L and the right subtreeT R. The nodes in T consist of node R and all the nodes in T L and T R.

Main Index Contents 11 Main Index Contents Selected Samples of Binary Trees

Main Index Contents 12 Main Index Contents Binary Tree Nodes

Main Index Contents 13 Main Index Contents Binary Search Trees

Main Index Contents 14 Main Index Contents - CurrentNodeAction -LOCATING DATA IN A TREE- Root = 50Compare item = 37 and < 50, move to the left subtree Node = 30Compare item = 37 and > 30, move to the right subtree Node = 35Compare item = 37 and > 35, move to the right subtree Node = 37Compare item = 37 and 37. Item found.

Main Index Contents 15 Main Index Contents Removing a Binary Search Tree Node

Main Index Contents 16 Main Index Contents CLASS stree Constructors “d_stree.h” stree(); Create an empty search tree. stree(T *first, T *last); Create a search tree with the elements from the pointer range [first, last). CLASS stree Opertions “d_stree.h” void displayTree(int maxCharacters); Display the search tree. The maximum number of characters needed to output a node value is maxCharacters. bool empty(); Return true if the tree is empty and false otherwise.

Main Index Contents 17 Main Index Contents CLASS stree Opertions “d_stree.h” int erase(const T& item); Search the tree and remove item, if it is present; otherwise, take no action. Return the number of elements removed. Postcondition: If item is located in the tree, the size of the tree decreases by 1. void erase(iterator pos); Erase the item pointed to the iterator pos. Precondition:The tree is not empty and pos points to an item in the tree. If the iterator is invalid, the function throws the referenceError exception. Postcondition: The tree size decreases by 1.

Main Index Contents 18 Main Index Contents CLASS stree Opertions “d_stree.h” void erase(iterator first, iterator last); Remove all items in the iterator range [first, last). Precondition: The tree is not empty. If empty, the function throws the underflowError exception. Postcondition: The size of the tree decreases by the number of items in the range. iterator find(const T& item); Search the tree by comparing item with the data values in a path of nodes from the root of the tree. If a match occurs, return an iterator pointing to the matching value in the tree. If item is not in the tree, return the iterator value end().

Main Index Contents 19 Main Index Contents CLASS stree Opertions “d_stree.h” Piar insert(const T& item); If item is not in the tree, insert it and return an iterator- bool pair where the iterator is the location of the newelement and the Boolean value is true. If item is already in the tree, return the pair where the iterator locates the existing item and the Boolean value is false. Postcondition: The size of the tree is increased by 1 if item is not present in the tree. int size(); Return the number of elements in the tree.

Main Index Contents 20 Main Index Contents Using Binary Search Trees Application: Removing Duplicates

Main Index Contents 21 Main Index Contents Update Operations: 1 st of 3 steps 1)-The function begins at the root node and compares item 32 with the root value 25. Since 32 > 25, we traverse the right subtree and look at node 35.

Main Index Contents 22 Main Index Contents Update Operations: 2 nd of 3 steps 2)-Considering 35 to be the root of its own subtree, we compare item 32 with 35 and traverse the left subtree of 35.

Main Index Contents 23 Main Index Contents Update Operations: 3 rd of 3 steps 1)-Create a leaf node with data value 32. Insert the new node as the left child of node 35. newNode = getSTNode(item,NULL,NULL,parent); parent->left = newNode;

Main Index Contents 24 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 25 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 26 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 27 Delete node 25 with two children Node R is the smallest node to the right 27 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 28 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 29 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 30 Main Index Contents Removing an Item From a Binary Tree

Main Index Contents 31 Main Index Contents Summary Slide 1 §- trees -hierarchical structures that place elements in nodes along branches that originate from a root. -Nodes in a tree are subdivided into levels in which the topmost level holds the root node. §-Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure. -Tree terminology with which you should be familiar: parent | child | descendents | leaf node | interior node | subtree.

Main Index Contents 32 Main Index Contents Summary Slide 2 §- Binary Trees -Most effective as a storage structure if it has high density §-ie: data are located on relatively short paths from the root. §-A complete binary tree has the highest possible density -an n-node complete binary tree has depth int(log2n). -At the other extreme, a degenerate binary tree is equivalent to a linked list and exhibits O(n) access times.

Main Index Contents 33 Main Index Contents Summary Slide 3 §- Traversing Through a Tree -There are six simple recursive algorithms for tree traversal. -The most commonly used ones are: 1)inorder (LNR) 2)postorder (LRN) 3)preorder (NLR). -Another technique is to move left to right from level to level. §-This algorithm is iterative, and its implementation involves using a queue.

Main Index Contents 34 Main Index Contents Summary Slide 4 §- A binary search tree stores data by value instead of position -It is an example of an associative container. §-The simple rules “== return” “< go left” “> go right” until finding a NULL subtree make it easy to build a binary search tree that does not allow duplicate values.

Main Index Contents 35 Main Index Contents Summary Slide 5 §- The insertion algorithm can be used to define the path to locate a data value in the tree. §- The removal of an item from a binary search tree is more difficult and involves finding a replacement node among the remaining values.