0% found this document useful (0 votes)
8 views

CS211 7 Trees

The document provides an overview of trees as a data structure, detailing their properties, terminologies, and classifications. It explains the importance of trees in computing, including their applications in hierarchical representations and efficient algorithms. Additionally, the document covers binary trees, their implementations, and various tree traversal methods.

Uploaded by

d865629w2z
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CS211 7 Trees

The document provides an overview of trees as a data structure, detailing their properties, terminologies, and classifications. It explains the importance of trees in computing, including their applications in hierarchical representations and efficient algorithms. Additionally, the document covers binary trees, their implementations, and various tree traversal methods.

Uploaded by

d865629w2z
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Algorithms & Data Structures

CS211

Trees

Lectures
13, 14 & 15

Samar Alsaleh
Spring 2022
04

03 Finishing Thoughts
Summary & resources

02 Binary Search Tree (BST)


Properties, operations, & implementation

Today’s 01 Binary Trees


Introduction, implementations, and traversal

Agenda Introduction to Trees


Trees as data structures: characteristics and
terminologies
Today’s 01

Agenda Introduction to Trees


Trees as data structures: characteristics and
terminologies
A Di erent Way to Structure Data
- So far we’ve considered linear data structures such as arrays, linked lists, stacks, …
- In linear data structures, data elements are arranged in sequen al manner and each
element has one successor and one predecessor (except the rst and last)
- We now consider a hierarchical (non-linear) data structure where each node may have
two successors, each of which which may have two successors, and so on
- Examples of non-linear data structures are Tree, BST, Graphs etc.
- Let’s start with trees…

4
ff
fi
ti
Trees as a Data Structure

- A tree is a non-linear structure in which elements are


organized into a hierarchy
- This data structure is called “tree” because it resembles
actual trees 🌳 (upside down)
- A tree data structure is comprised of:
- a set of nodes in which elements are stored
- edges that connect one node to another
- It starts with a root node and branch o
with its
descendants, and nally, there are leaves

5
fi
ff
Why Trees?
- Trees are very important data structures in compu ng
- They are suitable for:
- Hierarchical structure representa on, e.g.,
- File directory
- Organiza onal structure of an ins tu on
- Class inheritance tree
- Problem representa on, e.g.,
- Expression tree
- Decision tree
-E cient algorithmic solu ons, e.g.,
- Search trees
- E cient priority queues via heaps

6
ffi
ffi
ti
ti
ti
ti
ti
ti
ti
Trees Proper es

5
A tree sa s es the following proper es:
3 2
- It has one designated node, called the root, that
4 1 6 has no parent

Tree - Every node, except the root, has exactly one


parent
5 5
1
- A node may have zero or more children
3 2
3 2 - There is a unique directed path from the root to
4 6 4 1 6
each node

Not a Tree
7
ti
fi
ti
ti
Trees Terminologies

- Nodes at the lower level of a tree are the


children of nodes at the previous level
- Nodes that have the same parent are
siblings
- A node that has no children is a leaf node
- A subtree is a tree structure that makes
up part of another tree
- We can follow a path through a tree from
parent to child, star ng at the root

8
ti
Trees Terminologies (cont.)

Level of node B is 1 - The path length is the number of edges


Height of node B is 2 followed to get from the root to the node
- Level (or depth) of a node v: The length of
the path from the root to v
- Height of a node v: The length of the
Height

Depth
longest path from v to a leaf node
- The height of a tree is the height of its root
node (the length of the longest path from
the root to a leaf)
- By de ni on, the height of an empty tree
is -1

9
fi
ti
Trees Terminologies (cont.)
- Degree: The number of subtrees (or
Ordered tree of children) of a node
size is 10 - Each of node A, B, C, and D has degree 2
- Node E has degree 1
- Each of node H, I, J, F, and G has degree 0
- Leaf: A node with degree 0
- Internal or interior node: a node with
degree greater than 0
- Size: The number of nodes in a tree
- Ordered tree: A tree in which the children
of each node are linearly ordered (usually
from le to right)
10
ft
Trees Terminologies (cont.)
Ancestors of - Ancestor of a node v: Any node, including
node G v itself, on the path from the root to the
Proper node
Ancestors of
Defendants of node G - Proper ancestor of a node v: Any node,
node D excluding v, on the path from the root to
the node
- Descendant of a node v: Any node,
including v itself, on any path from the
node to a leaf node
- Proper descendant of a node v: Any
Proper Defendants node, excluding v, on any path from the
of node D
node to a leaf node
11
Trees Classi ca ons
- Trees can be classi ed in many ways
- One important criterion is the maximum number of children any node in the tree may have
- This may be referred to as the order of the tree
- General trees have no limit to the number of children a node may have
- A tree that limits each node to no more than n children is referred to as an n-ary tree
- A tree where each node had n sub-trees, any of which may be empty trees

Quaternary (4-ary) tree


Binary (2-ary) trees

Ternary (3-ary) trees 12


fi
fi
ti
Full, Complete, and Perfect Trees
- An n-ary tree is full if within each level, every node has either 0 or n children
- i.e., every non-leaf node has exactly n children
- An n-ary tree is complete if it is full, or full to the next-to-last level with all leaves at
the bo om level on the le side of the tree
- it must be completely lled on every level except for the last level
- An n-ary tree is perfect if it is full and all leaf nodes are at the same depth
- Examples where n = 3:

(Full/Complete Tree) (Complete Tree) (Full/Complete/Perfect Tree) 13


tt
fi
ft
Full, Complete, and Perfect Trees (cont.)
- More examples where n = 2:
- Check whether each of the following trees is full, complete, or perfect

Neither full nor Complete but Full but not Both full and
complete not full complete complete

None of the above trees is perfect!

14
Full, Complete, and Perfect Trees (cont.)
- A full node is a node where both the le and right sub-trees are non-empty trees

Legend: full nodes neither leaf nodes

15
ft
Balanced Trees
- A tree is balanced if all of the leaves of the tree are on the same level or within one
level of each other
- A balanced n-ary tree with m elements will have a height of logn m
- A balanced binary tree (trees in which nodes may have at most two children are called
binary trees) with m nodes has a height of log2 m

(Balanced) (Unbalanced)

16
02

Today’s 01 Binary Trees


Introduction, implementations, and traversal

Agenda Introduction to Trees


Trees as data structures: characteristics and
terminologies
Binary Tree

- A binary tree is a restric on where every node has


at most two children:
- Each child is either empty or another binary tree
- Maximum size ≈ 2depth
- This restric on allows us to label the children as le
and right subtrees
- Applica ons:
- arithme c expressions
- decision processes
left subtree right subtree
- searching

18
ti
ti
ti
ti
f
Binary Tree (cont.)

- The size of a binary tree is the number of nodes in it


- This tree has size 17
- The depth of a node is its distance from the root
- A is at depth zero
- E is at depth 4
- The depth of a binary tree is the depth of its
deepest node
- This tree has depth 7

19
Binary Tree (cont.)
- Le ≠ Right
- The two binary trees in the gure are di erent
A A - In the rst binary tree, node A has a le child
but no right child;
B B - In the second, node A has a right child but no
le child
Left ≠ Right - Put another way: Le and Right are not
rela ve terms

- A binary tree is balanced if every level above


the lowest is “full” (contains 2n nodes)
- where n = above depth
- In most applica ons, a reasonably balanced
binary tree is desirable
Balanced Binary Tree Unbalanced Binary Tree
20
ft
ft
ti
fi
ti
ft
fi
ft
ff
Binary Tree (cont.)

A full binary tree is a binary tree A perfect binary tree of height h A complete binary tree is like a
where every node has exactly 0 is a binary tree where all leaf perfect binary tree but missing a
or 2 children (i.e. either a full nodes have the same depth h few nodes in the last level. Nodes
node or a leaf node) and all other nodes are full are filled-in from left to right

21
Binary Tree Node - Key Field

- Each node of a tree contains a key/index that is associated


with a value similar to the rela onship between a primary
Key
Value
key of a database and a row within a table of the database
- A key is a value that is compared to search criteria. If the
Key Key index and the search criteria match, then the applica on
Value Value retrieves data stored in the row that corresponds to the key.
- The data is referred to as the value of the node
Key Key
Value Value - Each node has a index and a value; the index uniquely
iden es the node and retrieves the value of a node
- Any data type can be used as the key
22
ti
fi
ti
ti
Implemen ng Binary Trees Using Arrays
- An obvious choice for implemen ng binary trees is a linked
structure
- Array-based implementa ons are the less obvious choice, but
are some mes useful
- A very important ques on is how to represent the nodes and
reach their children?
- For full or complete binary trees, we can use an array to
represent a tree
- For any element stored in posi on n,
- the element’s le child is stored in array posi on (2n + 1)
- the element’s right child is stored in array posi on (2 * (n + 1))
- If the represented tree is not complete or rela vely complete,
this approach can waste large amounts of array space
23
ti
ft
ti
ti
ti
ti
ti
ti
ti
ti
Implemen ng Binary Trees Using Arrays (cont.)

- Simulated Child Links


- Each element of the array is an object that stores a reference to the tree element and the
array index of each child
- This approach is modeled a er the way opera ng systems manage memory
- Array posi ons are allocated on a rst-come, rst-served basis
24
ti
ti
ft
fi
fi
ti
Implemen ng Binary Trees Using Linked Lists
A binary tree node contains following parts:

NULL NULL

NULL NULL NULL NULL


25
ti
Implemen ng Binary Trees Using Linked Lists (cont.)
Example:

A
A
B C
B C
D E F
D E F
G H I J K
G H I J K
L
L
26
ti
Tree Traversals
- The process of systema
cally visi ng all the nodes in a tree and performing some
computa on at each node in the tree is called a tree traversal
- For linear structures, the process of itera ng through the elements is fairly obvious
(forwards or backwards)
- For non-linear structures like a tree, the possibili es are more interes ng
- All traversals start at the root of the tree
- Each node can be thought of as the root of a subtree
- There are three tasks of interest tree traversal:
- V — Visi ng a node
- L — Traversing the le subtree
- R — Traversing the right subtree
27
ti
ti
ft
ti
ti
ti
ti
ti
Tree Traversals (cont.)

Let's look at four classic ways of traversing a tree:


- Depth-First Traversal (DF):
- Preorder (VLR): visit the root, then traverse the subtrees
from le to right
- Inorder (LVR): traverse the le subtree, then visit the root,
then traverse the right subtree (for binary trees only)

Preorder: A B D E C
- Postorder (LRV): traverse the subtrees from le to right,
then visit the root
Inorder: D B E A C
- Breadth-First Traversal (BF):
Postorder: D E B C A
- Level-order: visit each node at each level of the tree from
Level-Order: A B C D E top (root) to bo om and le to right
28
ft
tt
f
f
f
Tree Traversals (cont.)

DF DF DF BF
Preorder Inorder Portorder Level-order
Top → Bottom
Node → Left → Right Left → Node → Right Left → Right → Node Left → Right

29
Tree Traversals Using “Flags”
- The order in which the nodes are visited during a tree traversal can be easily
determined by imagining there is a “ ag” a ached to each node, as follows:

Preorder Inorder Postorder

- To traverse the tree, collect the ags:

ABDECFG DBEAFCG DEBFGCA 30


fl
fl
tt
Depth-First Traversals
Name: For each node: Code:

1 public void preorderTraversal(BTNode v) {


2 if( !isEmpty() && !v.isDone() ) {
Preorder
- Visit the node visit(v);
3
(N-L-R)
- Visit the left subtree, if any. 4
preorderTraversal(v.left);
preorderTraversal(v.right);
- Visit the right subtree, if any. 5 }
6 }

1 public void inorderTraversal(BTNode v) {


2 if( !isEmpty() && !v.isDone() ) {
Inorder
- Visit the left subtree, if any. inorderTraversal(v.left);
3
(L-N-R)
- Visit the node 4
visit(v);
inorderTraversal(v.right);
- Visit the right subtree, if any. 5 }
6 }

1 public void postorderTraversal(BTNode v) {


2 if( !isEmpty() && !v.isDone() ) {
- Visit the left subtree, if any. postorderTraversal(v.left) ;
Postorder 3 postorderTraversal(v.right);
- Visit the right subtree, if any. 4
(L-R-N) visit(v);
- Visit the node 5 }
6 }
31
Breadth-First Traversals
- A level-order traversal is more complicated
- It requires the use of extra structures (such as queues and/or lists) to create the
necessary order
Name: Pseudocode: Code:
Create a queue called nodes 1 public void breadthFirst() {
Create an unordered list called results 2 BTNode<T> p = root;
Enqueue the root onto the nodes queue 3 Queue<BSTNode<T>> queue = new Queue<BSTNode<T>>();
While the nodes queue is not empty 4 if (p != null) {
5 queue.enqueue(p);
{ 6 while (!queue.isEmpty()) {
Level- Dequeue the first element from the queue 7 p = queue.dequeue();
Order If that element is not null 8 visit(p);
Add that element to the rear of the results list 9 if (p.left != null)
Enqueue the children of the element on the nodes queue 10 queue.enqueue(p.left);
11 if (p.right != null)
Else 12 queue.enqueue(p.right);
Add null on the result list 13 }
} 14 }
Return an iterator for the result list 15 }

32
Binary Tree Implementa on
Crea ng a Binary Tree using TreeMap Java Collec on
- Besides wri ng your own implementa on of a binary tree structure, using and array
or linked nodes, you may use the TreeMap collec on class, which is de ned in the
java.util package
- The TreeMap class has many member methods but does not provide methods to
display tree nodes in order or to get the tree depth
- You have to write your own displayInOrder() method - to display keys and values
stored in nodes of the tree,
- However, there isn’t any way of calcula ng the depth of a tree in Java
- This is an implementa on detail that’s hidden from the end user

33
ti
ti
ti
ti
ti
ti
ti
ti
fi
Binary Tree Implementa on (cont.)
1. It begins by declaring an instance of the TreeMap class and assigning it to the tree
reference
2. Then Declares two strings to store the key and value of a node (ini alized to NULL)
3. Also Declares an integer to control the for loop. The for loop assigns strings to the
key and value variables and then adds each key and value to the tree

1 TreeMap tree = new TreeMap();


Creating object tree of type TreeMap: 2 String key = null;
3 String value = null;
4 int i;

34
ti
ti
Binary Tree Implementa on (cont.)
4. Java applica on calls the containsKey() method that is a member of the TreeMap class
5. This method returns a Boolean true if the key already exists in the tree; otherwise, a
Boolean false is returned
6. The put() member method of the TreeMap class places the key and value on the tree. The
put() method is similar to the add() member func on
7. Each me a new key and value is added to the tree, the key and value are displayed on the
screen, as shown here:
Adding three keys and values into the tree.
1 System.out.println("Adding three keys and values into the tree.");
2 for(i=0; i<3; i++){ Adding node - key: 345 value: Bob
3 if (i==0) { key ="345"; value="Bob"; } Adding node - key: 123 value: Mary
4 if (i==1) { key ="123"; value="Mary"; }
5 if (i==2) { key="999"; value="Sue"; } Adding node - key: 999 value: Sue
6 if (!tree.containsKey(key)) {
7 System.out.println("Adding node - key: " + key + “\tvalue: " + value);
8 tree.put(key, value); }
9 else
10 System.out.println("Generated duplicate key: " + key);
11 } 35
ti
ti
ti
ti
Binary Tree Implementa on (cont.)
Next, the displayInOrder() method is called to display the contents of the tree on
the screen. This is how it works:
1. First the keySet() method is called to copy the contents of the tree to a key set
2. Think of a key set as a two-column table where one column contains keys and the
other corresponding values. Each row is a node of the tree
3. An instance of the Iterator class is created
4. The Iterator class has member methods that enable you to move through a list such
as a key set
5. In this example, the Iterator class returns the key of each row in the key set

36
ti
Binary Tree Implementa on (cont.)
6. The key is then passed to the get() member method of the TreeMap class to retrieve
the value associated with the key
7. Both the key and the value are then displayed on the screen, as shown next

In order traversal of tree:


1 private static void displayInOrder(TreeMap tree) { key: 123 value: Mary
2 Set keys = tree.keySet();
3 Iterator ii = keys.iterator();
key: 345 value: Bob
4 while(ii.hasNext()) { key: 999 value: Sue
5 String key = (String)ii.next();
6 String value = (String)tree.get(key);
7 System.out.println("key: " + key + "\tvalue: " + value);
8 }
9 } //end displayInOrder

37
ti
Binary Tree Implementa on (cont.)
8. The applica on displays the size of the tree by calling the size() member method
of the TreeMap class. Here’s what is displayed on the screen
➔ Remember that the size is the number of nodes on the tree
➔ The size of the tree before removing any node is 3

9. The applica on then calls the get() member method to retrieve the value
associated with the key 123
➔ If the value isn’t NULL, then the applica on displays the key and the value on the
screen, as shown here:
1 System.out.println("\nSize of tree before removing nodes: " + tree.size());
2 System.out.println("\nRetrieving a value from the tree:");
3 value = (String)tree.get(“123");
Size of tree before removing nodes: 3
4 if(value != null) {
5 System.out.println("Found key: " + key + “\tvalue: " + value); Retrieving a value from the tree:
6 } Found key: 123 value: Mary
38
ti
ti
ti
ti
Binary Tree Implementa on (cont.)
10. The applica on removes the node whose key is 123 by rst calling the
containsKey() member method of the TreeMap class to determine if there is a
node that has 123 as a key
➔ Ifso, the remove() member method is called and passed the key 123 to remove the
node
➔ A message is displayed on the screen, as shown here, to indicate that the node is
being removed:
1 System.out.println("\nRemoving a node from the tree:");
2 if(tree.containsKey("123")){ Removing a node from the tree:
3 System.out.println("Removing key: " + key);
4 tree.remove("123"); Removing key: 123
5 }

39
ti
ti
fi
Binary Tree Implementa on (cont.)
11. To show the result of the node being removed, you call the displayInOrder()
method to display the contents of the tree and call the size() member method to
display the size of the tree
In order traversal of tree:
key: 345 value: Bob
key: 999 value: Sue
1 System.out.println("\nIn order traversal of tree:"); Size of tree a er removing nodes 2
2 displayInOrder(tree);
3 System.out.println("\nSize of tree after removing nodes: " + tree.size());

40
ft
ti
Binary Tree Demo- Program
All together (part 1):
1 import java.lang.*;
2 import java.text.*;
3 import java.util.*;
4 public class BinaryTreeDemo{
5 public static void main(String[] args) {
6 TreeMap tree = new TreeMap();
7 String key = null;
8 String value = null;
9 int i;
10 System.out.println("Adding three keys and values into the tree.");
11 for(i=0; i<3; i++){
12 if (i==0) { key ="345"; value="Bob"; }
13 if (i==1) { key ="123"; value="Mary"; }
14 if (i==2) { key="999"; value="Sue"; }
15 if (!tree.containsKey(key)) {
16 System.out.println("Adding node - key: " + key + “\tvalue: " + value);
17 tree.put(key, value); }
18 else
19 System.out.println("Generated duplicate key: " + key);
20 }
21 System.out.println("\nIn order traversal of tree:");
22 displayInOrder(tree);
23 System.out.println("\nSize of tree before removing nodes: " + tree.size());
41
Binary Tree Demo- Program (cont.)
All together (part 2):
24 System.out.println("\nRetrieving a value from the tree:”);
25 value = (String)tree.get(“123");
26 if(value != null) {
27 System.out.println("Found key: " + key + " value: " + value);
28 }
29 System.out.println("\nRemoving a node from the tree:");
30 if(tree.containsKey("123")){
31 System.out.println("Removing key: " + key);
32 tree.remove("123");
33 }
34 System.out.println("\nIn order traversal of tree:");
35 displayInOrder(tree);
36 System.out.println("\nSize of tree after removing nodes: " + tree.size());
37 } //end main
38 private static void displayInOrder(TreeMap tree) {
39 Set keys = tree.keySet();
40 Iterator ii = keys.iterator();
41 while(ii.hasNext()) {
42 String key = (String)ii.next();
43 String value = (String)tree.get(key);
44 System.out.println("key: " + key + "\tvalue: " + value);
45 }
46 } //end displayInOrder
47 } // end class 42
03

02 Binary Search Tree (BST)


Properties, operations, & implementation

Today’s 01 Binary Trees


Introduction, implementations, and traversal

Agenda Introduction to Trees


Trees as data structures: characteristics and
terminologies
Binary Search Trees

- A binary search tree (BST) is a tree in which


<n ≥n elements are organized to facilitate nding a
par cular element when needed
- The lesubtree of any node n in a BST, contains
elements less than the element stored in n
- The right subtree of any node n contains elements
greater than or equal to the element stored in n

44
ti
f
fi
Binary Search Trees (cont.)
- All the following binary search trees store the same data

Unfortunately, it is possible to
construct weak binary search
trees. This is equivalent to a
linked list, i.e., O(n)

45
Binary Search Trees (cont.)
- The par cular shape of a binary search tree depends on:
- the order in which the elements are added
- any addi onal processing performed on the tree to reshape it
- Binary search trees can hold any type of data, so long as we have a way to determine
rela ve ordering
- Objects implemen ng the comparable interface provide such capability
- Duplicate elements:
- We assume that in any BST, we are not storing duplicate elements unless otherwise stated
- In reality, it is seldom the case where duplicate elements in a container must be stored as
separate en es
- You can always consider duplicate elements with modi ca ons to the algorithms we will
cover

46
ti
ti
ti
ti
ti
ti
fi
ti
Binary Search Trees - Opera ons
- Following are some of the basic opera ons of a BST:
Operation Description
addElement Add an element to the tree
removeElement Remove an element from the tree
removeAllOccurances Remove all occurrences of element from the tree
removeMin Remove the minimum element in the tree
removeMax Remove the maximum element in the tree
findMin Returns a reference to the minimum element in the tree
findMax Returns a reference to the maximum element in the tree

47
ti
ti
Binary Search Trees - Opera ons (cont.)
- More BST opera ons:
Operation Description Methodology

Find out The search goes as follows:


whether a - Start at the root and compare target to element at current node;
Searching - Move left from current node if target is less than element in the current node;
value exists - Move right from current node if target is greater than element in the current node
in a tree - We eventually find the target or encounter the end of a path (target is not found)
Similar to finding an element:
Add an - New elements are added as leaf nodes
Insertion element to - Start at the root, follow path dictated by existing elements until you find no child
a tree in the desired direction
- Add the new element

Remove an Deletion goes as follows:


- The process starts by finding the node needed to be deleted
Deletion element
- The node may be a leaf, parent for one subtree or full node
from a tree - Each case should be handled properly to maintain the property of the BST
48
ti
ti
Why BST?
- BSTs provide good logarithmic me performance in the best and average cases
- Average case complexi es of using linear data structures compared to BSTs:
Data Structure Deletion Insertion Searching
O(log n) O(log n) O(log n)
BST
FAST FAST FAST
O(n) O(n) O(log n)
Sorted Array
SLOW SLOW FAST*
O(n) O(n) O(n)
Sorted Linked List
SLOW SLOW SLOW
* using binary search

49
ti
ti
BST Implementa on
- We will look at an implementa on of a binary search tree
- As usual, there are di erent ways to implement any data structure algorithm
- Let us explore algorithms of basic methods
- To start with, consider the following BST node implementa on
1 public class BSTNode<T extends Comparable<? super T>> {
2 T el;
3 BSTNode<T> left, right;
4 // constructors
5 }

- This will result in having a node that looks something like the following:
el
→ abbreviated as → el

left right 50
ff
ti
ti
ti
Searching in a BST
To locate a node, the following steps are to be performed:
root
1. Start at root node
2. Examine node data:
31
2.1. Is it desired value (desired data = node data)?
→ Done 19 59
2.2. Else, is desired data < node data?
→ Repeat step 2 with le subtree 7 NULL 43 NULL
2.3. Else, is desired data > node data?
→ Repeat step 2 with right subtree NULL NULL NULL NULL

3. Con nue un l desired value found or NULL pointer reached

51
ti
ti
f
Searching in a BST (cont.)
- Pseudocode:
1 Recursive-Tree-Search(n, k)
41
2 if n = null
3 then return n
22 200
4 if n.key = k
5 then return n
6 if n.key > k 18 28 50 213
7 then return Recursive-Tree-Search(n.left, k)
8 else return Recursive-Tree-Search(n.right, k) 12 19 45

Example: n = 41, k = 19
➔ Running me: O(h) = O(log n) Recursive-Tree-Search(41, 19) //n = n.left = 22
Recursive-Tree-Search(22, 19) //n = n.left = 18
➔ What type of recursion is it? Recursive-Tree-Search(18, 19) //n = n.right = 19
n = k = 19 → key found!

52
ti
Searching in a BST (cont.)
- Alterna ve pseudocode:
1 Iterative-Tree-Search(n, k) 41
2 while n ≠ null and n.key ≠ k
3 if n.key > k 22 200
4 then n ←⃪ n.left
5 else n ←⃪ n.right
18 28 50 213
6 return n

12 19 45
➔ The itera ve tree search is more e cient on
Example: n = 41, k = 19
most computers
Iterative-Tree-Search(41, 19)
➔ However, the recursive tree search is more n = n.left // n = 22
n = n.left // n = 18
straigh orward
n = n.right // n = 19
n = k = 19 → key found!
53
tf
ti
ti
ffi
Finding Min & Max
- Binary search tree guarantees that:
- The minimum is located at the le -most node
- The maximum is located at the right-most node
- The following are the pseudocode for both opera ons:
1 Tree-Minimum(n) 1 Tree-Maximum(n)
2 while n.left ≠ null 2 while n.right ≠ null
3 n ←⃪ n.left 3 n ←⃪ n.right
4 return n.key 4 return n.key

➔ What is the running me of both opera ons?

54
ti
ft
ti
ti
Binary Search Trees - Inser on
- Remember,
- The process of adding an element is similar to nding an element
- New elements are added as leaf nodes
- Start at the root, follow path dictated by exis ng elements un l you nd no child in the
desired direc on
- Then add the new element
- It’s important to ensure the binary-search-tree property holds a er the inser on
- If tree is empty, replace the empty tree with a new binary tree consis ng of the new node
as root, with empty le and right subtrees
- Otherwise, if the item is less than root, recursively insert the item in the le subtree, but
if the item is greater than the root, recursively insert the item into the right subtree
- Inser on is easier than dele on!

55
ti
ti
ft
ti
ti
fi
ti
ti
fi
ti
ft
ft
ti
Binary Search Trees - Inser on (cont.)
- Pseudocode:
1 BSTNode Tree-Insert(n, newNode) 41
2 if (n == null)
3 n ←⃪ newNode
22 200
4 if (newNode.key < n.key)
5 n.left ←⃪ Tree-Insert(n.left, newNode)
18 28 50 213
6 else if (newNode.key > n.key)
7 n.right ←⃪ Tree-Insert(n.right, newNode)
12 19 45 69
8 return n

15
➔ Example, adding 69 and 15

56
ti
Binary Search Trees - Dele on

41 - Removing a target in a BST is not as


Case 3
simple as that for linear data structures
22 200
- Three dis
nct cases must be considered
18 28 50 213 when removing an element
1. The node to remove is a leaf
Case 2
12 19 45 69 2. The node to remove has one child
Case 1 3. The node to remove has two children
15

57
ti
ti
Binary Search Trees - Dele on (cont.)
Case 1: The node to remove is a leaf
- Simply remove the node from the tree

41 41

22 200 22 200
Delete 69

18 28 50 213 18 28 50 213

12 19 45 69 12 19 45 69

15 15

58
ti
Binary Search Trees - Dele on (cont.)
Case 2: The node to remove has one child
- Copy the child to the node and then delete the child

41 41

22 200 22 200
Delete 12

18 28 50 213 18 28 50 213

12 19 45 15 19 45

15 15

59
ti
Binary Search Trees - Dele on (cont.)
Case 3: The node to remove has two children
- Find an appropriate node lower in the tree and use it to replace the node to be deleted
- A good choice is either the in-order successor or the in-order predecessor of the node
- That is, the smallest in its right subtree (in-order successor) or the largest in its le subtree (in-
order predecessor)
41 41

22 200 19 200
Delete 22

18 28 50 213 18 28 50 213

15 19 45 15 19 45

➔ Note that the in-order (LVR) traversal of this tree is: 15, 18, 19, 22, 28, 41, 45, 50, 200, 213
➔ This means that either 19 or 28 could’ve been used to replace 22 60
ti
ft
Binary Search Trees - Dele on (cont.)
- Pseudocode:
1 Tree-Delete-Node(n, key)
2 if (n == null) return n 41
3 if (key < n.key)
4 n.left ←⃪ Tree-Delete-Node(n.left, key) 19 200
5 else if (key > n.key)
6 n.right ⃪ Tree-Delete-Node(n.right, key)
18 28 50 213
7 else
8 if (n.left == null)
9 return n.right; 15 45
10 else if (n.right == null)
11 return n.left; Example: n = 41, k = 19
n.key ⃪ Tree-Minimum(n.right) Tree-Delete-Node(41, 19) //n = n.left = 19
12
n.right ⃪ Tree-Delete-Node(n.right, n.key) Tree-Delete-Node(19, 19) //n= k=19, n.right=28
13
n ⃪ Tree-Minimum(28) //n = 28
14 return n
n.right ⃪ Tree-Delete-Node(28, 28)//n.right=null
return null → key deleted!
61
ti
Binary Search Exercise
32 teams quali ed for the 2014 World Cup. If the names of the teams were arranged in sorted
order (an array), how many items in the array would binary search have to examine to nd the
loca on of a par cular team in the array, in the worst case?
- Each "guess" in binary search halves the number of possible array loca ons where the item you
are looking for could be
- Ini ally, the name of the team could be in any of 32 array loca ons, with index in the range 0...31
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

-A er the rst item is checked (at index 15), there are no more than 15 possible loca ons where
the team name you are looking for could be, since it's either at an index in the range 0...14 or at
an index in the range 16…31
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

*https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/e/running-time-of-binary-search 62
ft
ti
ti
fi
fi
ti
ti
ti
ti
fi
Binary Search Exercise (cont.)
-A er guess number 2 (at index 7), there are no more than 7 possible loca ons le (indices at either 0…6 or
8…14)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

-A er guess number 3 (at index 3), there are 3 loca ons le (indices at either 0…2 or 4…6)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

-A er guess number 4 (at index 1), there's only one loca on le (either index 0 or 2)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

- binary search s ll needs to check the last loca on to see if it's the par cular team
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

- That means that in the worst case, it will have to check out 5 items total
➔ Can you guess the complexity of the binary search algorithm?
63
ft
ft
ft
ti
ti
ti
ti
ft
ft
ti
ti
ft
Heaps

BST: el
- Heap is a special case of binary tree data
structure that has the following two proper es:
1. The value of each node is greater than or equal
< el ≥ el to the values stored in each of its children
➔ this property generates ‘Max Heap’
2. The tree is perfectly balanced, and the leaves in
Heap: el the last level are all in the le most posi ons
- Heaps may be implemented as arrays and linked
nodes too!

≤ el ≤ el
64
f
ti
ti
Heaps (cont.)
- Do the following binary trees (BTs) depict heaps?

20
20

10 15
10 15

7 3 7 8
7 3 7 18

6 5
6 5

Violates the first heap condition Violates the second heap condition
(node 15 is not greater of each of its children) (leaves are not all in the left most positions)
65
Heaps (cont.)
- Heap or Non Heap?
10 10 10

8 7 2 7 8 7

2 3 6 8 3 6 2 3 6

Heap NonHeap NonHeap

21 12 15 6 15 6

10 12 10 21 6 15 6 15

Heap NonHeap Heap NonHeap NonHeap NonHeap


66
Heaps (cont.)
- Di erent heaps constructed with the same elements:

10 10 10

2 9 9 8 7 9

1 0 8 7 7 2 0 1 0 1 2 8

67
ff
04

03 Finishing Thoughts
Summary & resources

02 Binary Search Tree (BST)


Properties, operations, & implementation

Today’s 01 Binary Trees


Introduction, implementations, and traversal

Agenda Introduction to Trees


Trees as data structures: characteristics and
terminologies
Comics Relief :)

69
Summary
A tree is a nonlinear structure whose elements are organized into a hierarchy.
Trees are described by a large set of related terms.
In general, a balanced n-ary tree with m elements will have height lognm.
There are four basic methods for traversing a tree: preorder, inorder,
postorder, and level-order.
Preorder traversal means visit the node, then the le child, then the right child.
Inorder traversal means visit the le child, then the node, then the right child.
Postorder traversal means visit the le child, then the right child, then the
node.
Level-order traversal means visit the nodes at each level, one level at a
me, star ng with the root.
In the array implementa on of a BT, the le and right children of a node
stored at index i are stored in the indices 2i + 1 and 2(i + 1), respec vely.
70
ti
ti
ti
ft
ft
ft
ft
ti
Summary
A binary search tree is a binary tree with the added property that the le
child is less than the parent, which is less than or equal to the right child.
The de ni on of a binary search tree is an extension of the de ni on of a
binary tree.
In removing an element from a binary search tree, another node must be
promoted to replace the node being removed.
The le most node in a binary search tree will contain the minimum
element, whereas the rightmost node will contain the maximum element.
If a binary search tree is not balanced, it may be less e cient than a
linear structure.
A par cular kind of binary tree, called a heap, has two proper es: The
value of each node is greater than or equal to the values stored in each of
its children, and the tree is perfectly balanced, and the leaves in the last
level are all in the le most posi ons.
71
ti
ft
fi
ti
ft
ti
ffi
ti
fi
ti
ft
Resources & More…
Textbooks Chapters:
- Data Structures and Algorithms in Java, Ch6
- Data Structures and Algorithm Analysis in Java, Ch4
Interesting Articles & Videos:
- Introduction to Tree Data Structure (video)
- Tree Data Structure (video)
- Tree Data Structure (short article)
- Binary Tree and BST in Data Structure (video)
- Binary Search Tree (collection of articles)
Credits:
- Some slides illustrative content is courtesy of
tutorialspoint.com 72
Resources & More…

Other Resources:
- Java Software Structures - Designing and Using Data Structures,
4th edition, Lewis and Chase.
- Data Structures & Algorithms in Java, 4th edition, Drozdek.
- Data Structures and Algorithms in Java, 6th edition, Goodrich,
Tamassia and Goldwasser.
- Slides at: https://ece.uwaterloo.ca/~dwharder/aads/
Lecture_materials/
- Douglas Wilhelm Harder, Mmath, University of Waterloo.
- Slides at: http://faculty.kfupm.edu.sa/ICS/jauhar/ics202/

73
Thank You & Stay Safe
:)

You might also like