Download presentation
Presentation is loading. Please wait.
1
Sorted Binary Trees
2
Non-sorted binary trees
There are many uses for binary trees = x + 1 The expression x = x + 1 Mary Bob Ann Jack Jill Tom Tina A genealogy: left=father, right=mother Many—in fact, most—of these uses do not involve the idea of a “sorted” binary tree
3
Sorted binary trees We will call a binary tree sorted if, for every node in the binary tree, Every value in the left subtree of this node (if any) is less than the value in this node Every value in the right subtree of this node (if any) is greater than or equal to the value in this node It doesn’t really matter which side “equal” values go on, so long as we are consistent 15 12 20 16 25 4 8 21 6 9
4
Inserting nodes Inserting a new node is easy
If the value is smaller than this node, go left If the value is greater or equal, go right Add the node as a leaf Example: add a node containing 23 to this binary tree 15 12 20 16 25 4 8 21 6 9 23 15 12 20 16 25 4 8 21 6 9
5
Deleting nodes To delete a node from a sorted binary tree, leaving the binary tree sorted, there are three cases: Deleting a leaf Deleting a node with one child Deleting a node with two children
6
Deleting a leaf Case 1: Deleting a leaf (a node with no children)
This is trivial--just remove it 15 12 20 16 25 4 8 21 6 9 6
7
Deleting a node with one child
Case 2: Deleting a node with just one child This is simple--the child moves up to replace the deleted node 15 12 20 16 25 4 8 21 6 9
8
Deleting a node with one child
Case 2: Deleting a node with just one child This is simple--the child moves up to replace the deleted node 15 12 20 16 25 8 21 6 9
9
Deleting a node with one child
Case 2: Deleting a node with just one child This is simple--the child moves up to replace the deleted node 15 12 20 16 25 8 21 6 9
10
Deleting a node with two children
Case 3: Deleting a node with two children Find the inorder successor Replace the deleted node with its successor 15 12 20 16 25 4 8 21 6 9
11
Deleting a node with two children
Case 3: Deleting a node with two children Find the inorder successor Replace the deleted node with its successor 15 12 20 16 25 4 8 21 6 9
12
Deleting a node with two children
Case 3: Deleting a node with two children Find the inorder successor Replace the deleted node with its successor 12 20 16 25 4 8 21 6 9
13
Deleting a node with two children
Case 3: Deleting a node with two children Find the inorder successor Replace the deleted node with its successor 12 20 16 25 4 8 21 6 9 It gets more complicated if the inorder successor has a right child It cannot have a left child
14
Finding the inorder successor
Let node be the node to be deleted successor = node.rightChild; 20 10 40 30 90 60 70 50 80 while (successor.leftChild != null) successor = successor.leftChild; This only works if the node to be deleted has a right child We are only using it in the case where the node to be deleted has two children, so it’s good enough for our current purposes
15
If inorder successor has right child
Notice that the inorder successor cannot have a left child If it did, that child would be the inorder successor We need to replace the 40 with 50, as before 20 10 40 30 90 60 70 50 80
16
If inorder successor has right child
Notice that the inorder successor cannot have a left child If it did, that child would be the inorder successor We need to replace the 40 with 50, as before 20 10 30 90 60 70 50 80 But we also need to move the right child (and any subtree beneath it) up to the deleted node’s position
17
If inorder successor has right child
Notice that the inorder successor cannot have a left child If it did, that child would be the inorder successor We need to replace the 40 with 50, as before 20 10 30 90 60 70 50 80 But we also need to move the right child (and any subtree beneath it) up to the deleted node’s position
18
Observations Deleting a node from the “middle” of a binary tree (as opposed to deleting a leaf) is an unusual operation We did it in such a way as to keep a sorted binary tree in sorted order This procedure would not make any sense for other, non-sorted binary trees If we have a sorted binary tree, it is probably because we want to do searches on it If so, we also want to keep the binary tree balanced There are several complex algorithms for doing this
19
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.