Binary Search Trees CS3240. 2 Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.

Slides:



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

Chapter 8 Binary Search Tree 1 Fall Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees CS 308 – Data Structures What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
1 Chapter 8 Binary Search Trees. 2 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Marc Smith and Jim Ten Eyck
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.
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.
Chapter 8 Binary Search Trees. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
Binary Search Trees. What is a binary tree? Property 1: each node can have up to two successor nodes.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
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.
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 ),
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.
1 5 Linked Structures Based on Levent Akın’s CmpE160 Lecture Slides 3 Binary Trees.
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.
Chapter 8 Binary Search Trees. 2 Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Chapter 8 Binary Search Trees 1. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop 2.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
Binary Search Trees (BST)
Binary Search Trees … From
Chapter 8 Binary Search Trees. Chapter 8: Binary Search Trees 8.1 – Trees 8.2 – The Logical Level 8.3 – The Application Level 8.4 – The Implementation.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Trees and Binary Search Trees
Week 6 - Wednesday CS221.
Chapter 8 Binary Search Trees.
Binary Trees, Binary Search Trees
Binary Tree and General Tree
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.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Trees.
Chapter 8 Binary Search Tree
Yan Shi CS/SE 2630 Lecture Notes
Binary Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Search Trees CS3240

2 Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor child height Define a binary search tree at the logical level Show what a binary search tree would look like after a series of insertions and deletions

3 Goals Implement the following binary search tree algorithms in C++ Inserting an element Deleting an element Retrieving an element Modifying an element Copying a tree Traversing a tree in preorder, inorder, postorder

4 Goals Discuss the Big-O efficiency of a given binary search tree operation Describe an algorithm for balancing a binary search tree Show how a binary tree can be represented in an array, with implicit positional links between the elements Define the terms full binary tree and complete binary tree

5 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Trees Jake’s Pizza Shop

6 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len ROOT NODE Trees

7 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEAF NODES Trees

8 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 0 Trees

9 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 1 Trees

10 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 2 Trees

11 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEFT SUBTREE OF ROOT NODE Trees

12 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len RIGHT SUBTREE OF ROOT NODE Trees

A structure with a unique starting node (the root), in which each node is capable of having zero or more child nodes and a unique path exists from the root to every other node Root The top node of a tree structure; a node with no parent Leaf Node A tree node that has no children 13

Trees can be used… Trees are data structures that can be used for many different applications Representing decisions and logic Partitioning of keys/data in database/file systems 14

15 Trees used to store generic data possibly improve on generic insertion, removal and search operations…. ……Binary Search Tree…. first, what is a Binary Tree

16 Binary Trees Binary Tree A structure with a unique starting node (the root), in which each node is capable of having two child nodes and a unique path exists from the root to every other node used to store generic data possibly improve on generic insertion, removal and search operations…. ……Binary Search Tree…. first, what is a Binary Tree

17 Trees—some terms Level Distance of a node from the root Height The maximum level

18 Trees Why is this not a tree ?

19 Trees Q V T K S A E L How many leaf nodes?

20 Trees Q V T K S A E L Q has how many descendant?

21 Trees Q V T K S A E L S has how many ancestors?

22 Trees How many different binary trees can be made from 2 nodes? 4 nodes? 6 nodes?

Answers…you investigate See… 23

Motivation for the Tree Motivation How can we implement a table? need insert, delete and search operations A1) Sorted Array Binary search is fast O(log(N)) Delete and Insert require O(N) steps A2) Linked List Search is slow O(N) Insert and delete are fast, once we find the right spot Goal Is there a datastructure to support O(log(N)) search, insert and delete operations? Problem with Linked List: in search only one path to take..... what if we had more than one path (link) Solution: Tree??? Best Case: Fast Worst Case: Like a Linked List Better Solution: Constrained Trees (e.g. Binary Tree)  try the Binary Search Tree 24

25 Binary Search Trees A binary tree in which the key value in any node is greater than the key value in the left child and any of its children and less than the key value in the right child and any of its children A BST is a binary tree with the search property

26 Binary Search Trees Each node is the root of a subtree

27 Recursive Implementation

28 Recursive Count Let’s start by counting the number of nodes in a tree Size? Base cases(s)? General case(s)?

29 Recursive Count Count (Version 1) if (Left(tree) is NULL) AND (Right(tree) is NULL) return 1 else return Count (Left(tree)) + Count(Right(tree)) + 1 Apply to these trees

30 Recursive Count Count (Version 2) if (left(tree) is NULL) AND (Right(tree) is NULL) return 1 else if (Left(tree) is NULL) return Count(Right(tree)) + 1 else if (Right(tree) is NULL) return Count(Left(tree)) + 1 else return Count(Left(tree)) + Count(Right(tree)) + 1 Apply to an empty tree

31 Recursive Count Count (Version 3) if (tree is !NULL) // Version 2 Stop Count (Version 4) if (tree is NULL) return 0 else return Count(Left(tree)) + Count(Right(tree)) + 1

32 Recursive Count int TreeType() const { return Count(root); ) int Count(TreeNode* tree) { if (tree == NULL) return 0 else return Count(tree->left) + Count(tree->right) + 1; } Why do we need two functions?

33 Recursive Search ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’ Are ‘D’, ‘Q’, and ‘N’ in the tree?

34 Recursive Search Retrieve(tree, item, found) Size? Base case(s)? General case(s)?

35 Recursive Search void Retrieve(TreeNode* tree, ItemType& item, bool& found) { if (tree == NULL) found = false; else if (item info) Retrieve(tree->left, item, found); else if (item > tree->info) Retrieve(tree->right, item, found); else { item = tree->info; found = true; }

36 Shape depends on the order of item insertion Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order The first value inserted is always put in the root Shape of BST ‘J’

37 Thereafter, each value to be inserted compares itself to the value in the root node moves left it is less or moves right if it is greater When does the process stop? Shape of BST ‘J’ ‘E’

38 Trace path to insert ‘F’ Shape of BST ‘J’ ‘E’ ‘F’

39 Trace path to insert ‘T’ Shape of BST ‘J’ ‘E’ ‘F’ ‘T’

40 Trace path to insert ‘A’ Shape of BST ‘J’ ‘E’ ‘F’ ‘T’ ‘A’

41 Now build tree by inserting ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order Shape of BST And the moral is?

42 Recursive Insertion Insert an item into a tree Where does each new node get inserted? Insert(tree, item) if (tree is NULL) Get a new node Set right and left to NULL Set info to item

43 Recursive Insertion

44 Recursive Insertion –insert 11

45 Recursive Insertion How must the tree be passed? 11

46 Recursive Insertion void Insert(TreeNode*& tree, ItemType item) { if (tree == NULL) {// Insertion place found. tree = new TreeNode; tree->right = NULL; tree->left = NULL; tree->info = item; } else if (item info) Insert(tree->left, item); else Insert(tree->right, item); }

47 Recursive Deletion Delete ‘Z’

48 Recursive Deletion Delete ‘R’

49 Recursive Deletion Delete ‘Q’

50 Recursive Deletion Can you summarize the three deletion cases?

51 Recursive Deletion DeleteItem(tree, item ) …first assume tree is node containing item if (Left(tree) is NULL) AND (Right(tree) is NULL) Set tree to NULL else if Left(tree) is NULL Set tree to Right(tree) else if Right(tree) is NULL Set tree to Left(tree) else Find predecessor Set Info(tree) to Info(predecessor) Delete predecessor

52 Recursive Deletion void Delete(TreeNode*& tree, ItemType item) { if (item info) Delete(tree->left, item); else if (item > tree->info) Delete(tree->right, item); else DeleteNode(tree); // Node found }

53 Recursive Deletion void DeleteNode(TreeNode*& tree) { ItemType data; TreeNode* tempPtr; tempPtr = tree; if (tree->left == NULL) { tree = tree->right; delete tempPtr; } else if (tree->right == NULL){ tree = tree->left; delete tempPtr;} else{ GetPredecessor(tree->left, data); tree->info = data; Delete(tree->left, data);} }

54 Recursive Deletion void GetPredecessor(TreeNode* tree, ItemType& data) { while (tree->right != NULL) tree = tree->right; data = tree->info; } Why is the code not recursive? ---simple to find predecessor---it is the right most node of the left subtree that was passed as a parameter

55 Recursive Deletion

56 Printing the Tree Apply same algorithm to each tree What is the order of the output? Least to greatest A,D,J,M,Q,R,T

57 Printing the Tree PrintTree operation Size? Base case(s)? General case(s)?

58 Printing the Tree void PrintTree(TreeNode* tree, std::ofstream& outFile) { if (tree != NULL) { PrintTree(tree->left, outFile); outFile info; PrintTree(tree->right, outFile); } Is that all there is ?

59 A Question Do we need a destructor? Yes to avoid memory leaks

60 Copying a Tree CopyTree(copy, originalTree) if (originalTree is NULL) Set copy to NULL else Set copy to new node Set Info(copy) to Info(originalTree) CopyTree(Left(copy), Left(originalTree)) CopyTree(Right(copy), Right(originalTree)) How must copy be passed? How must originalTree be passed?

61 Copying a Tree void CopyTree(TreeNode*& copy, const TreeNode* originalTree) { if (originalTree == NULL) copy = NULL; else { copy = new TreeNode; copy->info = originalTree->info; CopyTree(copy->left, originalTree->left); CopyTree(copy->right, originalTree->right); }

62 Traversals Inorder(tree) if tree is not NULL Inorder(Left(tree)) Visit Info(tree) Inorder(Right(tree)) PostOrder(tree) if tree is not NULL Postorder(Left(tree)) Postorder(Right(tree)) Visit Info(tree) PreOrder(tree) if tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree)) alphabetic order destructor (why?) expression evaluation

63 Traversals Each node is visited three times

64 Traversals

65 Iterator Approach l The client program passes a parameter to ResetTree and GetNextItem indicating which of the three traversals to use l ResetTree generates a queue of node contents in the indicated order l GetNextItem processes the node contents from the appropriate queue: inQue, preQue, postQue

66 Iterator void TreeType::ResetTree(OrderType order) // Calls function to create a queue of the tree // elements in the desired order. { switch (order) { case PRE_ORDER : PreOrder(root, preQue); break; case IN_ORDER : InOrder(root, inQue); break; case POST_ORDER: PostOrder(root, postQue); break; }

67 void TreeType::GetNextItem(ItemType& item, OrderType order,bool& finished) { finished = false; switch (order) { case PRE_ORDER : preQue.Dequeue(item); if (preQue.IsEmpty()) finished = true; break; case IN_ORDER : inQue.Dequeue(item); if (inQue.IsEmpty()) finished = true; break; case POST_ORDER: postQue.Dequeue(item); if (postQue.IsEmpty()) finished = true; break; } Iterator

68 Iterative Versions FindNode(tree, item, nodePtr, parentPtr) Set nodePtr to tree Set parentPtr to NULL Set found to false while more elements to search AND NOT found if item < Info(nodePtr) Set parentPtr to nodePtr Set nodePtr to Left(nodePtr) else if item > Info(nodePtr) Set parentPtr to nodePtr Set nodePtr to Right(nodePtr) else Set found to true

69 Iterative Versions InsertItem Create a node to contain the new item Find the insertion place Attach new node Find the insertion place FindNode(tree, item, nodePtr, parentPtr);

70 Iterative Versions Insert 13

71 Iterative Versions

72 Iterative Versions AttachNewNode if item < Info(parentPtr) Set Left(parentPtr) to newNode else Set Right(parentPtr) to newNode See a problem ? What if tree empty and parentPtr Null

73 Iterative Version AttachNewNode(revised) if parentPtr equals NULL Set tree to newNode else if item < Info(parentPtr) Set Left(parentPtr) to newNode else Set Right(parentPtr) to newNode

74 Iterative Version void TreeType::DeleteItem(ItemType item) { TreeNode* nodePtr; TreeNode* parentPtr; FindNode(root, item, nodePtr, parentPtr); if (nodePtr == root) DeleteNode(root); else if (parentPtr->left == nodePtr) DeleteNode(parentPtr->left); else DeleteNode(parentPtr->right); } Why not parentPtr? Why not nodePtr?

75 Iterative Version parentPtr and nodePtr are external; parentPtr->left is internal

76 Recursion VS Iteration Compare versions of Tree algorithms Is the depth of recursion relatively shallow? Is the recursive solution shorter or cleaner? Is the recursive version much less efficient?

77 Nonlinked Representation What is the mapping into the index?

78 Nonlinked Representation For any node tree.nodes[index] its left child is in tree.nodes[index*2 + 1] right child is in tree.nodes[index*2 + 2] its parent is in tree.nodes[(index - 1)/2] Can you determine which nodes are leaf nodes? ANSWER: any index where left and right contain nothing (represent say with the value -1 or some other unique number)

79 Nonlinked Representation Full Binary Tree A binary tree in which all of the leaves are on the same level and every nonleaf node has two children

80 Nonlinked Representation Complete Binary Tree A binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible

81 Nonlinked Representation

82 Nonlinked Representation A technique for storing a non- complete tree