Download presentation
Presentation is loading. Please wait.
Published byRosa Moody Modified over 8 years ago
1
10-1 10 Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
2
10-2 Binary trees (1) A binary tree consists of a header, plus a number of nodes connected by links in a hierarchical data structure: Each node contains an element (value or object), plus links to at most two other nodes (its left child and right child). The header contains a link to a node designated as the root node. D B A E C F G header root node leaf node
3
10-3 Binary trees (2) A leaf node is one that has no children (i.e., both its links are null). Every node, except the root node, is the left or right child of exactly one other node (its parent). The root node has no parent – the only link to it is the header. The size of a binary tree is the number of nodes (elements). An empty binary tree has size zero. Its header is null.
4
10-4 A’s left subtree A’s right subtree Binary trees and subtrees (1) Each node has both a left subtree and a right subtree (either of which may be empty). The node’s left (right) subtree consists of the node’s left (right) child together with that child’s own children, grandchildren, etc. C’s left subtree C’s right subtree B’s right subtree B’s left subtree is empty D B A E C F G
5
10-5 Binary trees and subtrees (2) Each subtree is itself a binary tree. This gives rise to an equivalent recursive definition. A binary tree is: empty, or nonempty, in which case it has a root node containing an element, a link to a left subtree, and a link to a right subtree.
6
10-6 Node and tree depths (1) Observation: For any node N in a tree, there is exactly one sequence of links between the root node and N. The depth of node N is the number of links between the root node and N. The depth of a tree is the depth of the deepest node in the tree. A tree consisting of a single node has depth 0. By convention, an empty tree has depth –1.
7
10-7 depth 0 depth 1 depth 2 depth 3 depth 0 depth 1 depth 2 Node and tree depths (2) Illustrations: D B A E C F (a) M K N P L J (b) balanced ill-balanced
8
10-8 depth 4 depth 5 depth 3 depth 0 depth 1 depth 2 Node and tree depths (3) Illustrations (continued): very ill- balanced S T (c) U V W X
9
10-9 Balanced binary trees A binary tree of depth d is balanced if all nodes at depths 0, 1, …, d–2 have two children. Nodes at depth d–1 may have two/one/no children. Node at depth d have no children (by definition). A binary tree of depth 0 or 1 is always balanced. A balanced binary tree of depth d has at least 2 d and at most 2 d+1 – 1 nodes. Conversely: Depth of balanced binary tree of size n = floor(log 2 n) An ill-balanced binary tree of depth d could have as few as d+1 nodes. Conversely: Max. depth of ill-balanced binary tree of size n = n–1
10
10-10 Binary search trees (1) A binary search tree (or BST) is a binary tree with the following property. For any node in the binary tree, if that node contains element elem: Its left subtree (if nonempty) contains only elements less than elem. Its right subtree (if nonempty) contains only elements greater than elem.
11
10-11 Binary search trees (2) Illustrations: (a) dog cat fox lion pig rat tiger (b) pig cat dog fox lionrat tiger
12
10-12 Binary search trees (3) An equivalent recursive definition is also possible. A binary search tree is: empty, or nonempty, in which case it has: a root node containing an element elem a link to a left subtree in which (if it is not empty) all elements are less than elem a link to a right subtree in which (if it is not empty) all elements are greater than elem.
13
10-13 Binary search trees (4) Java class implementing BST nodes: public class BSTNode { protected Comparable element; protected BSTNode left, right; protected BSTNode (Comparable elem) { element = elem; left = null; right = null; } … } BSTNode methods (to follow)
14
10-14 Binary search trees (5) Java class implementing BST headers: public class BST { private BSTNode root; public BST () { // Construct an empty BST. root = null; } … } BST methods (to follow)
15
10-15 BST search (1) Problem: Search for a given target value in a BST. Idea: Compare the target with the element in the root node. If it is equal, the search is successful. If it is less, search the left subtree. If it is greater, search the right subtree. If the subtree is empty, the search is unsuccessful.
16
10-16 BST search (2) BST search algorithm: To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null: 2.1.1.Terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element: 2.2.1.Terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element: 2.3.1.Set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element: 2.4.1.Set curr to curr’s right child.
17
10-17 BST search (3) Animation (successful search): To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger pig target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr pig target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr pig target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr pig target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr pig target
18
10-18 BST search (4) Animation (unsuccessful search): To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger goat target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr goat target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr goat target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr goat target To find which if any node of a BST contains an element equal to target: 1.Set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, terminate with answer none. 2.2.Otherwise, if target is equal to curr’s element, terminate with answer curr. 2.3.Otherwise, if target is less than curr’s element, set curr to curr’s left child. 2.4.Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger curr goat target
19
10-19 BST search (5) Analysis (counting comparisons): Let the BST’s size be n. If the BST has depth d, the number of comparisons is at most d+1. If the BST is well-balanced, its depth is floor(log 2 n): Max. no. of comparisons = floor(log 2 n) + 1 Best-case time complexity is O(log n). If the BST is ill-balanced, its depth is at most n–1: Max. no. of comparisons = n Worst-case time complexity is O(n).
20
10-20 BST search (6) Implementation as a Java method (in class BST ): public BSTNode search (Comparable target) { int direction = 0; BSTNode curr = root; for (;;) { if (curr == null) return null; direction = target.compareTo(curr.element); if (direction == 0) return curr; else if (direction < 0) curr = curr.left; else curr = curr.right; } }
21
10-21 BST insertion (1) Idea: To insert a new element into a BST, proceed as if searching for that element. If the element is not already present, the search will lead to a null link. Replace that null link by a link to a leaf node containing the new element.
22
10-22 BST insertion (2) BST insertion algorithm: To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null: 2.1.1.Replace the null link from which curr was taken (either the BST’s root or parent’s left child or parent’s right child) by a link to a newly-created leaf node with element elem. 2.1.2.Terminate. 2.2.Otherwise, if elem is equal to curr’s element: 2.2.1.Terminate. 2.3.Otherwise, …
23
10-23 BST insertion (3) BST insertion algorithm (continued): 2.3.Otherwise, if elem is less than curr’s element: 2.3.1.Set parent to curr, and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element: 2.4.1.Set parent to curr, and set curr to curr’s right child.
24
10-24 BST insertion (4) Animation (empty BST): To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root goat elem To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root goat elem curr parent To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root goat elem goat curr parent
25
10-25 BST insertion (5) Animation (nonempty BST): To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root fox lion rat goat elem cat To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root fox lion rat curr goat elem parent cat To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root fox lion rat curr goat elem parent cat To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root fox lion rat curr goat elem parent cat To insert the element elem into a BST: 1.Set parent to null, and set curr to the BST’s root. 2.Repeat: 2.1.If curr is null, replace the null link from which curr was taken by a link to a newly-created leaf node with element elem, and terminate. 2.2.Otherwise, if elem is equal to curr’s element, terminate. 2.3.Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr and set curr to curr’s right child. root cat fox lion rat curr goat elem parent goat
26
10-26 BST insertion (6) Analysis (counting comparisons): No. of comparisons is the same as for BST search. If the BST is well-balanced: Max. no. of comparisons = floor(log 2 n) + 1 Best-case time complexity is O(log n). If the BST is ill-balanced: Max. no. of comparisons = n Worst-case time complexity is O(n).
27
10-27 BST insertion (7) Implementation as a Java method (in class BST ): public void insert (Comparable elem) { int direction = 0; BSTNode parent = null, curr = root; for (;;) { if (curr == null) { BSTNode ins = new BSTNode(elem); if (root == null) root = ins; else if (direction < 0) parent.left = ins; else parent.right = ins; return; }
28
10-28 BST insertion (8) Implementation (continued): direction = elem.compareTo(curr.element); if (direction == 0) return; parent = curr; if (direction < 0) curr = curr.left; else curr = curr.right; } }
29
10-29 BSTs in practice: insertions (1) Whether a BST is well-balanced or ill-balanced depends on the order of insertions. If the inserted elements are randomly ordered, the BST will probably be reasonably well-balanced. If the inserted elements happen to be in ascending (or descending) order, the BST will be extremely ill-balanced.
30
10-30 Initially: Example 1: successive insertions (1) Animation (inserting ‘lion’, ‘fox’, ‘rat’, ‘cat’, ‘pig’, ‘dog’, ‘tiger’): After inserting ‘lion’: lion After inserting ‘fox’: fox lion After inserting ‘rat’: fox lion rat After inserting ‘cat’: cat fox lion rat After inserting ‘pig’: cat fox lion pig rat After inserting ‘dog’: dog cat fox lion pig rat After inserting ‘tiger’: dog cat fox lion pig rat tiger
31
10-31 Example 1 (2) Animation (inserting ‘cat’, ‘dog’, ‘fox’, ‘lion’, ‘pig’, ‘rat’): Initially:After inserting ‘cat’: cat After inserting ‘dog’: dog cat After inserting ‘fox’: dog cat fox After inserting ‘lion’: dog cat fox lion After inserting ‘pig’: dog cat fox lion pig After inserting ‘rat’: dog cat fox lion pig rat
32
10-32 0 6 4 2 BSTs in practice: insertions (2) The following trials show the results of loading a BST with n randomly-generated elements. First trial with n = 35:
33
10-33 0 6 4 2 8 BSTs in practice: insertions (3) Second trial with n = 35:
34
10-34 BST deletion Deleting a subtree’s leftmost element. Deleting a subtree’s topmost element. Deleting an arbitrary given element in a BST.
35
10-35 Deleting a leftmost element (1) Problem: Delete the leftmost element in a subtree. Two cases to consider: 1)The subtree’s topmost node has no left child. 2)The subtree’s topmost node has a left child. Note: By definition, the leftmost node has no left child.
36
10-36 Deleting a leftmost element (2) Case 1 (topmost node has no left child): Replace the subtree by its own right subtree. E.g.: leftmost node After: pig rat tiger top top’ lion Before: lion pig rat tiger top
37
10-37 Deleting a leftmost element (3) Case 2 (topmost node has a left child): Link the leftmost node’s parent to the leftmost node’s right child. E.g.: garbage leftmost node Before: cat fox lion pig rat tiger top After: cat lion pig rat tigerfox top top’
38
10-38 Deleting a leftmost element (4) Algorithm: To delete the leftmost element in the (nonempty) subtree whose topmost node is top: 1.If top has no left child: 1.1.Terminate with top’s right child as answer. 2.If top has a left child: 2.1.Set parent to top, and set curr to top’s left child. 2.2.While node curr has a left child, repeat: 2.2.1.Set parent to curr, and set curr to curr’s left child. 2.3.Set parent’s left child to curr’s right child. 2.4.Terminate with top as answer. case 1 case 2
39
10-39 Deleting a leftmost element (5) Implementation as a Java method (in class BSTNode ): private BSTNode deleteLeftmost () { if (this.left == null) return this.right; else { BSTNode parent = this, curr = this.left; while (curr.left != null) { parent = curr; curr = curr.left; } parent.left = curr.right; return this; } }
40
10-40 Deleting a topmost element (1) Problem: Delete the topmost element in a subtree. Four cases to consider: 1)The topmost node has no children. 2)The topmost node has a right child but no left child. 3)The topmost node has a left child but no right child. 4)The topmost node has two children.
41
10-41 Deleting a topmost element (2) Case 1 (topmost node has no children): Replace the subtree by an empty subtree. E.g.: Before: lion top After: top top’ lion
42
10-42 Deleting a topmost element (3) Case 2 (topmost node has a right child but no left child): Replace the subtree by its own right subtree. E.g.: Before: lion pig rat tiger top After: pig rat tiger top top’ lion
43
10-43 Deleting a topmost element (4) Case 3 (topmost node has a left child but no right child): Replace the subtree by its own left subtree. E.g.: Before: lion cat fox goat top After: cat fox goat top top’ lion
44
10-44 Deleting a topmost element (5) Case 4 (topmost node has two children): Copy the right subtree’s leftmost element into the topmost node, then delete the right subtree’s leftmost element. E.g.: garbage Before: fox cat lion rat tiger pig top After: fox cat pig rat tiger pig top top’
45
10-45 Deleting a topmost element (6) Algorithm: To delete the topmost element in the subtree whose topmost node is top: 1.If top has no left child: 1.1.Terminate with top’s right child as answer. 2.If top has no right child: 2.1.Terminate with top’s left child as answer. 3.If top has two children: 3.1.Set top’s element to the leftmost element in top’s right subtree. 3.2.Delete the leftmost element in top’s right subtree. 3.3.Terminate with top as answer. cases 1, 2 cases 1, 3 case 4
46
10-46 Deleting a topmost element (7) Auxiliary algorithm: To determine the leftmost element in the (nonempty) subtree whose topmost node is top: 1.Set curr to top. 2.While curr has a left child, repeat: 2.1.Set curr to curr’s left child. 3.Terminate with curr’s element as answer.
47
10-47 Deleting a topmost element (8) Implementation as a Java method (in class BSTNode ): public BSTNode deleteTopmost () { if (this.left == null) return this.right; else if (this.right == null) return this.left; else { // this node has two children this.element = this.right.getLeftmost(); this.right = this.right.deleteLeftmost(); return this; } }
48
10-48 Deleting a topmost element (9) Auxiliary method (in class BSTNode ): private Comparable getLeftmost () { BSTNode curr = this; while (curr.left != null) curr = curr.left; return curr.element; }
49
10-49 Deleting a given element (1) BST deletion algorithm: To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null: 2.1.1.Terminate. 2.2.Otherwise, if elem is equal to curr’s element: 2.2.1.Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree. 2.2.2.Replace the link to curr by del. 2.2.3.Terminate. 2.3.Otherwise, …
50
10-50 Deleting a given element (2) BST deletion algorithm (continued): 2.3.Otherwise, if elem is less than curr’s element: 2.3.1.Set parent to curr, and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element: 2.4.1.Set parent to curr, and set curr to curr’s right child.
51
10-51 Deleting a given element (3) Animation: To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: … 2.3.Otherwise, if elem is less than curr’s element, set parent to curr, and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr, and set curr to curr’s right child. root fox lion fox elem dog rat pigtiger To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: … 2.3.Otherwise, if elem is less than curr’s element, set parent to curr, and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr, and set curr to curr’s right child. root fox lion fox elem dog rat pigtiger parent curr To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: … 2.3.Otherwise, if elem is less than curr’s element, set parent to curr, and set curr to curr’s left child. 2.4.Otherwise, if elem is greater than curr’s element, set parent to curr, and set curr to curr’s right child. root fox lion fox elem dog rat pigtiger parent curr To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: 2.2.1.Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree. 2.2.2.Replace the link to curr by del. 2.2.3.Terminate. 2.3.Otherwise, … root fox lion fox elem dog rat pigtiger parent curr To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: 2.2.1.Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree. 2.2.2.Replace the link to curr by del. 2.2.3.Terminate. 2.3.Otherwise, … root fox lion fox elem dog rat pigtiger parent curr del To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: 2.2.1.Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree. 2.2.2.Replace the link to curr by del. 2.2.3.Terminate. 2.3.Otherwise, … root fox lion fox elem dog rat pigtiger parent curr del To delete the element elem in a BST: 1.Set parent to null, and set curr to the BST’s root node. 2.Repeat: 2.1.If curr is null, terminate. 2.2.Otherwise, if elem is equal to curr’s element: 2.2.1.Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree. 2.2.2.Replace the link to curr by del. 2.2.3.Terminate. 2.3.Otherwise, … root fox lion fox elem dog rat pigtiger
52
10-52 Deleting a given element (4) Implementation as a Java method (in class BST ): public void delete (Comparable elem) { int direction = 0; BSTNode parent = null, curr = root; for (;;) { if (curr == null) return; direction = elem.compareTo(curr.element); if (direction == 0) { BSTNode del = curr.deleteTopmost(); if (curr == root) root = del; else if (curr == parent.left) parent.left = del; else parent.right = del; return; }
53
10-53 Deleting a given element (5) Implementation (continued): parent = curr; if (direction 0 curr = parent.right; } }
54
10-54 BSTs in practice: deletions Whether a BST is well-balanced or ill-balanced depends on the order of insertions and deletions. Deletion can make a well-balanced BST ill-balanced, or vice versa.
55
10-55 Example 2: successive deletions Animation (deleting ‘lion, ‘fox’, ‘pig’): Initially: fox cat lion rat tiger dog pig After deleting ‘lion’: fox cat pig rat tiger dog pig garbage After deleting ‘fox’: fox cat pig rat tiger dog garbage After deleting ‘pig’: cat rat tiger dog garbage
56
10-56 Binary tree traversal Binary tree traversal: Visit all nodes (elements) of the tree in some predetermined order. During a binary tree traversal, we must visit the root node, traverse the left subtree, and traverse the right subtree. But in which order? In-order traversal: Traverse the left subtree, then visit the root node, then traverse the right subtree. Pre-order traversal: Visit the root node, then traverse the left subtree, then traverse the right subtree. Post-order traversal: Traverse the left subtree, then traverse the right subtree, then visit the root node.
57
10-57 Binary tree in-order traversal (1) Schematic:
58
10-58 Binary tree in-order traversal (2) Illustration: fox cat lion rat tiger dog pig In-order traversal of a BST visits the elements in ascending order.
59
10-59 Binary tree in-order traversal (3) Binary tree in-order traversal algorithm (generic): To traverse, in in-order, the subtree whose topmost node is top: 1.If top is not null: 1.1.Traverse, in in-order, top’s left subtree. 1.2.Visit top. 1.3.Traverse, in in-order, top’s right subtree. 2.Terminate.
60
10-60 Example 3: printing elements in order Java method: public static void printInOrder (BSTNode top) { // Print, in ascending order, all the elements in the BST subtree // whose topmost node is top. if (top != null) { printInOrder(top.left); System.out.println(top.element); printInOrder(top.right); } } visit top
61
10-61 Binary tree pre-order traversal (1) Schematic:
62
10-62 Binary tree pre-order traversal (2) Illustration: fox cat lion rat tiger dog pig
63
10-63 Binary tree pre-order traversal (3) Binary tree pre-order traversal algorithm (generic): To traverse, in pre-order, the subtree whose topmost node is top: 1.If top is not null: 1.1.Visit top. 1.2.Traverse, in pre-order, top’s left subtree. 1.3.Traverse, in pre-order, top’s right subtree. 2.Terminate.
64
10-64 Binary tree post-order traversal (1) Schematic:
65
10-65 Binary tree post-order traversal (2) Illustration: fox cat lion rat tiger dog pig
66
10-66 Binary tree post-order traversal (3) Binary tree post-order traversal algorithm (generic): To traverse, in post-order, the subtree whose topmost node is top: 1.If top is not null: 1.1.Traverse, in post-order, top’s left subtree. 1.2.Traverse, in post-order, top’s right subtree. 1.3.Visit top. 2.Terminate.
67
10-67 Implementation of sets using BSTs (1) Represent an (unbounded) set by a BST whose elements are the set members. Empty set: Invariant: Illustration: CA MX US represents the set {CA, MX, US}
68
10-68 Implementation of sets using BSTs (2) Note: The BST representation of a set is not unique: BE DE represents the set {BE, DE, FR, IT, LU, NL} IT LU NL FR BE DE represents the set {BE, DE, FR, IT, LU, NL} LU NL IT FR
69
10-69 Implementation of sets using BSTs (3) Summary of algorithms and time complexities: OperationAlgorithmTime complexity contains BST searchO(log n)best O(n)worst add BST insertionO(log n)best O(n)worst remove BST deletionO(log n)best O(n)worst
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.