Binary Search Trees BST CSE 3318 Algorithms and
Binary Search Trees (BST) CSE 3318 – Algorithms and Data Structures Alexandra Stefan Includes materials from Dr. Bob Weems University of Texas at Arlington 5/18/2021 1
Search Trees • "search tree" as a term does NOT refer to a specific implementation. • All search trees support: search, insert and delete. • The term refers to a family of implementations, that may have different properties. • Insertions and deletions can differ among trees, and have important implications on overall performance. • We will discuss: – Binary search trees (BST). – 2 -3 -4 trees (a special type of a Btree). – nd mention: red-black trees, AVL trees, splay trees, B-trees and other variations. • The main goal is to have insertions and deletions that: – Are efficient (at most logarithmic time). – Leave the tree balanced, to support efficient search (at most logarithmic time). 2
Binary Search Tree (BST) • Resources: – BST in general – CLRS – BST in general and solved problems: http: //cslibrary. stanford. edu/110/Binary. Trees. html#s – Insertion at root (using insertion at a leaf and rotations) • Sedgewick • Dr. Bob Weems: Notes 11, parts: ‘ 11. D. Rotations’ and ’ 11. E. Insertion At Root’ – Randomizing the tree by inserting at a random position – not covered • Sedgewick 3
Tree Properties - Review • Full tree • Complete tree (e. g. heap tree) • Perfect binary tree • Alternative definitions: complete (for perfect) and almost complete or nearly complete (complete). • Tree – connected graph with no cycles, or connected graph with N-1 edges (and N vertices). 4
Binary Search Trees • Definition: a binary search tree is a binary tree where the item at each node is: – Greater than or equal to all items on the left subtree. – Less than all items in the right subtree. (Use ≤ to create more balanced trees with duplicate values. ) • How do we search? – 30? 44? 40 23 15 37 52 44 5
Example 1 • What values could the empty leaf have? 40 40 15 52 44 6
Example 1 • What values could the empty leaf have? • Only value 40 40 40 15 40 52 44 7
Example 1 • If you change direction twice, (go to A, left child, and then go to X, right child) all the nodes in the subtree rooted X will be in the range [A, B]. B 40 A 40 52 15 15 40 52 X 44 44 A≤X≤B 8
Range of possible values • As we travel towards node 50, we find the interval of possible values in the tree rooted at 50 to be: [40, 70] The search for 50 gave the sequence: 120, 70, 40, 50 X ≤ 120 20≤ X ≤ 120 E. g. of impossible search sequence for 50 in a BST: 120, 70 , 80, 50 20≤X≤ 70 40≤X≤ 70 The range is now [20, 70] and so 80 is impossible (all nodes in the tree at 70 will be in the [20, 70] range). 9 (tree image: Dr. Bob Weems: Notes 11, parts: ‘ 11. C. Binary Search Trees’ )
Properties • Where is the item with the smallest key? • Where is the item with the largest key? • What traversal prints the data in increasing order? – How about decreasing order? Consider the special cases where the root has: - No left child - No right child 10 (tree image: Dr. Bob Weems: Notes 11, parts: ‘ 11. C. Binary Search Trees’ )
Predecessor and Successor (according to key order) • When the node has the child you need. • When the node does NOT have he child you need. Here, in the nodes, the first number is the item/key and the second number is the tree size. Root has key 120 and size 21 (the whole tree has 21 nodes). Node Predecessor Successor 120 70 160 * 130 * 50 * 180 * * 11 (tree image: Dr. Bob Weems: Notes 11, parts: ‘ 11. C. Binary Search Trees’ )
Predecessor and Successor (according to key order) • Successor of node x with key k (go right): – Smallest node in the right subtree – Special case: no right subtree: first parent to the right • Predecessor of node x with key k (go left): – Largest node in the left subtree – Special case: no left subtree: first parent to the left Node Predecessor Successor 120 110 130 70 60 80 170 160 180 160 *170 60 * 50 130 *120 50 *40 180 *170 *70 12 (tree image: Dr. Bob Weems: Notes 11, parts: ‘ 11. C. Binary Search Trees’ )
• Min: leftmost node (from the root keep going left) – Special case: no left child => root • Max: rightmost node (from the root keep going right. – Special case: no right child => root • Print in order: – Increasing: Left, Root, Right (inorder traversal) – Decreasing: Right, Root, Left • Successor of node x with key k (go right): – Smallest node in the right subtree – Special case: no right subtree: first parent to the right • Predecessor of node x with key k (go left): – Largest node in the left subtree – Special case: no left subtree: first parent to the left 13
Binary Search Trees - Search node. PT search(node. PT tree, int s_data) { if (tree == null) return null; else if (s_data == tree->data) return tree; else if (s_data < tree->data) return search(tree->left, s_data); else return search(tree->right, s_data); } Runtime (in terms of , N, number of nodes in the tree or tree height) 15 – Best case: – Worst case: 40 23 37 52 44 14
Naïve Insertion To insert an item, the simplest approach is to travel down in the tree until finding a leaf position where it is appropriate to insert the item. node. PT insert(node. PT h, int n_data) if (h == null) return new_tree(n_data); else if (n_data < h->data) h->left = insert(h->left, n_data); else if (n_data > h->data) h->right = insert(h->right, n_data); return h; How will we call this method? root = insert(root, data) 40 23 15 52 37 44 39 Note that we use: h->left = insert(h->left, data) to handle the base case, where we return a new node, and the parent must make this new node a child. 15
Performance of BST • Are these trees valid BST? 15 • Give two sequences of nodes s. t. when inserted in an empty tree will produce the two trees shown here (each sequence produces a different tree). 23 37 40 23 15 37 40 52 44 44 52 16
Performance of BST • Are these trees valid BST? 15 – Yes • Give two sequences of nodes s. t. when inserted in an empty tree will produce the two trees shown here (each sequence produces a different tree). 23 – 40, 23, 37, 52, 44, 15 – 15, 23, 37, 40, 44, 52 40 23 15 37 37 Search, Insert and Delete take time linear to the height of the tree (worst). 52 44 Ideal: build and keep a balanced tree • insertions and deletions should leave the tree balanced. 40 44 52 17
Performance of BST • If items are inserted in: – ascending order, the resulting tree is maximally imbalanced. – random order, the resulting trees are reasonably balanced. • Can we insert the items in random order? – If we build the tree from a batch of items. • Shuffle them first, or grab them from random positions. – If they come online (we do not have them all as a batch). • Insert in the tree at a random position – see Sedgewick textbook 18
BST - Rotations • Left and right rotations (image source: Dr. Bob Weems: Notes 11, parts: ‘ 11. D. Rotations’ ) // Sedgewick code: // rotate to the right node. PT rot. R(node. PT B) { node. PT A = B->left; B->left = A->right; A->right = B; return A; } // rotate to the left node. PT rot. L(node. PT B) { node. PT C = B->right; B->right = C->left; C->left = B; 19 return C; }
BST – Insertion at Root (small changes to Sedgewick code) node. PT rot. R(node. PT h) { node. PT x = h->left; h->left = x->right; x->right = h; return x; } node. PT rot. L(node. PT h) { node. PT x = h->right; h->right = x->left; x->left = h; return x; } ----node. PT insert. T(node. PT h, int data) { if (h == NULL) return new_tree(data, NULL, 1); if (data < h->data) { h->left = insert. T(h->left, data); h = rot. R(h); } else { h->right = insert. T(h->right, data); h = rot. L(h); } return h; } void STinsert(int data) { head = insert. T(head, data); } // Sedgewick code adaptation 20
BST - Deletion Delete a node, z, in a BST • If z is a leaf, delete it, • If z has only one child, replace z with the child • If z has 2 children, replace it with its order-wise successor, y, and delete old y. (Note: y will be a leaf or have only one child. ) 1. Method 1 (Simple: copy the data) 1. 2. Copy the data from y to z Delete node y. 3. Problem if other components of the program maintain pointers to nodes in the tree they would not know that the tree was changed and their data cannot be trusted anymore. 2. Method 2 (move the nodes) 1. 2. 3. 4. Replaces the node (not content) z with node y in the tree. Delete node z (y is now linked in place of z) Does not have the pointer referencing problem. 2 implementations: Sedgewick and CLRS. 21
BST – Deletion – Method 1 (Copy the data) Delete(z) - delete a node z in a BST - Method 1. 1. If z is a leaf, delete it 2. If z has only one child, delete it and readjust the links (the child ‘moves’ in the place of z). 3. If z has 2 children: a) Find the successor, y, of z. 1. Where is the successor of z? b) Copy only the data from y to z c) Call Delete(y) node y. Note that y can only be: 1. 2. Leaf (case 1 above) A node with only one child (the right child) (This is case 2 above. ) 22
BST – Deletion – Method 2 (Move nodes) Delete a node z in a BST - Method 2. 1. If z is a leaf, delete it 2. If z has only one child, delete it and readjust the links (the child ‘moves’ in the place of z). 3. If z has 2 children, find the successor, y, of z. Is y the right child of z? a) YES: Transplant y over z (y will have only the right child) b) NO: Draw image 23
- Slides: 23