210 likes | 325 Views
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.
E N D
11Binary 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. • 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.
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.
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):
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):
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).
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; }}
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 (1) • Schematic for in-order traversal:
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.
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.
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).
Binary tree pre-order traversal (1) • Schematic for pre-order traversal:
lion rat fox cat tiger pig dog Binary tree pre-order traversal (2) • Illustration:
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 (1) • Schematic for post-order traversal:
lion rat fox cat tiger pig dog Binary tree post-order traversal (2) • Illustration:
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: