Download presentation
Presentation is loading. Please wait.
1
Podcast Ch18a Title: Overview of Binary Search Trees
Description: Building a Tree; scanning a tree; removing nodes Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp
2
Binary Search Tree The nodes in a binary search tree are on unique paths from the root according to an ordering principle. The left subtree of a node contains smaller values and the right subtree contains larger values. This implies that duplicates are not allowed.
3
Building a Binary Search Tree
Insertion strategy: If the value of the new element is equal to the value of the current node, perform no action. If the value of the new element is less than the value of the current node, proceed to the left subtree (left child) of the node. If null, insert the node as a left child of the parent. If the value of the new element is greater than the value of the current node, proceed to the right subtree (right child) of the node. If null, insert the node as a left child of the parent.
4
Building a Binary Search Tree (continued)
5
Student Question Draw the binary search tree after inserting into the empty tree: F D G T M A C P Someone wanted to be helpful and sorted the data before insertion. Draw the binary search tree after inserting A B C D E F
6
Practice Problem Draw the binary search tree after inserting into the empty tree: Draw the binary search tree after inserting into the empty tree M L K J I H
7
Locating an Object in a Binary Search Tree
Search for an element the same way you inserted it.
8
Student Question Given the tree on the previous slide, what nodes are visited when searching for 53? When searching for 36?
9
Practice Problem Given the tree on the previous slide, what nodes are visited when searching for 62? When searching for 8?
10
Inorder Scan of a Binary Search Tree
The first value visited in the minimum value in the tree. The LNR inorder scan visits the nodes in ascending order. The RNL inorder scan visits the nodes in descending order.
11
Removing a Binary Search Tree Node
Note immediately that the root node cannot be removed from the following tree since we would be left with two orphaned subtrees. We need to find a replacement value for 25.
12
Removing a Binary Search Tree Node (continued)
To delete a node from a binary search tree, you must find a replacement node, copy its value, and then splice it out of the tree.
13
Student Question The examples so far show integer data or character data in a BST? Is this how BSTs are normally used?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.