Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fa 2016 Ji Seaman 1 Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except root) has exacty exacty one edge coming into it. - Every node can have any number of edges going out of it (zero or more). Parent: source node of directed edge Chid: termina node of directed edge Binary Tree: a tree in which no node can have more than two chidren. 2 Tree Traversas: exampes Binary Trees: impementation Structure with a data vaue, and a pointer to the eft subtree and another to the right subtree. Preorder: print node vaue, process eft tree, then right + + a * b c * + * d e f g Postorder: process eft tree, then right, then print node vaue a b c * + d e * f + g * + Inorder: process eft tree, print node vaue, then process right tree struct TreeNode { int vaue; // the data TreeNode *eft; // eft subtree TreeNode *right; // right subtree ; Like a inked ist, but two next pointers. There is aso a specia pointer to the root node of the tree (ike head for a ist). TreeNode *root; a + b * c + d * e + f * g 3 4
Binary Search Trees Binary Search Trees: operations A specia kind of binary tree, used for efficient searching, insertion, and deetion. Binary Search Tree property: For every node X in the tree: - A the vaues in the eft subtree are smaer than the vaue at X. - A the vaues in the right subtree are arger than the vaue at X. Not a binary trees are binary search trees An inorder traversa of a BST shows the vaues in insert(x) remove(x) (or deete) isempty() (returns boo) - if the root is NULL find(x) findmin() (or search, returns boo) (returns <type>) - Smaest eement is found by aways taking the eft branch. findmax() (returns <type>) - Largest eement is found by aways taking the right branch. sorted order 5 6 BST: find(x) BST: find(x) Defined iterativey: boo IntBinaryTree::searchNode(int num) { TreeNode *p = root; Agorithm: if we are searching for 15 we are done. If we are searching for a key < 15, then we shoud search in the eft subtree. If we are searching for a key > 15, then we shoud search in the right subtree. 7 whie (p) { if (p->vaue == num) return true; ese if (num < p->vaue) p = p->eft; ese p = p->right; return fase; Can aso be defined recursivey 8
BST: insert(x) Agorithm is simiar to find(x) If x is found, do nothing (no dupicates in tree) If x is not found, add a new node with x in pace of the ast empty subtree that was searched. Inserting 13: 9 Recursive function BST: insert(x) root is passed by reference to this function void IntBinaryTree::insert(TreeNode *&nodeptr, int num) { if (nodeptr == NULL) { // Create a new node and store num in it, // making nodeptr point to it nodeptr = new TreeNode; nodeptr->vaue = num; nodeptr->eft = nodeptr->right = NULL; ese if (num < nodeptr->vaue) insert(nodeptr->eft, num); // Search the eft branch ese if (num > nodeptr->vaue) insert(nodeptr->right, num); // Search the right branch // ese nodept->vaue == num, do nothing, no dupicates 10 BST: remove(x) BST: remove(x) Agorithm is starts with finding(x) Case 1: Node is a eaf If x is not found, do nothing If x is found, remove node carefuy. - Must remain a binary search tree (smaers on eft, biggers on right). - The agorithm is described here in the ecture, the code is in the book (and on cass website in BinaryTree.zip) - Can be removed without vioating BST property Case 2: Node has one chid - Make parent pointer bypass the Node and point to that chid Does not matter if the chid is the eft or right chid of deeted node 11 12
BST: remove(x) Case 3: Node has 2 chidren - Find minimum node in right subtree --cannot have eft subtree, or it s not the minimum - Move origina node s eft subtree to be the eft subtree of this node. - Make origina node s parent pointer bypass the origina node and point to right subtree find minimum of 2 s right subtree (3) and move 2 s eft subtree (1) to be it s new eft. Remove 2 (make 6 point to 5). Binary heap data structure A binary heap is a specia kind of binary tree - has a restricted structure (must be compete) - has an ordering property (parent vaue is smaer than chid vaues) - NOT a Binary Search Tree! Used in the foowing appications - Priority queue impementation: supports enqueue and deetemin operations in O(og N) - Heap sort: another O(N og N) sorting agorithm. 13 14 Binary Heap: structure property Compete binary tree: a tree that is competey fied - every eve except the ast is competey fied. - the bottom eve is fied eft to right (the eaves are as far eft as possibe). Compete Binary Trees A compete binary tree can be easiy stored in an array - pace the root in position 1 (for convenience) 15 16
Compete Binary Trees Properties In the array representation: - put root at ocation 1 - use an int variabe (size) to store number of nodes - for a node at position i: - eft chid at position 2i (if 2i <= size, ese i is eaf) - right chid at position 2i+1 (if 2i+1 <= size, ese i is eaf) - parent is in position foor(i/2) (or use integer division) There is a heap impementation on the cass website in Heap.zip 17 Binary Heap: ordering property In a heap, if X is a parent of Y, vaue(x) is ess than or equa to vaue(y). - the minimum vaue of the heap is aways at the root. 18 Heap: insert(x) Heap: insert(x) First: add a node to tree. - must be paced at next avaiabe ocation, size+1, in order to maintain a compete tree. Next: maintain the ordering property: - if x doesn t have a parent: done - if x is greater than its parent: done - ese swap with parent, repeat Caed percoate up or reheap up preserves ordering property 19 20
Heap: deetemin() Heap: deetemin() Minimum is at the root, removing it eaves a hoe. - The ast eement in the tree must be reocated. First: move ast eement up to the root Next: maintain the ordering property, start with root: - if no chidren, do nothing. - if one chid, swap with parent if it s smaer than the parent. - if both chidren are greater than the parent: done - otherwise, swap the smaer of the two chidren with the parent, and repeat on that chid. Caed percoate down or reheap down preserves ordering property 21 22