1 / 21

1 1 Binary Tree Data Structures

1 1 Binary Tree Data Structures. Binary trees and binary search trees. Searching. Traversal. Implementation of sets using BSTs. BST search (1). Problem: Search for a given target value in a BST. Idea: Compare the target value with the element in the root node.

Download Presentation

1 1 Binary Tree Data Structures

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. 11Binary Tree Data Structures • Binary trees and binary search trees. • Searching. • Traversal. • Implementation of sets using BSTs.

  2. BST search (1) • Problem: Search for a given target value in a BST. • Idea: Compare the target value with the element in the root node. • If the target value is equal, the search is successful. • If target value is less, search the left subtree. • If target value is greater, search the right subtree. • If the subtree is empty, the search is unsuccessful.

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

  4. 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. 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. 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. 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. 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. lion lion lion lion lion target target target target target pig pig pig pig pig root root root root root fox fox fox fox fox rat rat rat rat rat curr curr curr curr cat cat cat cat cat pig pig pig pig pig tiger tiger tiger tiger tiger dog dog dog dog dog BST search (3) • Animation (successful search):

  5. 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. 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. 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. 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. 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. lion lion lion lion lion target target target target target goat goat goat goat goat root root root root root fox fox fox fox fox rat rat rat rat rat curr curr curr curr cat cat cat cat cat pig pig pig pig pig tiger tiger tiger tiger tiger dog dog dog dog dog BST search (4) • Animation (unsuccessful search):

  6. 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(log2n): Max. no. of comparisons = floor(log2n) + 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).

  7. 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) returnnull; direction =target.compareTo(curr.element);if (direction == 0) return curr;elseif (direction < 0) curr = curr.left;else curr = curr.right; }}

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

  9. Binary tree in-order traversal (1) • Schematic for in-order traversal:

  10. lion rat fox cat tiger pig dog Binary tree in-order traversal (2) • Illustration: • In-order traversal of a BST visits the elements in ascending order.

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

  12. Example: printing elements in order • Java method: publicstaticvoid 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 (printing its element).

  13. Binary tree pre-order traversal (1) • Schematic for pre-order traversal:

  14. lion rat fox cat tiger pig dog Binary tree pre-order traversal (2) • Illustration:

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

  16. Binary tree post-order traversal (1) • Schematic for post-order traversal:

  17. lion rat fox cat tiger pig dog Binary tree post-order traversal (2) • Illustration:

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

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

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

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

More Related