1 / 27

Lecture 8 The Tree Data Structure ctd

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 {

xue
Download Presentation

Lecture 8 The Tree Data Structure ctd

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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)

  3. 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>>

  4. 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; } }

  5. 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; }

  6. 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

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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:

  13. Implementation of sets using BSTs (3) • Summary of algorithms and time complexities:

  14. 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.

  15. 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

  16. 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

  17. 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

  18. 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.

  19. Before: lion After: lion Deleting a topmost element (2) • Case 1 (topmost node has no children): Make the subtree empty. E.g.: garbage

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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.

  25. 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, …

  26. 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.

  27. 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:

More Related