Docsity

Binary Search Trees: Finding the Minimal Element, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Binary Search Trees: Finding the Minimal Element and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ Binary Search Trees^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science โ€” University of San Francisco โ€“ p.1/

8-0:^ BST Minimal Element

To find the minimal element in a BST:^ Base Case: When is it easy to find the smallest element ina BST?^ Recursive Case: How can we make the problem smaller?How can we use the solution to the smaller problem tosolve the original problem?

Department of Computer Science โ€” University of San Francisco โ€“ p.2/

8-2:^ BST Minimal Element

To find the minimal element in a BST:Base Case:^ When is it easy to find the smallest element in a BST?^ When the left subtree is empty, then the elementstored at the root is the smallest element in the tree.

Department of Computer Science โ€” University of San Francisco โ€“ p.4/

8-3:^ BST Minimal Element

To find the minimal element in a BST:Recursive Case:^ How can we make the problem smaller?

Department of Computer Science โ€” University of San Francisco โ€“ p.5/

8-5:^ BST Minimal Element

To find the minimal element in a BST:Recursive Case:^ How can we make the problem smaller?^ Both the left and right subtrees are smaller versions ofthe same problem^ How can we use the solution to a smaller problem to solvethe original problem?^ The smallest element in the left subtree is the smallestelement in the tree

Department of Computer Science โ€” University of San Francisco โ€“ p.7/

8-6:^ BST Minimal Element

Object^ minimum(Node

tree)^ { if (tree ==^ null)return null;if (tree.left()^ ==

null)return tree.element(); elsereturn minimum(tree.left());}

Department of Computer Science โ€” University of San Francisco โ€“ p.8/

8-8: Inserting into BST

What is the base case โ€“ an easy tree to insert an elementinto?

Department of Computer Science โ€” University of San Francisco โ€“ p.10/

8-9: Inserting into BST

What is the base case โ€“ an easy tree to insert an elementinto?^ An empty tree^ Create a new tree, containing the element

Department of Computer Science โ€” University of San Francisco โ€“ p.11/

8-11: Inserting into BST

Recursive Case: How do we make the problem smaller?^ The left and right subtrees are smaller versions of thesame problem.^ How do we use these smaller versions of the problem?

Department of Computer Science โ€” University of San Francisco โ€“ p.13/

8-12: Inserting into BST

Recursive Case: How do we make the problem smaller?^ The left and right subtrees are smaller versions of thesame problem^ Insert the element into the left subtree if

value stored at the root, and insert the element into the right^ ^ subtree if^ value stored at the root

Department of Computer Science โ€” University of San Francisco โ€“ p.14/

8-14:^ Tree Manipulation in Java

Tree manipulation functions return trees Insert method takes as input the old tree and the elementto insert, and returns the new tree, with the elementinserted^ Old value (pre-insertion) of tree will be destroyed To insert an element^ e

into a tree^ T: T = insert(T, e);^ Department of Computer Science โ€” University of San Francisco โ€“ p.16/

8-15: Inserting into BST

Node^ insert(Node^

tree,^ Object^ elem) {if (tree == null) {return new Node(elem);if (elem < tree.element()))^ { tree.setLeft(insert(tree.left(),

elem)); return tree;} else {tree.setRight(insert(tree.right(),

elem)); return tree;} }

Department of Computer Science โ€” University of San Francisco โ€“ p.17/

8-17:^ Deleting From a BST

Removing a leaf:^ Remove element immediately Removing a node with one child:^ Just like removing from a linked list^ Make parent point to child Removing a node with two children:^ Replace node with largest element in left subtree, orthe smallest element in the right subtree

Department of Computer Science โ€” University of San Francisco โ€“ p.19/

8-18:^ Deleting from a BST

Key: we need to find the right element to replace thedeleted element. We can find the minimal element in the right subtree^ Why will this work? Or the maximal element in the left subtree^ Why will this work?

Department of Computer Science โ€” University of San Francisco โ€“ p.20/