Binary Search Trees. John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

Trees Types and Operations
BST Search To find a value in a BST search from the root node: If the target is less than the value in the node search its left subtree If the target is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Marc Smith and Jim Ten Eyck
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
Binary Trees Chapter 6.
Tree.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Data Structures CSCI 2720 Spring 2007 Balanced Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Binary Search Trees … From
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
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.
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 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
ADT description Implementations
Cinda Heeren / Geoffrey Tien
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Ch. 11 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.
Introduction to Trees IT12112 Lecture 05.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Trees, Binary Search Trees
Trees.
Tree traversals BST properties Search Insertion
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Search Trees

John Edgar  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary search trees  Implement the TreeSort algorithm 2

John Edgar3

 A set of nodes (or vertices) with a single starting point  called the root  Each node is connected by an edge to another node  A tree is a connected graph  There is a path to every node in the tree  A tree has one less edge than the number of nodes 4

John Edgar5 yes! NO! All the nodes are not connected NO! There is an extra edge (5 nodes and 5 edges) yes! (but not a binary tree) yes! (it’s actually the same graph as the blue one)

A BCD GEF NNode v is said to be a child of u, and u the parent of v if TThere is an edge between the nodes u and v, and uu is above v in the tree, TThis relationship can be generalized EE and F are descendants of A DD and A are ancestors of G BB, C and D are siblings FF and G are? 6 root edge parent of B, C, D

John Edgar  A leaf is a node with no children  A path is a sequence of nodes v 1 … v n  where v i is a parent of v i+1 (1  i  n)  A subtree is any node in the tree along with all of its descendants  A binary tree is a tree with at most two children per node  The children are referred to as left and right  We can also refer to left and right subtrees 7

John Edgar8 C A B C D GEF EFGDGA C, E, F and G are leaves path from A to D to G subtree rooted at B

9 A BC GDE left subtree of A H IJ F right subtree of C right child of A

 The height of a node v is the length of the longest path from v to a leaf  The height of the tree is the height of the root  The depth of a node v is the length of the path from v to the root  This is also referred to as the level of a node  Note that there is a slightly different formulation of the height of a tree  Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root) 10

John Edgar11 A BC GDE H IJ F A B E height of node B is 2 height of the tree is 3 depth of node E is 2 level 1 level 2 level 3 BA

John Edgar  A binary tree is perfect, if  No node has only one child  And all the leaves have the same depth  A perfect binary tree of height h has  2 h+1 – 1 nodes, of which 2 h are leaves  Perfect trees are also complete 12 A BC GDE F

John Edgar  Each level doubles the number of nodes  Level 1 has 2 nodes (2 1 )  Level 2 has 4 nodes (2 2 ) or 2 times the number in Level 1  Therefore a tree with h levels has 2 h+1 - 1nodes  The root level has 1 node the bottom level has 2 h nodes, that is, just over ½ the nodes are leaves the bottom level has 2 h nodes, that is, just over ½ the nodes are leaves

John Edgar  A binary tree is complete if  The leaves are on at most two different levels,  The second to bottom level is completely filled in and  The leaves on the bottom level are as far to the left as possible 14 A BC DE F

John Edgar  A binary tree is balanced if  Leaves are all about the same distance from the root  The exact specification varies  Sometimes trees are balanced by comparing the height of nodes  e.g. the height of a node’s right subtree is at most one different from the height of its left subtree  Sometimes a tree's height is compared to the number of nodes  e.g. red-black trees 15

John Edgar16 A A B B C C F F D D E E A A B B C C F F D D E E G G

John Edgar17 A B CD A BC E D F

John Edgar18

John Edgar  A traversal algorithm for a binary tree visits each node in the tree  Typically, it will do something while visiting each node!  Traversal algorithms are naturally recursive  There are three traversal methods  Inorder  Preorder  Postorder 19

John Edgar inOrder(Node* nd) { if (nd != NULL) { inOrder(nd->leftChild); visit(nd); inOrder(nd->rightChild); } inOrder(Node* nd) { if (nd != NULL) { inOrder(nd->leftChild); visit(nd); inOrder(nd->rightChild); } 20 The visit function would do whatever the purpose of the traversal is, for example print the data in the node

John Edgar visit(nd) preOrder(nd->leftChild) preOrder(nd->rightChild) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right)

John Edgar postOrder(left) postOrder(right) visit postOrder(nd->leftChild) postOrder(nd->rightChild) visit(nd) postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit

John Edgar23

John Edgar  The binary tree ADT can be implemented using a number of data structures  Reference structures (similar to linked lists)  Arrays  We will look at three implementations  Binary search trees (references)  Red – black trees  Heap (arrays) 24

John Edgar  Consider maintaining data in some order  The data is to be frequently searched on the sort key e.g. a dictionary  Possible solutions might be:  A sorted array ▪ Access in O(logn) using binary search ▪ Insertion and deletion in linear time  An ordered linked list ▪ Access, insertion and deletion in linear time 25

John Edgar  The data structure should be able to perform all these operations efficiently  Create an empty dictionary  Insert  Delete  Look up  The insert, delete and look up operations should be performed in at most O(logn) time 26

John Edgar  A binary search tree is a binary tree with a special property  For all nodes in the tree: ▪ All nodes in a left subtree have labels less than the label of the subtree's root ▪ All nodes in a right subtree have labels greater than or equal to the label of the subtree's root  Binary search trees are fully ordered 27

John Edgar

John Edgar inOrder(nd->leftChild) visit(nd) inOrder(nd->rightChild) inOrder(left) visit inOrder(right) inOrder(left) visit inOrder(right) inOrder(left) visit inOrder(right) inOrder(left) visit inOrder(right) inOrder(left) visit inOrder(right) inOrder(left) visit inOrder(right) inOrder(left) visit inOrder(right) An inorder traversal retrieves the data in sorted order

John Edgar30

John Edgar  Binary search trees can be implemented using a reference structure  Tree nodes contain data and two pointers to nodes 31 Node* leftChild Node* rightChild data references or pointers to Nodes data to be stored in the tree (usually an object)

John Edgar  To find a value in a BST search from the root node:  If the target is less than the value in the node search its left subtree  If the target is greater than the value in the node search its right subtree  Otherwise return true, (or a pointer to the data, or …)  How many comparisons?  One for each node on the path  Worst case: height of the tree

John Edgar click on a node to show its value

John Edgar34 bool search(x, Node* nd){ if (nd == NULL){ return false; }else if(x == nd->data){ return true; } else if (x data){ return search(x, nd->left); } else { return search(x, nd->right); } bool search(x, Node* nd){ if (nd == NULL){ return false; }else if(x == nd->data){ return true; } else if (x data){ return search(x, nd->left); } else { return search(x, nd->right); } reached the end of this path note the similarity to binary search called by a helper method like this: search(target, root)

John Edgar35

John Edgar  The BST property must hold after insertion  Therefore the new node must be inserted in the correct position  This position is found by performing a search  If the search ends at the (null) left child of a node make its left child refer to the new node  If the search ends at the right child of a node make its right child refer to the new node  The cost is about the same as the cost for the search algorithm, O(height) 36

John Edgar create new node insert 43 find position insert new node

John Edgar38

John Edgar  After deletion the BST property must hold  Deletion is not as straightforward as search or insertion  With insertion the strategy is to insert a new leaf  Which avoids changing the internal structure of the tree  This is not possible with deletion ▪ Since the deleted node's position is not chosen by the algorithm  There are a number of different cases that must be considered 39

John Edgar  The node to be deleted has no children  Remove it (assigning null to its parent’s reference)  The node to be deleted has one child  Replace the node with its subtree  The node to be deleted has two children  … 40

John Edgar delete

John Edgar delete 79 replace with subtree delete 79 replace with subtree

John Edgar delete 79 after deletion delete 79 after deletion

John Edgar  One of the issues with implementing a BST is the necessity to look at both children  And, just like a linked list, look ahead for insertion and deletion  And check that a node is null before accessing its member variables  Consider deleting a node with one child in more detail 44

John Edgar delete 59 Step 1 - we need to find the node to delete and its parent it’s useful to know if nd is a left or right child while (nd != target) if (nd == NULL) return if (target data) parent = nd nd = nd->left isLeftChild = true else parent = nd nd = nd->right isLeftChild = false while (nd != target) if (nd == NULL) return if (target data) parent = nd nd = nd->left isLeftChild = true else parent = nd nd = nd->right isLeftChild = false

John Edgar delete 59 parent nd isLeftChild = false Now we have enough information to detach 59, after attaching its child to 54

John Edgar  The most difficult case is when the node to be deleted has two children  The strategy when the deleted node had one child was to replace it with its child  But when the node has two children problems arise  Which child should we replace the node with?  We could solve this by just picking one …  But what if the node we replace it with also has two children?  This will cause a problem 47

John Edgar delete 32 let’s say that we decide to replace it with its right child (41) But 41 has 2 children, and it also has to inherit (adopt?) the other child of its deleted parent

John Edgar  When a node has two children, instead of replacing it with one of its children find its predecesor  A node’s predecessor is the right most node of its left subtree  The predecessor is the node in the tree with the largest value less than the node’s value  The predecesor cannot have a right child and can therefore have at most one child  Why? 49

John Edgar ’s predecessor the predecessor of 32 is the right most node in its left subtree The predecessor cannot have a right child as it wouldn’t then be the right most node 30

John Edgar  The predecssor has some useful properties  Because of the BST property it must be the largest value less than its ancestor’s value ▪ It is to the right of all of the nodes in its ancestor’s left subtree so must be greater than them ▪ It is less than the nodes in its ancestor’s right subtree  It can have only one child  These properties make it a good candidate to replace its ancestor 51

John Edgar  The successor to a node is the left most child of its right subtree  It has the smallest value greater than its ancestor’s value  And cannot have a left child  The successor can also be used to replace a deleted node  Pick either! 52

John Edgar delete 32 find successor and detach temp 37

John Edgar find successor and detach delete 32 temp attach node’s children to its successor 37 temp

John Edgar find successor and detach delete 32 attach node’s children 37 temp make successor child of node’s parent

John Edgar find successor and detach delete 32 attach node’s children 37 temp make successor child in this example the successor had no subtree

John Edgar delete 63 find predecessor* *just because … temp 59

John Edgar delete 63 find predecessor temp 59 attach predecessor’s subtree to its parent

59 John Edgar delete 63 find predecessor temp attach pre’s subtree attach node’s children to predecessor temp 59

John Edgar delete 63 find predecessor attach pre’s subtree attach node’s children temp 59 attach the predecessor to the node’s parent

John Edgar delete 63 find predecessor attach pre’s subtree attach node’s children 59 attach the predecessor to the node’s parent

John Edgar  Instead of deleting a BST node mark it as deleted in some way  Set the data object to null, for example  And change the insertion algorithm to look for empty nodes  And insert the new item in an empty node that is found on the way down the tree 62

John Edgar  An alternative to the deletion approach for nodes with 2 children is to replace the data  The data from the predecessor node is copied into the node to be deleted  And the predecessor node is then deleted ▪ Using the approach described for deleting nodes with one or no children  This avoids some of the complicated pointer assignments 63

John Edgar64

John Edgar  The efficiency of BST operations depends on the height of the tree  All three operations (search, insert and delete) are O(height)  If the tree is complete the height is  log(n)   What if it isn’t complete? 65

John Edgar IInsert 7 IInsert 4 IInsert 1 IInsert 9 IInsert 5 IIt’s a complete tree! height =  log(5)  = 2

John Edgar IInsert 9 IInsert 1 IInsert 7 IInsert 4 IInsert 5 IIt’s a linked list with a lot of extra pointers! height = n – 1 = 4 = O(n)

John Edgar  It would be ideal if a BST was always close to complete  i.e. balanced  How do we guarantee a balanced BST?  We have to make the structure and / or the insertion and deletion algorithms more complex ▪ e.g. red – black trees. 68

John Edgar IIt is possible to sort an array using a binary search tree IInsert the array items into an empty tree WWrite the data from the tree back into the array using an InOrder traversal RRunning time = n*(insertion cost) + traversal IInsertion cost is O(h) TTraversal is O(n) TTotal = O(n) * O(h) + O(n), i.e. O(n * h) IIf the tree is balanced = O(n * log(n)) 69