Binary Search Tree A binary search tree (BST) is a binary tree that, for each node n: The elements in the left subtree are less than the element in n The elements in the right subtree are greater than or equal to the element in n This definition extends the basic binary tree to allow insertions and deletions The elements must have an inherent ordering (be Comparable) Binary Search Tree 45 12 7 69 15 14 51 81 42 33 25 42 38 70 44 Adding an Element To add an element: Follow the appropriate path until it ends and add the new node at that point Add the following elements to an initially empty tree: 38 60 87 24 56 19 45 59 22 38 24 60 19 56 22 45 87 59 Finding an Element To find an element: Starting at the root, compare elements, then move into the left or right subtree as appropriate Eventually the target will be found or the end of the path will be reached For a generally balanced tree, about half of the remaining elements are eliminated with each comparison Degenerate Trees A degenerate tree is one which is highly unbalanced 23 28 35 33 40 43 57 Defeats the purpose of efficient search Removing an Element After removing an element, the result must still be a valid BST Removing a node may disconnect parts of the tree Three distinct situations: 1. The node to remove is a leaf 2. The node to remove has one child 3. The node to remove has two children The first two of these are easy to deal with Removing an Element 51 Situation 3 28 72 25 36 18 11 64 33 24 30 49 39 37 Situation 1 68 44 34 41 77 76 85 74 50 Situation 2 Removing an Element A leaf can simply be deleted A node with one child is simply bypassed, "pulling up" its one subtree A node with two children can be replaced with its inorder successor or its inorder predecessor Inorder successor - greatest element in the left subtree Inorder predecessor - least element in the right subtree Either will have at most one child Efficiency The cost of each operation is a function of the number of nodes it must access For a balanced tree, these operations are logarithmic, or O(log N) The worst case (a fully degenerate tree) is O(N) If all permutations are equally likely, the average case is O(N log N) Various strategies exist for keeping a BST balanced
© Copyright 2026 Paperzz