CS235102 Data Structures Chapter 5 Trees Additional Binary Tree Operations (1/7) Copying Binary Trees we can modify the postorder traversal algorithm only slightly to copy the binary tree similar as Program 5.3 Additional Binary Tree Operations (2/7) Testing Equality Binary trees are equivalent if they have the same topology and the information in corresponding nodes is identical V L R the same topology and data as Program 5.6 Additional Binary Tree Operations (3/7) Variables: x1, x2, …, xn can hold only of two possible values, true or false Operators: (and), (or), ¬(not) Propositional Calculus Expression A variable is an expression If x and y are expressions, then ¬x, xy, xy are expressions Parentheses can be used to alter the normal order of evaluation (¬ > > ) Example: x1 (x2 ¬x3) Additional Binary Tree Operations (4/7) Satisfiability problem: Is there an assignment to make an expression true? Solution for the Example x1 (x2 ¬x3) : If x1 and x3 are false and x2 is true false (true ¬false) = false true = true For n value of an expression, there are 2n possible combinations of true and false Additional Binary Tree Operations (5/7) (x1 ¬x2) (¬ x1 x3) ¬x3 postorder traversal data value X1 X3 X2 X3 X1 Additional Binary Tree Operations (6/7) node structure For the purpose of our evaluation algorithm, we assume each node has four fields: We define this node structure in C as: Additional Binary Tree Operations (7/7) Satisfiability function To evaluate the tree is easily obtained by modifying the original recursive postorder traversal TRUE node TRUE FALSE FALSE TRUE TRUE T TRUE TRUE FALSE F F FALSE FALSE T TRUE T L R V Threaded Binary Trees (1/10) Threads Do you find any drawback of the above tree? Too many null pointers in current representation of binary trees n: number of nodes number of non-null links: n-1 total links: 2n null links: 2n-(n-1) = n+1 Solution: replace these null pointers with some useful “threads” Threaded Binary Trees (2/10) Rules for constructing the threads If ptr->left_child is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal If ptr->right_child is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal Threaded Binary Trees (3/10) A Threaded Binary Tree t: true thread f: false child root A dangling f B f dangling C t E t D F G inorder traversal: H I H D I B E A F C G Threaded Binary Trees (4/10) Two additional fields of the node structure, left-thread and right-thread If ptr->left-thread=TRUE, then ptr->left-child contains a thread; Otherwise it contains a pointer to the left child. Similarly for the right-thread Threaded Binary Trees (5/10) If we don’t want the left pointer of H and the right pointer of G to be dangling pointers, we may create root node and assign them pointing to the root node Threaded Binary Trees (6/10) Inorder traversal of a threaded binary tree By using of threads we can perform an inorder traversal without making use of a stack (simplifying the task) Now, we can follow the thread of any node, ptr, to the “next” node of inorder traversal 1. If ptr->right_thread = TRUE, the inorder successor of ptr is ptr->right_child by definition of the threads 2. Otherwise we obtain the inorder successor of ptr by following a path of left-child links from the right-child of ptr until we reach a node with left_thread = TRUE Threaded Binary Trees (7/10) Finding the inorder successor (next node) of a node threaded_pointer insucc(threaded_pointer tree){ threaded_pointer temp; temp = tree->right_child; if (!tree->right_thread) while (!temp->left_thread) temp = temp->left_child; return temp; } Inorder tree temp Threaded Binary Trees (8/10) Inorder traversal of a threaded binary tree void tinorder(threaded_pointer tree){ /* traverse the threaded binary tree inorder */ threaded_pointer temp = tree; output: H D I B E A FC G for (;;) { temp = insucc(temp); if (temp==tree) tree break; printf(“%3c”,temp->data); } } Time Complexity: O(n) Threaded Binary Trees (9/10) Insertion of Threaded Binary Tree Insert child as the right child of node parent root A parent A B B parent child X C X child C D First Case E F Second Case Threaded Binary Trees (10/10) Right insertion in a threaded binary tree void insert_right(thread_pointer parent, threaded_pointer child){ /* insert child as the right child of parent in a threaded binary tree */ threaded_pointer temp; root child->right_child = parent->right_child; parent child->right_thread = parent->right_thread; A child->left_child = parent; B child->left_thread = TRUE; X C child parent->right_child = child; parent->right_thread = FALSE; temp If(!child->right_thread){ parent A temp = insucc(child); child B temp->left_child = child; X } C } D First Case Second Case E successor F Heaps (1/6) The heap abstract data type Definition: A max(min) tree is a tree in which the key value in each node is no smaller (larger) than the key values in its children. A max (min) heap is a complete binary tree that is also a max (min) tree Basic Operations: creation of an empty heap insertion of a new elemrnt into a heap deletion of the largest element from the heap Heaps (2/6) The examples of max heaps and min heaps Property: The root of max heap (min heap) contains the largest (smallest) element Heaps (3/6) Abstract data type of Max Heap Heaps (4/6) Queue in Chapter 3: FIFO Priority queues Heaps are frequently used to implement priority queues delete the element with highest (lowest) priority insert the element with arbitrary priority Heaps is the only way to implement priority queue machine service: amount of time (min heap) amount of payment (max heap) factory: time tag Heaps (5/6) Insertion Into A Max Heap Analysis of insert_max_heap The complexity of the insertion function is O(log2 n) insert 2 51 *n= 6 5 i= 1 6 7 3 [1] 20 21 [2] 15 [4] parent sink item upheap [3] [5] 14 10 20 52 [6] [7] 2 5 Deletion from a max heap Heaps (6/6) After deletion, the heap is still a complete binary tree Analysis of delete_max_heap The complexity of the insertion function is O(log2 n) parent = 4 1 2 child = 8 2 [1] 4 [2] < 15 20 [3] 15 14 [4] *n= 5 4 [5] 14 10 10 2 item.key = 20 temp.key = 10 Binary Search Trees (1/8) Why do binary search trees need? Heap is not suited for applications in which arbitrary elements are to be deleted from the element list a min (max) element is deleted O(log2n) deletion of an arbitrary element O(n) search for an arbitrary element O(n) Definition of binary search tree: Every element has a unique key The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree The left and right subtrees are also binary search trees Binary Search Trees (2/8) Example: (b) and (c) are binary search trees medium smaller larger Binary Search Trees (3/8) Search: Search(25) Search(76) 44 17 88 65 32 28 97 54 29 82 76 80 Binary Search Trees (4/8) Searching a binary search tree O(h) Binary Search Trees (5/8) Inserting into a binary search tree An empty tree Binary Search Trees (6/8) Deletion from a binary search tree Three cases should be considered case 1. leaf delete case 2. one child delete and change the pointer to this child case 3. two child either the smallest element in the right subtree or the largest element in the left subtree Binary Search Trees (7/8) Height of a binary search tree The height of a binary search tree with n elements can become as large as n. It can be shown that when insertions and deletions are made at random, the height of the binary search tree is O(log2n) on the average. Search trees with a worst-case height of O(log2n) are called balance search trees Binary Search Trees (8/8) Time Complexity Searching, insertion, removal O(h), where h is the height of the tree Worst case - skewed binary tree O(n), where n is the # of internal nodes Prevent worst case rebalancing scheme AVL, 2-3, and Red-black tree Selection Trees (1/7) Problem: suppose we have k order sequences, called runs, that are to be merged into a single ordered sequence Solution: straightforward : k-1 comparison selection tree : log2k+1 There are two kinds of selection trees: winner trees and loser trees Selection Trees (2/7) Definition: (Winner tree) a selection tree is the binary tree where each node represents the smaller of its two children root node is the smallest node in the tree a winner is the record with smaller key Rules: tournament : between sibling nodes put X in the parent node X tree where X = winner or loser Winner Tree Selection Trees (3/7) sequential allocation scheme (complete binary tree) Each node represents the smaller of its two children ordered sequence Selection Trees (4/7) Analysis of merging runs using winner trees # of levels: log2K +1 restructure time: O(log2K) merge time: O(nlog2K) setup time: O(K) merge time: O(nlog2K) Slight modification: tree of loser consider the parent node only (vs. sibling nodes) Selection Trees (5/7) After one record has been output 6 6 6 6 15 Selection Trees (6/7) Tree of losers can be conducted by Winner tree 0 6 8 9 10 17 20 9 90 Selection Trees (7/7) The loser tree after output 6 0 1 Overall winner 8 9 2 15 17 4 10 run 20 10 9 1 2 9 10 5 6 11 20 15 3 4 12 3 9 90 8 9 5 6 13 14 7 15 90 17 7 8