Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
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.
Ceng-112 Data Structures I Chapter 8 Search Trees.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
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.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Copyright©1999 Angus Wu PROGRAMMING METHDOLOGY AND SOFTWARE ENGINEERING EE PROGRAMMING METHODOLOGY AND SOFTWARE ENGINEERING.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
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.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
AA Trees.
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
Binary Search Trees Chapter 7 Objectives
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Chapter 20: Binary Trees.
Chapter 7 TREES.
Chapter 21: Binary Trees.
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures( 数据结构 ) Course 8: Search Trees

2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem: Store ordered data in an array structure : efficient search algorithm, inefficient insertion and deletion Linked list: efficient insertion and deletion,inefficient search Binary search trees can satisfy both of them Definition: the properties of a binary search tree 1.All items in the left subtree are less than the root 2.All items in the right subtree are greater than or equal to the root 3.Each subtree is itself a binary search tree. (a) (d)(c) (b) (e) Figure 8-2 binary search trees (a) (d)(c) (b) Figure 8-3 invalid binary search trees

3 西南财经大学天府学院 binary search tree traversals Algorithms are identical to the ones in chapter 7 Operations on binary search trees binary search tree search algorithms Find smallest node Find largest node Find requested node Follow the left branches until we get to a leaf Algorithm findsmallestBST( val root ) To find the smallest node in a BST Pre root is a pointer to a nonempty BST or subtree Return address of smallest node 1 if (root->left null) 1 return(root) 2 endif 3 return findsmallestBST (root->left) end findsmallestBST Follow the right branches to the last node in the tree Algorithm findlargestBST( val root ) To find the largest node in a BST Pre root is a pointer to a nonempty BST or subtree Return address of largest node 1 if (root->right null) 1 return(root) 2 endif 3 return findlargestBST (root->right) end findlargestBST Assume: To find T, Comparing T with root, T root go right Algorithm searchBST(val root, val argument ) Search a binary search tree for a given value Pre root is the root to a binary tree or subtree argument is the key value requested Return the node address if the value is found Null if the node is not in the tree 1 if (root is null) 1 return null 2 end if 3 if (argument key ) 1 return searchBST (root->left, argument) 4 else if (argument > root->key ) 1 return searchBST (root->right, argument) 5 else 1 return root 6 end if end searchBST Figure 8-4 a binary search trees 5235 Preorder traversal: Postorder traversal: Inorder traversal :

4 西南财经大学天府学院 Insert node Steps: 1. Follow the branches to an empty subtree 2. Insert the new node All inserts take place at a leaf or a leaflike node that has only one null branch After inserting After inserting After inserting Note: node with equal values are found in the right subtree

5 西南财经大学天府学院 Iterative insert (algorithm) 1 if (root is null) 1 root = new 2 else 1 pwalk = root 2 loop ( pwalk not null ) 1 parent = pwalk 2 if ( new->key key ) 1 pwalk = pwalk->left 3 else 1 pwalk = pwalk->right 4 end if 3 end loop Location for new node found 4 if (new->key key ) 1 parent->left = new 5 else 1 parent->right = new 6 end if 3 end if 4 return end insertBST Algorithm insertBST ( ref root, val new ) Insert node containing new data into BST using iteration Pre root is address of first node in a BST new is address of node containing data to be inserted Post new node inserted into the tree Algorithm 8-4 iterative binary search tree insert new parent Final position when pwalk null null pwalk

6 西南财经大学天府学院 Recursive insert node(algorithm) Algorithm addBST ( ref root, val new ) Insert node containing new data into BST using recursion Pre root is address of current node in a BST new is address of node containing data to be inserted Post new node inserted into the tree 1 if (root is null) 1 root = new 2 else locate null subtree for insertion 1 if ( new->key key ) 1 addBST (root->left, new ) 2 else 1 addBST (root->right, new ) 3 end if 4 return end insertBST algorithm 8-5 add node to BST recursively

7 西南财经大学天府学院 Delete node 1.locate it 2.delete: there are 4 possible cases The node has no children: set the delete node’s parent to null, recycle its memory, and return Has only a right subtree: attach the right subtree to the delete node’s parent and recycle its memory. Has only a left subtree: attach the left subtree to the delete node’s parent and recycle its memory. Has two subtree: to delete a node from the middle of a tree, to keep the balance, we must find a node,…, two ways: Find the largest node in the deleted node’s left subtree, move its data to replace the deleted node’s data Find the smallest node in the deleted node’s right subtree, move its data to replace the deleted node’s data Regardless of which logic we use, we will be moving data from a leaf or leaflike node that can be deleted.

8 西南财经大学天府学院 dltptr (a) Delete node (44) that has no children before after

9 西南财经大学天府学院 1 if (root is null) 1 return false 2 endif 3 if(dltkey data.key ) 1 return deleteBST (root->left,dltkey) 4 elseif (dltkey > root->data.key ) 1 return deleteBST (root->right,dltkey) 5 else //delete node found—test for leaf node 1 if (root->left null) 1 dltptr = root 2 root = root->right 3 recycle ( dltptr ) 4 return true 2 elseif ( root->right null ) 1 dltptr = root 2 root = root->left 3 recycle ( dltptr ) 4 return true 3 else // node to be deleted not a leaf (or leaflike), find largest node on left subtree 1 dltptr = root->left 2 loop ( dltptr->right not null ) 1 dltptr = dltptr->right // node found, move data and delete leaf node 3 root->data = dltptr->data 4 return deleteBST (root->left,dltptr->data.key) 4 end if 6 endif end deleteBST Algorithm deleteBST ( ref root, val dltKey ) Delete a node from a BST Pre root is pointer to tree containing data to be deleted dltkey is key of node to be deleted Post node deleted and memory recycled, if dltkey not found, root unchanged Return true if node deleted, false if not found Base case 1 Node not find Base case 2 After node deleted

10 西南财经大学天府学院 Summary A binary search tree is a binary tree with the following properties: All items in the left subtree are less than the root. All items in the right subtree are greater than or equal to the root. Each subtree is itself a binary search tree. The inorder traversal of the binary search tree produces an ordered list. In a binary search tree, the node with the larges value is the rightmost node. To find the largest node, we follow the right branches until we get to a null right pointer. To search for a value in a binary search tree,we follow the left or right branch down the tree,depending on the value of the new node, until we find a null subtree.

11 西南财经大学天府学院 Summary To delete a node from a subtree, we must consider four cases: The node to be deleted has no children. The node to be deleted has only a left subtree. The node to be deleted has only a right subtree. The node to be deleted has two subtrees.

12 西南财经大学天府学院 Exercises Create a binary search tree using the following data entered as sequence set : 7,10,14,23,33,56,66,70,80 Insert 44 and 50 into the tree created in above exercise Create a binary search tree using the following data entered as sequence set :70,60,80,50,65,75,85,45,55,90 Delete node contain 60 and node contain 85 from the binary search tree above

13 西南财经大学天府学院 Team work Create a binary search tree using the following data entered as sequence set :70,60,80,50,65,75,85,45,55,90, output the ordered list Insert 34 and 79 into the tree created in above exercise and Delete node contain 60 and node contain 85 from the binary search tree above