Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1
Binary tree root parent child nonterminal node (terminal node) leaf Data Structures and Algorithms in Java, Third EditionCh06 – 2
Binary search tree el < el≥ el Data Structures and Algorithms in Java, Third EditionCh06 – 3
Node implementation public class BSTNode<T extends Comparable > { protected T el; protected BSTNode left, right; // constructors } el abbreviated as: Data Structures and Algorithms in Java, Third EditionCh06 – 4
Insertion in BST: algorithm insert(el ) p = root; prev = null; while p is not null // find a place for inserting new node; prev = p; if element in node p < el p = p.right; else p = p.left; if root is null // tree is empty; root becomes a new node with el; else if element in node prev < el new node with el is attached to the right of prev; else new node with el is attached to the left of prev; Data Structures and Algorithms in Java, Third EditionCh06 – 5
20 Insertion in the BST: example null Data Structures and Algorithms in Java, Third EditionCh06 – 6
Insertion in the BST: example (cont’d) Data Structures and Algorithms in Java, Third EditionCh06 – 7
Searching in the BST: algorithm search(el) p = root; while p is not null if element in node p equals el return element in node p; else if element in node p < el p = p.right; else p = p.left; return null; // el was not found; Data Structures and Algorithms in Java, Third EditionCh06 – 8
Searching in the BST: example success failure Data Structures and Algorithms in Java, Third EditionCh06 – 9
Breadth-first traversal: algorithm public void breadthFirst() { BSTNode p = root; Queue > queue = new Queue >(); if (p != null) { queue.enqueue(p); while (!queue.isEmpty()) { p = queue.dequeue(); visit(p); if (p.left != null) queue.enqueue(p.left); if (p.right != null) queue.enqueue(p.right); }
Breadth-first traversal: example 30 iteration queuep init Data Structures and Algorithms in Java, Third EditionCh06 – 11
Depth-first traversals: algorithms Three tasks: V ‑ visiting a node L ‑ traversing the left subtree R ‑ traversing the right subtree The three tasks can be executed in 3! = 6 different orders: left to rightright to left preorder inorder postorder VLR LVR LRV VRL RVL RLV Data Structures and Algorithms in Java, Third EditionCh06 – 12
public void preorder() { preorder(root); } protected void preorder(BSTNode p) { if (p != null) { visit(p); preorder(p.left); preorder(p.right); } Depth-first traversals: implementation Data Structures and Algorithms in Java, Third EditionCh06 – 13
Depth-first traversals: example preorder inorder postorder Data Structures and Algorithms in Java, Third EditionCh06 – 14
Deletion by copying 1. Deleting a leaf 2. Deleting a node with one descendant Data Structures and Algorithms in Java, Third EditionCh06 – 15
Deletion by copying (cont’d) 3. Deleting a node with two descendants A BA Data Structures and Algorithms in Java, Third EditionCh06 – 16
Deletion by copying: example delete 30 delete Data Structures and Algorithms in Java, Third EditionCh06 – 17
Rotations rotateRight(G,P,C) if P is not the root of the tree // i.e., if G is not null 1.grandparent G of child C becomes C ’s parent by replacing P; 2.right subtree of C becomes left subtree of C ’s parent P; 3.node C acquires P as its right child ; Data Structures and Algorithms in Java, Third EditionCh06 – 18
Step-by-step right rotation XY Z G P C XY Z G P C XY Z G P C XY Z G P C 2. right subtree of C becomes left subtree of C’s parent P; 3. node C acquires P as its right child; G C P YZ X = 1. grandparent G of child C becomes C’s parent by replacing P;
Left rotation YZ X G P C Y Z X G P C Data Structures and Algorithms in Java, Third EditionCh06 – 20
Self-adjusting trees Single rotation: Rotate a child about parent if an element in the child is accessed unless it is the root. Moving to the root: Repeat the child‑parent rotation until the element being accessed is in the root. Data Structures and Algorithms in Java, Third EditionCh06 – 21
Single rotation technique: example Data Structures and Algorithms in Java, Third EditionCh06 – 22
Expression trees (5 + 6) * 5 preorder inorder postorder +5 * 67 * * * + preorder inorder postorder * * * ** Data Structures and Algorithms in Java, Third EditionCh06 – 23
Processing an expression in postfix notation * Data Structures and Algorithms in Java, Third EditionCh06 – 24
(max) heaps 1.the value of each node is greater than or equal to the values stored in each of its children, 2.the tree is perfectly balanced and the leaves in in the last level are all in the leftmost positions. Data Structures and Algorithms in Java, Third EditionCh06 – 25
A binary tree that violates the first heap condition: A binary tree that violates the second heap condition: Heaps: counterexamples Data Structures and Algorithms in Java, Third EditionCh06 – 26
heap binary search tree el ≤ el el < el≥ el Data Structures and Algorithms in Java, Third EditionCh06 – 27
parentleft child right child …. i2∙i +12∙i +2 Data Structures and Algorithms in Java, Third EditionCh06 – 28
Heap as a priority queue: enqueuing heapEnqueue(el) put el at the end of heap ; while el is not in the root and el > parent( el ) swap el with its parent ; Data Structures and Algorithms in Java, Third EditionCh06 – 29
Series of enqueuings empty
heapDequeue() extract the element from the root ; put the element from the last leaf in its place ; remove the last leaf ; // both subtrees of the root are heaps p = the root ; while p is not a leaf and p < any of its children swap p with the larger child ; Heap as a priority queue: dequeuing Data Structures and Algorithms in Java, Third EditionCh06 – 31
Series of dequeuings Data Structures and Algorithms in Java, Third EditionCh06 – 32
Series of dequeuings (cont’d) Data Structures and Algorithms in Java, Third EditionCh06 – 33
Organizing arrays as heaps FloydAlgorithm(data[]) for i = index of the last nonleaf, down to 0 p = data[i]; while p is not a leaf and p < any of its children swap p with the larger child ; Data Structures and Algorithms in Java, Third EditionCh06 – 34
Organizing arrays as heaps: example last nonleaf Data Structures and Algorithms in Java, Third EditionCh06 – 35