Download presentation
Presentation is loading. Please wait.
1
CS 261 – Data Structures Binary Search Trees
2
Binary Tree: Useful Collection?
Can we make a collection using the idea of a binary tree? How about starting with an implementation of a BAG Goal: make operations proportional to the length of a path, rather than the number of elements in the tree Implication must know which side of a node a given value will/should be on First step Binary Search Tree
3
Binary Search Tree Binary search trees are binary trees where every node’s object value is: Greater than all its descendents in the left subtree Less than or equal to all its descendents in the right subtree In-order traversal returns elements in sorted order If tree is reasonably full (well balanced), searching for an element is O(log n)
4
Binary Search Tree: Example
Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur
5
Binary Search Tree (BST): Implementation
struct BSTree { struct Node *root; int cnt; }; void initBSTree(struct BSTree *tree); void addBSTree(struct BSTree *tree, TYPE val); int containsBSTree(struct BSTree *tree, TYPE val); void removeBSTree(struct BSTree *tree, TYPE val); int sizeBSTree(struct BSTree *tree);
6
Binary Node: Reminder struct Node { TYPE val;
struct Node *left; /* Left child. */ struct Node *rght; /* Right child. */ };
7
Bag Implementation: Contains
Start at root At each node, compare value to node value: Return true if match If value is less than node value, go to left child (and repeat) If value is greater than node value, go to right child (and repeat) If node is null, return false Traverses a path from the root to the leaf Therefore, if tree is reasonably full (an important if), execution time is O( ?? )
8
BST: Contains/Find Example
Agnes Alex Object to find Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur
9
Bag Implemention: Add Do the same type of traversal from root to leaf
When you find a null value, create a new node Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur
10
Before first call to add
BST: Add Example Aaron Alex Object to add Abner Angela Abigail Adela Alice Audrey Aaron Adam Agnes Allen Arthur “Aaron” should be added here Before first call to add
11
BST: Add Example Ariel Alex Abner Angela Abigail Adela Alice Audrey
Next object to add Abner Angela Abigail Adela Alice Audrey Aaron Adam Agnes Allen Arthur “Ariel” should be added here Ariel After first call to add
12
Useful Trick A useful trick (adapted from the functional programming world): Secondary routine that returns tree with the value inserted Node addNode(Node current, TYPE value) if current is null then return new Node with value otherwise if value < Node.value left child = addNode(left child, value) else right child = addNode(right child, value) return current node
13
Add: Calls Utility Routine
void add(struct BSTree *tree, TYPE val) { tree->root = _addNode(tree->root, val); tree->cnt++; } What is the complexity? O( ?? )
14
Bag Implementation: Remove
Remove is most complex Leaves a “hole” What value should fill the hole? Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur
15
BST: Remove Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes
Allen Arthur How would you remove Abigail? Audrey? Angela?
16
Who fills the hole? Answer: the leftmost child of the right child (smallest element in right subtree) Try this on a few values Useful to have a couple of private inner routines: TYPE _leftmost(struct Node *cur) { ... /* Return value of leftmost child of current node. */ } struct Node *_removeLeftmost(struct Node *cur) { ... /* Return tree with leftmost child removed. */
17
BST: Remove Example Alex Abner Angela Abigail Adela Alice Audrey Adam
Element to remove Alex Replace with: leftmost(rght) Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur Before call to remove
18
BST: Remove Example Alex Abner Arthur Abigail Adela Alice Audrey Adam
Agnes Allen After call to remove
19
Special Case What if you don’t have a right child?
Try removing “Audrey” Think about it Can just return left child Angela Alice Audrey Allen Arthur
20
Remove: Easy if you return a tree
Node removeNode(Node current, TYPE value) if value = current.value if right child is null return left child else replace value with leftmost child of right child set right child to be removeLeftmost(right) else if value < current.value left child = removeNode(left child, value) else right child = removeNode(right child, value) return current node
21
Complexity Running down a path from root to leaf
What is the complexity? O( ?? ) Questions?? Now the worksheet
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.