Ceng-112 Data Structures I 2006 1 Chapter 8 Search Trees.

Slides:



Advertisements
Similar presentations
Ceng-112 Data Structures I Chapter 5 Queues.
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
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 ),
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Ceng-112 Data Structures I Figure 9-1 The heap is guaranteed to hold the largest node of the tree in the root. The smaller nodes of a heap can be.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
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.
Ceng-112 Data Structures ISerap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists.
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.
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.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
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.
Binary Search Trees Chapter 7 Objectives
Chapter 12 – Data Structures
AA Trees.
Recursive Objects (Part 4)
Binary Search Trees Chapter 7 Objectives
UNIT III TREES.
Binary Search Tree (BST)
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 7 TREES.
Chapter 21: Binary Trees.
AVL Trees CENG 213 Data Structures.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
CS202 - Fundamental Structures of Computer Science II
Chapter 20: Binary Trees.
AVL Tree Chapter 6 (cont’).
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Ceng-112 Data Structures I Chapter 8 Search Trees

Ceng-112 Data Structures I Binary Search Trees The binary search is a very efficient search algorithm, without insertion and deletion, when we store ordered data in the array structure. When we use linked list, in that case we have to use sequential search as an inefficient search algorithm. Binary search trees provide a suitable structure.

Ceng-112 Data Structures I Figure 8-1 Binary Search Trees Definition 1.All terms in the left subtree are less than the root. 2.All terms in the right subtree are greater than or equal to the root. 3.Each subtree is itself a binary search tree. < K

Ceng-112 Data Structures I Figure 8-2 Binary Search Trees

Ceng-112 Data Structures I Figure 8-3 Binary Search Trees

Ceng-112 Data Structures I Figure 8-4 Binary Search Trees Preorder traversal? Postorder traversal? Inorder traversal?

Ceng-112 Data Structures I Binary Search Trees Binary Search Trees Find the smallest value algorithm findsmallestBST(val root ) This algorithm finds the smallest node in a BST. PRE root is a pointer to a non-empty BST. Return address of smallest node 1. if (root->left null) 1. return (root) 2. return findsmallestBST(root->left) end findsmallestBST

Ceng-112 Data Structures I Figure 8-5 Binary Search Trees Find the smallest node in BST.

Ceng-112 Data Structures I Binary Search Trees Binary Search Trees Find the largest value algorithm findlargestBST(val root ) This algorithm finds the larges node in a BST. PRE root is a pointer to a non-empty BST. Return address of largest node 1. if (root->right null) 1. return (root) 2. return findlargestBST(root->right) end findlargestBST

Ceng-112 Data Structures I Figure 8-6 Binary Search Tree Search Finding a specific node in the tree!

Ceng-112 Data Structures I Figure 8-7 We are looking for node 20.

Ceng-112 Data Structures I Binary Search Trees Binary Search Trees Find the specific value 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 If (argument key) 1 return searchBST(root->left, argument) 3 else If (argument > root->key) 1 return searchBST(root->right, argument) 4 else 1 return root end searchBST

Ceng-112 Data Structures I Binary Search Tree – Insert Node

Ceng-112 Data Structures I Binary Search Tree – Insert Node algorithm insertBST(ref root, val new ) Insert node containing new node into BST using iteration PRE root is address of the first node in a BST, new is address of node containing data to be inserted. POST new node inserted into tree.

Ceng-112 Data Structures I Binary Search Tree – Insert Node 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 pwalk=pwalk->right Location for new node found 3 If (new->key key) 1 parent->left=new 4 else parent->right=new 3 return end insertBST

Ceng-112 Data Structures I Binary Search Tree – Delete Node

Ceng-112 Data Structures I Binary Search Tree – Delete Node

Ceng-112 Data Structures I Binary Search Tree – Delete Node algorithm deleteBST(ref root, val dltkey ) This algorithm deletes a node from BST. Pre root is pointer to tree containing data to be deleted, dltkey is key of node to be deleted. Post node deleted & memory rcycled, if dltkey not found, root unchanged. Return true if node deleted, false if not found.

Ceng-112 Data Structures I Binary Search Tree – Delete Node 1 If (root null) 1 return false 2 If (dltkey key) return deleteBST(root->left, dltkey) 3 else If (dltkey > root->key) return deleteBST(root->right, dltkey) 4 else /*(Delete node found --- Test for leaf node)*/ 1 If (root ->left null) 1 dltprt=root, root =root->right, recycle(dltprt), return true 2 else If (root->right null) 1 dltprt=root, root =root->left, recycle(dltprt), return true 3 else /*Node is not a leaf, find largest node on left subtree*/ 1 dltprt = root->left 2 loop (dltprt->right not null) 1 dltprt=dltprt->right 3 root->data =dltprt->data 4 return deleteBST(root->left, dltprt->data.key) end deleteBST

Ceng-112 Data Structures I HW-8 1.Create a BST with positive integer numbers which are taken from the screen. 2.Write the BST delete function which establishes to delete desired node from the BST. 3.Write the BST list function which lists the nodes in the BST with inorder traversal. 4.Collect all above functions under a user menu. Load your HW-8 to FTP site until 14 May. 07 at 09:00 am.

Ceng-112 Data Structures I AVL Trees In 1962, two Russian mathematicians, G. M. Adelson-Velskil and E. M. Landis created balanced binary tree structure that is named after them – the AVL trees. |H L -H R | < = 1 Height balanced trees. O(n) O(log 2 n)

Ceng-112 Data Structures I

Ceng-112 Data Structures I Figure 8-14 (a and b) When we insert a node into a tree or delete a node from a tree, the resulting tree may be unbalanced and we must rebalance it. AVL Trees – Balancing

Ceng-112 Data Structures I All unbalanced trees fall into one of these four cases: 1.Left to left 2.Right to right 3.Right of left 4.Left of right

Ceng-112 Data Structures I Figure 8-15 LEFT-OF-LEFT We must balance the left-height tree by rotating the out-of-balance node to the right.

Ceng-112 Data Structures I Figure 8-16 RIGHT-OF-RIGHT We must balance the right-height tree by rotating the out-of-balance node to the left.

Ceng-112 Data Structures I Figure 8-17 RIGHT-OF-LEFT 1.Rotate left 2.Rotate right

Ceng-112 Data Structures I Figure 8-18 LEFT-OF-RIGHT 1.Rotate right 2.Rotate left

Ceng-112 Data Structures I AVL Node Structure Node key data leftsubtree rightsubtree bal End Node

Ceng-112 Data Structures I AVL Rotate Algorithm algorithm rotateRight( ref root ) This algorithm exchanges pointers to rotate the tree right. PRE root points to tree to be rotated. POST Node rotated and root updated. 1 tempPtr = root->left 2 root->left = tempPtr->right 3 tempPtr->right = root 4 root = tempPtr 5 return end rotateRight

Ceng-112 Data Structures I AVL Insert The search and retrieval algorithms are the same as for any binary tree. Inorder travelsal is used because AVL trees are search trees. As a binary search tree, we have to find suitable leaf node on left or right subtree, then we connect new node to this parent node and begin back out of tree. As we back out of tree we check the balance of each node. If we find unbalanced node we balance it and continue up the tree. Not all inserts create an out of balance condition.

Ceng-112 Data Structures I AVL Insert Algorithm algoritm AVLInsert (ref root, ref newPtr, ref taller ) Using recursion, insert a node into AVL tree. PRE root is a pointer to first node in AVL tree/subtree newPtr is a pointer to new node to be inserted. POST taller is a boolean: true indicating the subtree height has increased, false indicating same height.

Ceng-112 Data Structures I AVL Insert Algorithm 1 if (root null) // Insert at root // 1 taller = true2 root= newPtr 2 else 1 if (newPtr->key key)//left subtree // 1 AVLInsert(root->left, newPtr, taller) 2 if (taller)// insertion is completed and height is changed // 1 if (root left-height) 1 leftBalance(root, taller) 2 else if (root right-height) 1 taller =false 3 adjust balance factor 2 else if (newPtr->key > root->key)//right subtree // 1 AVLInsert(root->right, newPtr, taller) 2 if (taller)// insertion is completed and height is changed // 1 if (root left-height) 1 taller =false 2 else if (root right-height) 1 rihtBalance(root, taller) 3 adjust balance factor 3 else 1 error (“Dupe Data”) 2 recycle(newPtr) 3 taller = false 3 return end AVLInsert

Ceng-112 Data Structures I AVL Left Balance Algorithm algorithm leftBalance(ref root, ref taller ) This algorithm is entered when the root is left heavy (the left subtree is higher then the right subtree) PRE root is a pointer to the root of the (sub)tree, taller is true. POST root and taller has been updated. 1 leftTree = root->left 2 if (leftTree left-heigh) //Case 1: Left of left, single rotation required. // 1 rotateRight (root) 2 adjust balance factors 3 taller false 3 else // Case 2: Right of left. Double rotation required. // 1 rightTree = leftTree->right 2 adjust balance factors 3 rotateLeft (leftTree) 4 rotateRight (root) 5 taller = false 4 return end leftBalance

Ceng-112 Data Structures I Figure 8-22 AVL Delete Balancing

Ceng-112 Data Structures I Figure 8-25 Which of the trees in the following figure is a valid binary search tree and which one is not? Excercise

Ceng-112 Data Structures I Figure 8-26 Travers the binary search tree using a inorder traversal. Excercise

Ceng-112 Data Structures I Figure 8-27 The binary search tree was created starting with a null tree and entering data from the keyboard. In what sequence were the data entered? If there is more than one possible sequence, identify the alternatives. Excercise

Ceng-112 Data Structures I Figure 8-28 The binary search tree was created starting with a null tree and entering data from the keyboard. In what sequence were the data entered? If there is more than one possible sequence, identify the alternatives. Excercise

Ceng-112 Data Structures I Figure 8-29 Insert 44, 66 and 77 in the binary search tree. Excercise

Ceng-112 Data Structures I Figure 8-30 Delete the node 60 and then 85 from the tree. Excercise

Ceng-112 Data Structures I Figure 8-31 Balance the tree. Excercise

Ceng-112 Data Structures I Figure 8-32 Balance the tree. Excercise

Ceng-112 Data Structures I Figure 8-33 Add 49 to the AVL tree. The result must be an AVL tree. Show the balance factors in the resulting tree. Excercise

Ceng-112 Data Structures I Figure 8-33 Add 68 to the AVL tree. The result must be an AVL tree. Show the balance factors in the resulting tree. Excercise

Ceng-112 Data Structures I HW-9 1.Create an AVL tree with positive integer numbers which are taken from the screen. 2.Write the AVL delete function which establishes to delete desired node from the AVL. 3.Write the AVL list function which lists the nodes in the AVL with inorder traversal. 4.Collect all above functions under a user menu. Load your HW-9 to FTP site until 14 May. 07 at 09:00 am.