1 10. Binary Trees Read Sec. 10.1-10.4 A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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
Trees Chapter 8.
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,
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.
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.
Gordon College Prof. Brinton
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
10. Binary Trees A. Introduction: Searching a linked list.
Binary Search Trees Chapter 6.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Searching. Motivation Find parts for a system Find an address for name Find a criminal –fingerprint/DNA match Locate all employees in a dept. based on.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Trees Chapter.
Starting at Binary Trees
Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way.
Search: Binary Search Trees Dr. Yingwu Zhu. Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n Assume == and.
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.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Trees Chapter 15.
Searching and Binary Search Trees
Binary Search Tree (BST)
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Searching: Binary Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Heaps and priority queues
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set loc = 0; 2. Repeat the following: a. If loc >= length of list Return –1 to indicate item not found. b. If list element at location loc is item Return loc as location of item c. Increment loc by 1. 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() algorithm in STL.) For a list of length n, its average search time will be ________.

2 2. Binary Search Ordered lists can be searched more efficiently using binary search: /* Binary search an ordered list for a particular item */ 1. Set start = 0 and last = length of list – Repeat the following: a. If start > last Return –1 to indicate item not found. b. Find the middle element in the sublist from locations start through last and its location mid. c. If item < the list element at mid Set last = mid – 1. // Search first half of list Else if item > the list element at Loc Set start = mid + 1. // Search last half of list Else Return mid as location of item

3 Since the size of the list being searched is reduced by approximately 1/2 on each pass through the loop, the number of times the loop will be executed is _____________ It would seem therefore that binary search is much more efficient than linear search. This is true for lists stored in arrays in which step 2b can be done simply by calculating mid = (start + last ) / 2 and array[mid] is the middle list element. (It's the method used in the binary_search() algorithm in STL.)

4 As we have seen, for linked lists, binary search is not practical, because we only have direct access to the first node, and locating any other node requires traversing the list until that node is located. Thus step 2b requires: i. mid = (start + last) / 2 ii. Set locPtr = first; // location of first node iii. For loc = start to mid - 1 Set locPtr = next part of node pointed to by locPtr. iv. locPtr points to the middle node and the data part of the node pointed to locPtr is the middle list element. Locating the middle node clearly negates the efficiency of binary search for array-based lists; the computing time becomes O(n) instead of O(log 2 n). However, perhaps we could modify the linked structure to make a binary search feasible. What would we need?

5 Direct access to the middle node: and from it to the middle of the first half and to the middle of the second half: and so on:

6 Or if we stretch out the links to give it a _________-like shape That is, use a ________________________________ /

7 B. Binary Search Trees 1.Definition and Terminology: Tree: A finite set of elements called nodes (or vertices) and a finite set of directed arcs that connect pairs of nodes. If the tree is not empty, one of the nodes, called the root, has no incoming arcs, but every other node in the tree can be reached from the root by a unique path (a sequence of consecutive arcs). Leaf:Node with no outgoing arcs Nodes directly accessible (usingone arc) from a node are called the children of that node, which is called the parent of these children; these nodes are siblings of each other.

________ ________ of this ________ ________ of each other ________

9 Morse code trees 2. Examples Game trees

10 Parse trees

11 3. Def: A binary tree is a tree in which ______________________ _______________________________ 4. Array-Based Implementation: An array can be used to store some binary trees. Just number the nodes level by level, from left to right, store node #0 in array location 0, node #1 in array location 1, etc. i... t [i ]... CEPU TM O

12 But, unless each level of the tree is full so there are no "dangling limbs," there can be much wasted space in the array. For example, this binary tree contains the same characters as before but require _____ array positions for storage: Max # nodes on level i: ________ In array representation, children of i are at: ________ Parent of i is at: ________

13 5. Linked Implementation: Use nodes of the form and maintain a pointer to the root. a. Example: BST b; b operations

14 b. C++ Implementation: template class BinaryTree { public: //... BinaryTree function members private: class BinNode // a binary tree node { public: //... BinNode member functions }; typedef BinNode * BinNodePointer; // BinaryTree data members BinNodePointer root; // pointer to root node };

15 5. A Binary Search Tree (BST) is a binary tree in which the value in each node is __________________________________ ___________________________________________________. a. We can "binary search" a BST: 1. Set pointer locPtr = root. 2. Repeat the following: If locPtr is null _____________________ If value data _____________________ Else if value > locPtr->data _____________________ Else _____________________ Search time: ____________________________

16 b. What about traversing a binary tree? Most easily done recursively, viewing a binary tree as a recursive data structure: Recursive definition of a binary tree: A binary tree either: i. is empty  Anchor or ii. consists of a node called the root, which has pointers to two disjoint binary subtrees  Inductive step called the left subtree and the right subtree.

17 Now, for traversal, consider the three operations: V: Visit a node L: (Recursively) traverse the left subtree of a node R: (Recursively) traverse the right subtree of a node We can do these in six different orders: LVR VLR LRV VRL RVL RLV

18 For example, LVR gives the following traversal algorithm: If the binary tree is empty// anchor Do nothing. Else do the following:// inductive step L: Call traversal to traverse the left subtree. V: Visit the root. R: Call traversal to traverse the right subtree. As a member function in a BinaryTree class: Rearranging the steps L, V, and R gives the other traversals.

19 LVR: _____________________ VLR: _____________________ LRV: _____________________ The first three orders, in which the left subtree is traversed before the right, are the most important of the six traversals and are commonly called by other names: LVR  _______________ VLR  _______________ LRV  _______________ Note: Inorder traversal of a BST visits the nodes __________________________.

20 To see why these names are appropriate, recall expression trees, binary trees used to represent the arithmetic expressions like a + b * c / d : Inorder traversal  infix expression: ______________________ Preorder traversal  prefix expression: ____________________ Postorder traversal  postfix expression: ___________________

21 c.So how do we insert in a binary tree so it grows into a BST? Modify the search algorithm so that a pointer parentPtr trails locPtr down the tree, keeping track of the parent of each node being checked: 1. Initialize pointers locPtr = root, parentPtr = null pointer. 2.While locPtr ≠ null pointer: a. parentPtr = locPtr b. If value Data locPtr = locPtr->Left Else if value > locPtr->Data locPtr = locPtr->Right Else value is already in the tree; return a found indicator 3. Get a new node pointed to by newPtr, put the value in its data part, and set left and right to null. 4. If parentPtr = null pointer// empty tree Set root = newptr. Else if value data Set parentPtr->left = newPtr. Else Set parentPtr->right = newPtr.

22 Examples: Insert in the order given: Insert in the order given: Insert in the order given: M, O, T, H, E, RT, H, E, R, M, OE, H, M, O, R, T

23 d.What about deleting a node a BST? Case 1: A leaf, andCase 2: 1 child Easy — just reset link from parent Case 3: 2 children: 1. Replace node with inorder successor X. 2. Delete X (which has 0 or 1 child)

24 Some Special Kinds of Trees: Threaded Binary Search Trees (§13.1) AVL Trees (§13.2) Trees (§13.3) B-Trees (§13.3) Red-Black Trees (map, set, multimap, multiset in STL) (§13.3) Tries Huffman Code Trees (data compression) (§10.5)