Download presentation
Presentation is loading. Please wait.
1
CS 240: Data Structures Monday, July 30 th Binary Search Trees
2
What are binary search trees? Binary search trees are a data structure where each node has two links – compared to linked list which has 1. Binary search trees are a data structure where each node has two links – compared to linked list which has 1. Generally, the links are called left and right. Generally, the links are called left and right. Node *left; Node *left; Node *right; Node *right; These, alternatively, could be stored in a list/vector of type “Node *” contained in each node These, alternatively, could be stored in a list/vector of type “Node *” contained in each node
3
Rules of the BST A binary search tree has rules that govern how insertion works. A binary search tree has rules that govern how insertion works. Smaller data goes on the left, larger/equal data goes on the right. Smaller data goes on the left, larger/equal data goes on the right. We will refer to the first node as root. We will refer to the first node as root.
4
5 Root NULL Initialize TreeInsert 5 Root NULL Insert 7 5 Root NULL 7 Insert 3 5 Root 7 NULL 3
5
Rules, cont The insertion rule is important to the functionality of the BST The insertion rule is important to the functionality of the BST This is what allows us to search in O(lg n) time This is what allows us to search in O(lg n) time Removal is a little trickier Removal is a little trickier
6
Removing nodes If a node has 1 or less children (next nodes) it is similar to how we’ve done it with linked list If a node has 1 or less children (next nodes) it is similar to how we’ve done it with linked list If left==right==NULL, the parent points to NULL (instead of the node we remove) If left==right==NULL, the parent points to NULL (instead of the node we remove) If left or right != NULL, but the other is NULL, the parent points to the non-NULL value If left or right != NULL, but the other is NULL, the parent points to the non-NULL value If both are != NULL, find the smallest value on the right subtree. Copy that value into our node. Remove “smallest value” from the right subtree. If both are != NULL, find the smallest value on the right subtree. Copy that value into our node. Remove “smallest value” from the right subtree. This form of remove will call itself with a different “target” value This form of remove will call itself with a different “target” value
7
Traversal There are three types of traversal which allow us to see the data in the tree There are three types of traversal which allow us to see the data in the tree Preorder: NLR Preorder: NLR Inorder: LNR Inorder: LNR Postorder: LRN Postorder: LRN N = look at node’s data N = look at node’s data L = go left L = go left R = go right R = go right These traversals indicate the order we perform these three operations These traversals indicate the order we perform these three operations
8
5 Root 7 NULL 3 Preorder - NLR Starting at root Starting at root 5 3 7 NLR: 5LR: 5 NLR: 3LR: 3 Nothing R: 3 Nothing 3 R: 5 NLR: 7LR: 7 Nothing R: 7 Nothing 7 5 Done
9
5 Root 7 NULL 3 Inorder - LNR Starting at root Starting at root 3 5 7 LNR: 5 LNR: 3 Nothing NR: 3R: 3 Nothing 3 NR: 5R: 5 LNR: 7 Nothing NR: 7R: 7 Nothing 7 5Done
10
5 Root 7 NULL 3 Postorder - LRN Starting at root Starting at root 3 7 5 LRN: 5 Nothing LRN: 3RN: 3 Nothing N: 33 RN: 5 LRN: 7 Nothing RN: 7 Nothing N: 77 N: 55Done
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.