280 likes | 450 Views
Lecture 8 The Tree Data Structure ctd. BSTs in Java Traversal. Implementation of sets using BSTs. Deleting a subtree’s leftmost element. Deleting a subtree’s topmost element. Deleting an arbitrary given element in a BST. BST in Java. A node of a BST of Strings: class BSTNode {
E N D
Lecture 8The Tree Data Structure ctd • BSTs in Java • Traversal. • Implementation of sets using BSTs. • Deleting a subtree’s leftmost element. • Deleting a subtree’s topmost element. • Deleting an arbitrary given element in a BST.
BST in Java A node of a BST of Strings: class BSTNode { public String element; public BSTNode parent, lftChld, rgtChld; public BSTNode(BSTNode pt, String e) { parent = pt; element = e; lftChld = null; rgtChld = null; } } More generally: class BSTNode<E>for a BST of objects of type E. (Use E in place of String above)
BST in Java • A BST of Strings: public class BST { private BSTNode root; //The header //Constructor: public BST(BSTNoder) { root = r; } //Accessor methods ... } • More generally: class BST<E extends Comparable<E>>
BST in Java -- add public void add(String e) { BSTNode parent = null, curr = root; while (curr != null) { if (e.compareTo(curr.element) == 0) //2.2: already there: nothing to do return; else if (e.compareTo(curr.element) < 0) { //2.3 parent = curr; curr = curr.lftChld; } else if (e.compareTo(curr.element) > 0) { //2.4 parent = curr; curr = curr.rgtChld; } }
BST in Java -- add BSTNodenewNode = new BSTNode(parent, e); //2.1 if (parent == null) //curr is at root root = newNode; else if (e.compareTo(parent.element) < 0) parent.lftChld = newNode; else //e > parent.element parent.rgtChld = newNode; }
BST in Java - search //The binary search algorithm. BSTNode<E> bSearch(E target) { BSTNode<E> curr = root; while (curr != null) { if (target.compareTo(curr.element) == 0) return curr; else if (target.compareTo(curr.element) < 0) curr = curr.lftChld; else if (target.compareTo(curr.element) > 0) curr = curr.rgtChld; } return null; } Download MyBST.zip to see a full implementation
Binary tree traversal • Binary tree traversal: Visit all nodes (elements) of the tree in some predetermined order. • 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.
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. • This algorithm is generic: the meaning of “Visit …” is left open.
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.
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.
Empty set: Illustration: MX represents the set {CA, MX, US} CA US Implementation of sets using BSTs (1) • Represent an (unbounded) set by a BST whose elements are the set members.
FR represents the set {BE, DE, FR, IT, LU, NL} DE LU BE IT NL IT represents the set {BE, DE, FR, IT, LU, NL} BE LU DE NL FR Implementation of sets using BSTs (2) • The BST representation of a set is not unique:
Implementation of sets using BSTs (3) • Summary of algorithms and time complexities:
Deleting a leftmost element (1) • Problem: Delete the leftmost element in a subtree. • Two cases to consider: • The subtree’s topmost node has no left child. • The subtree’s topmost node has a left child. • Note: By definition, the leftmost node has no left child.
Before: lion rat pig tiger After: lion rat pig tiger Deleting a leftmost element (2) • Case 1 (topmost node has no left child): Discard the topmost node, but retain its right subtree. E.g.: leftmost node garbage
Before: lion rat cat fox pig tiger After: lion rat cat fox pig tiger 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.: leftmost node garbage
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
Deleting a topmost element (1) • Problem: Delete the topmost element in a subtree. • Four cases to consider: • The topmost node has no children. • The topmost node has a right child but no left child. • The topmost node has a left child but no right child. • The topmost node has two children.
Before: lion After: lion Deleting a topmost element (2) • Case 1 (topmost node has no children): Make the subtree empty. E.g.: garbage
Before: lion rat pig tiger After: lion rat pig tiger Deleting a topmost element (3) • Case 2 (topmost node has a right child but no left child):Discard the topmost node, but retain its right subtree. E.g.: garbage
lion Before: fox cat goat lion After: fox cat goat Deleting a topmost element (4) • Case 3 (topmost node has a left child but no right child):Discard the topmost node, but retain its left subtree. E.g.: garbage
Before: lion rat fox cat tiger pig After: pig rat fox cat tiger pig 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
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
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.
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, …
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.
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, … 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. 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. 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, … 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, … 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. 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, … lion lion lion lion lion lion lion elem elem elem elem elem elem elem fox fox fox fox fox fox fox root root root root root root root parent parent parent parent parent fox fox fox fox fox fox fox rat rat rat rat rat rat rat curr curr curr curr curr del del dog dog dog dog dog dog dog pig pig pig pig pig pig pig tiger tiger tiger tiger tiger tiger tiger Deleting a given element (3) • Animation: