Download presentation
Presentation is loading. Please wait.
1
The Tree Data Structure
Binary trees and binary search trees Insertion. Searching. 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
Trees and their applications
For large amounts of data the linear access time of linked lists is prohibitive. Trees have a access time of O(log n) for most operations. Trees are used to implement the file system of several popular operating systems. Decision trees Family trees Many other applications
3
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. header root node D B A E C F G leaf node
4
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.
5
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. D B A E C F G A’s left subtree A’s right subtree B’s right subtree B’s left subtree is empty C’s left subtree C’s right subtree
6
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.
7
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.
8
Node and tree depths (2) Illustrations: balanced ill-balanced depth 0
F (a) balanced ill-balanced M K N P L J (b) depth 0 depth 1 depth 2 depth 3
9
Node and tree depths (3) Illustrations (continued): very ill-balanced
W X depth 4 depth 5 depth 3 depth 0 depth 1 depth 2 very ill-balanced
10
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. Nodes at depth d have no children (by definition). A binary tree of depth 0 or 1 is always balanced.
11
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.
12
Binary search trees (2) Illustrations: (a) dog cat fox lion pig rat
tiger (b) pig cat dog fox lion rat tiger
13
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.
14
BST insertion (1) 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.
15
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: If curr is null: 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 Terminate Otherwise, if elem is equal to curr’s element: Terminate.
16
BST insertion (3) BST insertion algorithm (continued):
2.3. Otherwise, if elem is less than curr’s element: Set parent to curr, and set curr to curr’s left child Otherwise, if elem is greater than curr’s element: Set parent to curr, and set curr to curr’s right child.
17
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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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
18
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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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 To insert the element elem into a BST: 1. Set parent to null, and set curr to the BST’s root. 2. Repeat: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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: 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 Otherwise, if elem is equal to curr’s element, terminate Otherwise, if elem is less than curr’s element, set parent to curr and set curr to curr’s left child 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
19
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 = int(log2 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).
20
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.
21
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: If curr is null: Terminate with answer none Otherwise, if target is equal to curr’s element: Terminate with answer curr Otherwise, if target is less than curr’s element: Set curr to curr’s left child Otherwise, if target is greater than curr’s element: Set curr to curr’s right child.
22
BST search (3) Animation (successful search): root dog cat fox lion
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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child Otherwise, if target is greater than curr’s element, set curr to curr’s right child. root dog cat fox lion pig rat tiger 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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 target
23
BST search (4) Animation (unsuccessful search): root dog cat fox lion
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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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: If curr is null, terminate with answer none Otherwise, if target is equal to curr’s element, terminate with answer curr Otherwise, if target is less than curr’s element, set curr to curr’s left child 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
24
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(log2 n): Max. no. of comparisons = floor(log2 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).
25
Construct a BST from these lists;
John, Jenny, Lewis, Lee, Jane, Jaz, Bill, Tom, Jim, Vinny, Kelly, Sam Ganguly, Vijay, Sehwag, Dravid, Tendulkar, Laxman, Dhoni, Harbhajan, Zaheer, Mishra, I Sharma
26
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)
27
BST in Java A BST of Strings:
public class BST { private BSTNode root; //The header //Constructor: public BST(BSTNode r) { root = r; } //Accessor methods ... } More generally: class BST<E extends Comparable<E>>
28
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 curr = curr.rgtChld;
29
BST in Java -- add if (parent == null) //curr is at root
BSTNode newNode = 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; }
30
BST in Java - search Download MyBST.zip to see a full implementation
//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
31
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.
32
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: Traverse, in in-order, top’s left subtree Visit top Traverse, in in-order, top’s right subtree. 2. Terminate. This algorithm is generic: the meaning of “Visit …” is left open.
33
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: Visit top Traverse, in pre-order, top’s left subtree Traverse, in pre-order, top’s right subtree. 2. Terminate.
34
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: Traverse, in post-order, top’s left subtree Traverse, in post-order, top’s right subtree Visit top. 2. Terminate.
35
Implementation of sets using BSTs (1)
Represent an (unbounded) set by a BST whose elements are the set members. Empty set: Illustration: CA MX US represents the set {CA, MX, US}
36
Implementation of sets using BSTs (2)
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
37
Implementation of sets using BSTs (3)
Summary of algorithms and time complexities: Operation Algorithm Time complexity contains BST search O(log n) best O(n) worst add BST insertion remove BST deletion
38
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.
39
Deleting a leftmost element (2)
Case 1 (topmost node has no left child): Discard the topmost node, but retain its right subtree. E.g.: Before: lion pig rat tiger leftmost node After: lion pig rat tiger garbage
40
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.: Before: cat fox lion pig rat tiger leftmost node After: cat lion pig rat tiger fox garbage
41
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: Terminate with top’s right child as answer. 2. If top has a left child: Set parent to top, and set curr to top’s left child While node curr has a left child, repeat: Set parent to curr, and set curr to curr’s left child Set parent’s left child to curr’s right child Terminate with top as answer. case 1 case 2
42
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.
43
Deleting a topmost element (2)
Case 1 (topmost node has no children): Make the subtree empty. E.g.: Before: lion garbage After: lion
44
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.: Before: lion pig rat tiger garbage After: lion pig rat tiger
45
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.: Before: lion cat fox goat After: lion cat fox goat garbage
46
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.: Before: fox cat lion rat tiger pig After: fox cat pig rat tiger garbage
47
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: Terminate with top’s right child as answer. 2. If top has no right child: Terminate with top’s left child as answer. 3. If top has two children: Set top’s element to the leftmost element in top’s right subtree Delete the leftmost element in top’s right subtree Terminate with top as answer. cases 1, 2 cases 1, 3 case 4
48
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: Set curr to curr’s left child. 3. Terminate with curr’s element as answer.
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: If curr is null: Terminate Otherwise, if elem is equal to curr’s element: Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree Replace the link to curr by del Terminate Otherwise, …
50
Deleting a given element (2)
BST deletion algorithm (continued): 2.3. Otherwise, if elem is less than curr’s element: Set parent to curr, and set curr to curr’s left child Otherwise, if elem is greater than curr’s element: Set parent to curr, and set curr to curr’s right child.
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: If curr is null, terminate Otherwise, if elem is equal to curr’s element: Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree Replace the link to curr by del Terminate Otherwise, … root fox lion elem dog rat pig tiger To delete the element elem in a BST: 1. Set parent to null, and set curr to the BST’s root node. 2. Repeat: If curr is null, terminate Otherwise, if elem is equal to curr’s element: Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree Replace the link to curr by del Terminate Otherwise, … root fox lion elem dog rat pig tiger 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: If curr is null, terminate Otherwise, if elem is equal to curr’s element: Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree Replace the link to curr by del Terminate Otherwise, … root fox lion elem dog rat pig tiger 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: If curr is null, terminate Otherwise, if elem is equal to curr’s element: … Otherwise, if elem is less than curr’s element, set parent to curr, and set curr to curr’s left child Otherwise, if elem is greater than curr’s element, set parent to curr, and set curr to curr’s right child. root fox lion elem dog rat pig tiger 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: If curr is null, terminate Otherwise, if elem is equal to curr’s element: … Otherwise, if elem is less than curr’s element, set parent to curr, and set curr to curr’s left child Otherwise, if elem is greater than curr’s element, set parent to curr, and set curr to curr’s right child. root fox lion elem dog rat pig tiger To delete the element elem in a BST: 1. Set parent to null, and set curr to the BST’s root node. 2. Repeat: If curr is null, terminate Otherwise, if elem is equal to curr’s element: … Otherwise, if elem is less than curr’s element, set parent to curr, and set curr to curr’s left child Otherwise, if elem is greater than curr’s element, set parent to curr, and set curr to curr’s right child. root fox lion elem dog rat pig tiger 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: If curr is null, terminate Otherwise, if elem is equal to curr’s element: Delete the topmost element in the subtree whose topmost node is curr, and let del be a link to the resulting subtree Replace the link to curr by del Terminate Otherwise, … root fox lion elem dog rat pig tiger parent curr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.