Chapter 8 Binary Search Tree

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
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 Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Marc Smith and Jim Ten Eyck
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
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.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
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.
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.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
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.
Data Structures Using C++ 2E Chapter 11 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 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Binary Search Trees CS Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CSE 373 Data Structures Lecture 7
Data Structure and Algorithms
Trees Chapter 11 (continued)
Binary Trees and Binary Search Trees
Trees Chapter 11 (continued)
Data Structures Binary Trees 1.
UNIT III TREES.
Week 6 - Wednesday CS221.
Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
CMSC 341 Introduction to Trees.
Week 11 - Friday CS221.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
ITEC 2620M Introduction to Data Structures
Chapter 8 Binary Search Trees.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Data Structures and Database Applications Binary Trees in C#
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
CMSC 341 Binary Search Trees.
CS6045: Advanced Algorithms
Binary Trees, Binary Search Trees
CMSC 341 Binary Search Trees.
Yan Shi CS/SE 2630 Lecture Notes
Tree.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
C++ Plus Data Structures
CMSC 341 Binary Search Trees 2/21/2006.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Chapter 8 Binary Search Tree Fall 2010

Binary Trees A structure with: a unique starting node (the root), in which each node has up to two child nodes, and a unique path exists from the root to every other node Root Top node of a tree structure; a node with no parent Leaf Node A tree node that has no children

Trees: level and height Level: distance of a node from the root Height: the maximum level

Trees Why is this not a tree? A tree is a structure with i) a unique starting node (the root), in which ii) each node can have up to two child nodes and iii) a unique path exists from the root to every other node

Descendants Descendant of a node is a child of the node, and any child of the children, etc. V Q L T E A K S How many descendants does Q have?

Ancestors Ancestor of a node: a parent of the node, the parent of the parent, etc. V Q L T E A K S How many ancestors does S have?

Binary Trees Come in Different Shapes How many structurally different binary trees can be made from 2 nodes? 4 nodes? 6 nodes?

Binary Tree Facts Max. number of nodes at Nth level : 2N. 0th level: root node: 1 1st level: 2 Double as level increases by 1 Suppose f(n) denotes the maximum number of nodes at the nth level: f(0)=1 f(n)=2*f(n-1) for n>=1 A recursive formula! f(n)=2n

Binary Tree Facts Max. total number of nodes in a tree of height N All levels are full, so add the max. number of nodes at each level together: 20+21+22+…+2N= 2N+1-1

Binary Tree Facts Given a binary tree with N nodes, what is the min. number of levels? Try to fill in each level The answer is: log2N + 1 Max. number of levels with N nodes ? One node at each level => degenerates to a linked list The answer is: N

Binary Search Trees (BST) A BST is a binary tree with a search property. A binary tree in which, for each node: key value in the node is greater than the key value in its left child and any of the left child’s descendents (left sub-tree) key value in the node is less than the key value in its right child and any of the right child’s descendents (right sub-tree)

Binary Search Trees Each node is the root of a subtree rooted at the node

Binary Search Tree ADT Application level: same as List Logic Level void MakeEmpty() bool IsEmpty() bool IsFull() int GetLength() RetrieveItem(ItemType &item, bool &found) InsertItem (ItemType item) DeleteItem (ItemType item) Print(ofstream &outFile) ResetTree(OrderType order) GetNextItem (ItemType &item, OrderType order,bool &finished)

Tree node in BST Can you define a structure to represent a binary tree node ?

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

Recursive 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:

Recursive 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

Recursive Count: version 3 if (tree is NULL) return 0 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

Recursive Count: version 4 if (tree is NULL) return 0 else return Count(Left(tree)) + Count(Right(tree)) + 1

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

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

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

Recursive Search void TreeType::Retrieve(TreeNode* tree, ItemType& item, bool& found) const { if (tree == NULL) found = false; //base case else if (item < tree->info) Retrieve(tree->left, item, found); else if (item > tree->info) Retrieve(tree->right, item, found); else //base case item = tree->info; found = true; }

Shape of BST 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 ‘J’

Shape of BST 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? ‘J’ ‘E’

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

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

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

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

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 and insert at this location Set right and left to NULL Set info to item else if (item is larger than tree->info) Insert(tree->right, item) else Insert(tree->left, item)

Recursive Insertion

Recursive Insertion Insert item 12

Recursive Insertion How must the tree be passed?

Recursive Insertion void TreeType::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 < tree->info) Insert(tree->left, item); else Insert(tree->right, item);

Deleting a Leaf Node

Deleting a Node with One Child

Deleting a Node with Two Children

Delete an existing item from BST Can you summarize the three deletion cases? Deleting a leaf node. Deleting a node with only one child. Deleting a node with two children.

Predecessor Predecessor: the element whose key immediately precedes (less than) the key of item If the item node has a left child, the largest element in the left subtree (right-most child) If the item has no left child, …

Successor Successor: the element whose key immediately follows (greater than) the key of item If the item node has two children, the smallest element in the right subtree (left-most child) If the item node has no right child, …

Recursive Deletion DeleteItem(tree, item) if (Left(tree) is NULL) AND (Right(tree) is NULL) // delete ‘Z’ Set tree to NULL else if (Left(tree) is NULL AND (Right(tree)) is not NULL) delete ‘R’ Set tree to Right(tree) else if (Right(tree) is NULL AND (Left(tree)) is not NULL) Set tree to Left(tree) else // delete ‘Q’, maintain a binary search tree Find predecessor Set Info(tree) to Info(predecessor) Delete predecessor

Recursive Deletion deletes item from a tree rooted at tree TreeType::DeleteItem (ItemType item) deletes item from the current object (a tree object) void Delete( TreeNode*& tree, ItemType item) deletes item from a tree rooted at tree void DeleteNode( TreeNode*& tree) deletes node pointed to by tree from a BST void GetPredecessor( TreeNode* tree, ItemType& data) finds data’s predecessor – the largest item in data’s left subtree and saves the info into data.

Recursive Deletion void TreeType::DeleteItem(ItemType item) { Delete(root, item); } //Calls the recursive function Delete to //delete item from tree. 43 43

Recursive Deletion // first, find which node should be deleted. void TreeType::Delete(TreeNode*& tree, ItemType item) { if (item < tree->info) Delete(tree->left, item); else if (item > tree->info) Delete(tree->right, item); else DeleteNode(tree); // Node found }

Recursive Deletion void TreeType::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; }else{ GetPredecessor(tree->left, data); tree->info = data; Delete(tree->left, data); } Tracing this function using various examples…

Find Predecessor void TreeType::GetPredecessor( TreeNode* tree, ItemType& data) { //the largest item is located in its rightmost node. while (tree->right != NULL) tree = tree->right; data = tree->info; } This function could be named GetLargestItem() as it returns the largest item stored in tree rooted at tree.

Recursive Deletion

Traversals Tree Traversal : visiting all the nodes of a tree Depth-First Traversal, Breadth-First Traversal Depth-First Traversal Inorder Traversal Preorder Traversal Postorder Traversal 48 48

Inorder Traversal   Inorder traversal visits the root in between visiting the left and right subtrees: Inorder traversal only makes sense for binary trees. Inorder(tree) if tree is not NULL Inorder(Left(tree)) Visit Info(tree) Inorder(Right(tree)) A B C D E F G 49 49

Preorder Traversal Preorder traversal visits the root first. PreOrder(tree) if tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree)) D B A C F E G 50 50

Postorder Traversal Postorder traversal visits the root last. PostOrder(tree) if tree is not NULL Postorder(Left(tree)) Postorder(Right(tree)) Visit Info(tree) A C B E G F D 51 51

Traversals 52 52

Printing the Tree Traversal Algorithm : Inorder traversal 53 53

Iterators Iterator functions for Binary Search Tree: ResetTree() GetNextItem() Provide different traversal orders In-order, pre-order, post-order Use a parameter to select which traversal order

Iterator Implementation ResetTree generates a queue of nodes in the indicated order Add private data members: three queues storing ItemTypes (tree node’s content) QueType inQue, preQue, postQue; GetNextItem returns next node content from the appropriate queue

Iterator What if the queue is not empty when ResetTree() is called ? 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); case POST_ORDER: PostOrder(root, postQue); } Insert the info field of nodes in tree (given by root) into preQue in pre-order What if the queue is not empty when ResetTree() is called ?

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

PreOrder() function void PreOrder(TreeNode * treeRoot, QueType & preQue) { if (treeRoot!=NULL){ preQue->enqueue (treeRoot->info); PreOrder (treeRoot->left, preQue); PreQrder (treeRoot->right, preQue); } Hint: For recursive functions on a binary tree, making an empty tree case your base case will lead to a cleaner and simpler code.

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

Printing the Tree void TreeType::PrintTree(TreeNode* tree, std::ofstream& outFile) { if (tree != NULL) PrintTree(tree->left, outFile); outFile << tree->info; PrintTree(tree->right, outFile); } Does this allow a reconstruction of the tree (same shape) based on the output?

Breadth-first traversal of tree Visit the nodes in the order of their layers, and, within each layer, visit nodes from left to right… For the tree in figure: D B F A C E G Printout in this order can be used to reconstruct the tree, why? Could make tree printout more readable D / \ B F /\ /\ A C E G

BST: Destructor Do we need a destructor? Yes, as dynamic allocated memory is used. Delete all nodes (free memory space occupied by them). In which order shall we delete the tree nodes? i.e., which Traversal order: inorder, preorder, postorder? TreeType::~TreeType(){ DeleteTree(root); root = NULL; } void TreeType::DeleteTreeType(TreeNode * root){ if (root!=NULL){ DeleteTree(root->left); DeleteTree(root->right); delete root; 62 62

Copy a Tree Copy constructor: Allows initialization of an object by copying from an existing one TreeType newTree(oldTree); Syntax of a copy constructor: TreeType::TreeType(const TreeType & originalTree); Overloaded Assignment Operator = TreeType newTree; newTree = oldTree; Syntax of assignment operator Void TreeType::operator=(const TreeType & originalTree); Both involves copying a tree Use which traversal order? Preorder Traversal

How must originalTree be passed? 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?

Copy 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); } 65 65

Recursive solutions so far Recursive solutions so far. Now, let’s try to solve some of the problem iteratively….

Iterative Search Searching for an item FindNode(tree, item, nodePtr, parentPtr) Searching for an item If a node is found, get two pointers, one points to the node and one points to its parent node. If root is the matching node, the first pointer points to the root and the second pointer points to NULL. If no node is found, the first pointer points to NULL and the second pointer points to its logical parent node.

Iterative Versions FindNode(tree, item, nodePtr, parentPtr) Set nodePtr to tree Set parentPtr to NULL Set found to false while there are more elements to search AND NOT found if item < Info(nodePtr) // search left subtree Set parentPtr to nodePtr Set nodePtr to Left(nodePtr) else if item > Info(nodePtr) // search right subtree Set nodePtr to Right(nodePtr) else //match Set found to true

void TreeType::FindNode(TreeNode. tree, ItemType item, TreeNode void TreeType::FindNode(TreeNode * tree, ItemType item, TreeNode *& nodePtr, TreeNode *& parentPtr) { nodePtr = tree; parentPtr = NULL; bool found = false; while( nodePtr != NULL & found == false) if (item.ComparedTo(nodePtr->info)== LESS) parentPtr = nodePtr; nodePtr = nodePtr->left; }else if (item.ComparedTo(nodePtr->info) == LARGER) nodePtr = nodePtr->right; }else found = true; }

Iterative insertion 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); If no node is found, the first pointer points to NULL and the second pointer points to its logical parent node.

Iterative Versions Insert 13

Iterative Versions

Iterative Versions AttachNewNode if item < Info(parentPtr) Set Left(parentPtr) to newNode else Set Right(parentPtr) to newNode

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

Iterative Version: DeleteItem 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 directly delete nodePtr?

Recursive Deletion: review void TreeType::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; }else{ GetPredecessor(tree->left, data); tree->info = data; Delete(tree->left, data); } 76 76

Iterative Version parentPtr and nodePtr are external; parentPtr->left is internal (to the tree)

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?

Nonlinked Representation What is the mapping into the index?

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?

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

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

Nonlinked Representation

Nonlinked Representation A technique for storing a non-complete tree

Binary Search Trees The same set of keys may have different BSTs Average depth of a node is O(log2 N) Maximum depth of a node is O(N)

Time Complexity Time complexity of Searching: O(height of the tree) Time complexity of Inserting: Time complexity of Deleting:

Comparison Assume that we have a Complete tree. Binary Search Tree Array-based Linear List Linked List Constructor O(1) Destructor O(N) O(1) (non-pointers) MakeEmpty GetLength IsFull IsEmpty RetrieveItem O(log2N) InsertItem DeleteItem