0% found this document useful (0 votes)
4 views16 pages

CSC312 -- Lecture 5b

This document provides an overview of trees in data structures, focusing on binary search trees (BSTs) and their operations such as search, insertion, and deletion. It explains the properties of BSTs, including the parent-child relationship and the process of traversing the tree to visit keys in order. Additionally, it outlines the three cases for deleting nodes from a BST and discusses tree traversal methods for printing a binary tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

CSC312 -- Lecture 5b

This document provides an overview of trees in data structures, focusing on binary search trees (BSTs) and their operations such as search, insertion, and deletion. It explains the properties of BSTs, including the parent-child relationship and the process of traversing the tree to visit keys in order. Additionally, it outlines the three cases for deleting nodes from a BST and discusses tree traversal methods for printing a binary tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Trees – Part II

The CSC312 Team


Reference Textbook for this Topic
Title
Data Structures and Algorithms in C++,
Second Edition
John Wiley & Sons

Authors
Michael T. Goodrich, Roberto Tamassia, and David M.
Mount.

2
Recap – What are Trees?

 A tree is a graph without cycles


 In software systems, a tree is an abstract model of a hierarchical
structure
 Compared with “linear” data structures
 A tree consists of nodes with a parent-child relation

3
Binary Search Trees

 A binary search tree is a binary tree storing keys (or key-value


entries) at its internal nodes and satisfying the following
property:
 Let 𝑢, 𝑣, and 𝑤 be three nodes such that 𝑢 is in the left subtree of 𝑣 and
w is in the right subtree of 𝑣.
 We have 𝑘𝑒𝑦(𝑢)  𝑘𝑒𝑦(𝑣)  𝑘𝑒𝑦(𝑤)
 External (Leaf) nodes do not store items

4
 An inorder traversal of a binary search trees visits the keys in
increasing order

2 9

1 4 8

Binary Search Trees – Sample Tree 5


Search

 To search for a key k, we trace a downward path starting at the


root
 The next node visited depends on the comparison of k with the
key of the current node
 If we reach a leaf, the key is not found
 Recursive

6
Algorithm TreeSearch(k, v)
if v.isExternal ()
return v
if k < v.key()
return TreeSearch(k, v.left())
else if k = v.key()
return v
else { k > v.key() }
return TreeSearch(k, v.right())

Call TreeSearch(4,root) 7
Insertion

 To perform operation put(k, o), we search for key k (using


TreeSearch)

 Assume k is not already in the tree, and let w be the leaf


reached by the search

 We insert k at node w and expand w into an internal node


8
Insertion Example: insert(5) 9
Deletion

 To perform operation erase(k), we search for key k


 Assume key k is in the tree, and let v be the node storing k
 Basic method
 removeAboveExternal(w): removes w and its parent
 If node v has a leaf child w, we remove v and w from the tree
with removeAboveExternal(w).

10
Deletion: Three Cases
Case 1: Deleting Node “5”
 If the node has no children, then
release the memory occupied by this
node.
 The pointer that originally points to
this child is set to NULL. The
figure on the slide illustrates this
scenario

11
Deletion: Three Cases
Case 2: Deleting Node “14”

 If the node has only one child, then


the node’s parent points to the
node’s only child and releases the
memory occupied by the node

12
What about “remove 1”?

Case 2 example: remove 4 13


Deletion: Three Cases
Case 3: Deleting Node “3”
 Key k to be removed is stored at a node v whose children are
both internal.
1. Find the internal node w that follows v in an inorder traversal
(find the smallest w larger than v)
2. Copy key(w) into node v
3. Remove node w and its left child z (which must be a leaf) by means of
operation removeExternal(z)
 Why left child z?

14
Example: remove 3 15
Printing a Binary Tree

 The operation is based on tree traversals, remember the


following algorithms from the previous lecture?
 Pre-order
 Post-order
 In-order
 Level-order

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy