Title: CMPT 225
1CMPT 225
2Objectives
- Understand tree terminology
- Understand and implement tree traversals
- Define the binary search tree property
- Implement binary search trees
3Trees
- A set of nodes with a single starting point
- called the root
- Each node is connected by an edge to some other
node - A tree is a connected graph
- There is a path to every node in the tree
- A tree has one less edge than the number of nodes
4Is it a Tree?
NO! All the nodes are not connected
yes! (but not a binary tree)
yes!
NO! There is an extra edge (5 nodes and 5 edges)
yes! (its actually the same graph as the blue
one)
5Tree Relationships
- If there is an edge between two nodes u and v,
and u is above v in the tree, then v is said to
be a child of u, and u the parent of v - A is the parent of B, C and D
- This relationship can be generalized
- E and F are descendants of A
- D and A are ancestors of G
- B, C and D are siblings
6More Tree Terminology
- A leaf is a node with no children
- A path is a sequence of nodes v1 vn
- where vi is a parent of vi1 (1 ? i ? n)
- A subtree is any node in the tree along with all
of its descendants - A binary tree is a tree with at most two children
per node - The children are referred to as left and right
- We can also refer to left and right subtrees
7Tree Terminology Example
A
path from A to D to G
B
D
subtree rooted at B
leaves C,E,F,G
G
E
F
8Binary Tree
A
B
C
right child of A
left subtree of A
F
G
D
E
right subtree of C
H
I
J
9Measuring Trees
- The height of a node v is the length of the
longest path from v to a leaf - The height of the tree is the height of the root,
which is the length of the longest path from the
root to a leaf - The depth of a node v is the length of the path
from the root to v - This is also referred to as the level of a node
- Note that there are slightly different
formulations of the height of a tree - Where the height of a tree is said to be the
number of different levels of nodes in the tree
(counting the root)
10Height of a Binary Tree
A
height of the tree is 3
B
C
B
height of node B is 2
level 1
F
G
D
E
E
level 2
depth of node E is 2
H
I
J
level 3
11Binary Tree Descriptions
- A binary tree is perfect if no node has only one
child and if all the leaves have the same depth - A perfect binary tree of height h has (2h1 1)
nodes, of which 2h are leaves - A complete binary tree is one where
- The leaves are on at most two different levels,
- The second to bottom level is filled in and
- The leaves on the bottom level are as far to the
left as possible. - A balanced binary tree is one where
- No leaf is more than a certain amount farther
from the root than any other leaf, this is
sometimes stated more specifically as - The height of any nodes right subtree is at most
one different from the height of its left subtree - A balanced trees height is sometime specified in
terms of its relation to the number of nodes in
the tree (see red-black trees)
12Perfect and Complete Binary Trees
Complete binary tree
Perfect binary tree
13Balanced Binary Trees
14Unbalanced Binary Trees
15Binary Tree Traversals
- A traversal algorithm for a binary tree visits
each node in the tree - and, typically, does something while visiting
each node! - Traversal algorithms are naturally recursive
- There are three traversal methods
- Inorder
- Preorder
- Postorder
16InOrder Traversal Algorithm
- // InOrder traversal algorithm
- inOrder(Node n)
- if (n ! null)
- inOrder(n.leftChild)
- visit(n)
- inOrder(n.rightChild)
-
17PreOrder Traversal
visit(n)
1
preOrder(n.leftChild)
17
preOrder(n.rightChild)
visit preOrder(l) preOrder(r)
visit preOrder(l) preOrder(r)
13
27
2
6
3
9
39
16
20
5
7
8
visit preOrder(l) preOrder(r)
visit preOrder(l) preOrder(r)
visit preOrder(l) preOrder(r)
visit preOrder(l) preOrder(r)
11
4
visit preOrder(l) preOrder(r)
18PostOrder Traversal
postOrder(n.leftChild)
8
postOrder(n.rightChild)
17
visit(n)
postOrder(l) postOrder(r) visit
postOrder(l) postOrder(r) visit
13
27
4
7
9
39
16
20
2
3
5
6
postOrder(l) postOrder(r) visit
postOrder(l) postOrder(r) visit
postOrder(l) postOrder(r) visit
postOrder(l) postOrder(r) visit
11
1
postOrder(l) postOrder(r) visit
19Binary Tree Implementation
- The binary tree ADT can be implemented using a
number of data structures - Reference structures (similar to linked lists)
- Arrays
- We will look at three implementations
- Binary search trees (references)
- Red black trees
- Heap (arrays)
20Problem Accessing Sorted Data
- Consider maintaining data in some order
- The data is to be frequently searched on the sort
key e.g. a dictionary - Possible solutions might be
- A sorted array
- Access in O(logn) using binary search
- Insertion and deletion in linear time
- An ordered linked list
- Access, insertion and deletion in linear time
21Dictionary Operations
- The data structure should be able to perform all
these operations efficiently - Create an empty dictionary
- Insert
- Delete
- Look up
- The insert, delete and look up operations should
be performed in O(logn) time
22Binary Search Trees (BSTs)
- A binary search tree is a binary tree with a
special property - For all nodes v in the tree
- All the nodes in the left subtree of v have
labels less than the label of v and - All the nodes in the right subtree of v have
labels greater than or equal to the label of v - Binary search trees are fully ordered
23BST Example
17
13
27
9
39
16
20
11
24BST InOrder Traversal
inOrder(n.leftChild)
5
visit(n)
17
inOrder(n.rightChild)
inOrder(l) visit inOrder(r)
inOrder(l) visit inOrder(r)
13
27
3
7
9
39
16
20
1
4
6
8
inOrder(l) visit inOrder(r)
inOrder(l) visit inOrder(r)
inOrder(l) visit inOrder(r)
inOrder(l) visit inOrder(r)
11
2
inOrder(l) visit inOrder(r)
25BST Implementation
- Binary search trees can be implemented using a
reference structure - Tree nodes contain data and two references to
nodes
Node leftChild
Node rightChild
Object data
26BST Search
- To find a value in a BST search from the root
node - If the target is less than the value in the node
search its left subtree - If the target is greater than the value in the
node search its right subtree - Otherwise return true, or return data, etc.
- How many comparisons?
- One for each node on the path
- Worst case height of the tree 1
27BST Search Example
click on a node to show its value
17
13
27
9
39
16
20
11
28BST Insertion
- The BST property must hold after insertion
- Therefore the new node must be inserted in the
correct position - This position is found by performing a search
- If the search ends at the (null) left child of a
node make its left child refer to the new node - If the search ends at the right child of a node
make its right child refer to the new node - The cost is about the same as the cost for the
search algorithm, O(height)
29BST Insertion Example
insert 43 create new node find position insert
new node
30BST Deletion
- After deleting a node the BST property must still
hold - Deletion is not as straightforward as search or
insertion - So much so that sometimes it is not even
implemented! - There are a number of different cases that have
to be considered
31BST Deletion Cases
- The node to be deleted has no children
- Remove it (assign null to its parents reference)
- The node to be deleted has one child
- Replace the node with its subtree
- The node to be deleted has two children
- Replace the node with its predecessor, the right
most node of its left subtree (or with its
successor, the left most node of its right
subtree) - If that node has a child (and it can have at most
one child) attach that to the nodes parent
32BST Deletion target is a leaf
delete 30
33BST Deletion target has one child
delete 79 replace with subtree
34BST Deletion target has one child
delete 79 after deletion
35BST Deletion target has 2 children
delete 32
find successor and detach
36BST Deletion target has 2 children
delete 32
find successor
attach target nodes children to successor
37BST Deletion target has 2 children
delete 32
find successor
attach target nodes children to successor
make successor child of targets parent
38BST Deletion target has 2 children
delete 32
note successor had no subtree
39BST Deletion target has 2 children
Note predecessor used instead of successor to
show its location - an implementation would have
to pick one or the other
delete 63
find predecessor - note it has a subtree
40BST Deletion target has 2 children
delete 63
find predecessor
attach predecessors subtree to its parent
41BST Deletion target has 2 children
delete 63
find predecessor
attach subtree
attach targets children to predecessor
42BST Deletion target has 2 children
delete 63
find predecessor
attach subtree
attach children
attach predecssor to targets parent
43BST Deletion target has 2 children
delete 63
44BST Efficiency
- The efficiency of BST operations depends on the
height of the tree - All three operations (search, insert and delete)
are O(height) - If the tree is complete the height is ?log(n)?
- What if it isnt complete?
45Height of a BST
- Insert 7
- Insert 4
- Insert 1
- Insert 9
- Insert 5
- Its a complete tree!
height ?log(5)? 2
46Height of a BST
- Insert 9
- Insert 1
- Insert 7
- Insert 4
- Insert 5
- Its a linked list!
height n 1 4 O(n)
47Balanced BSTs
- It would be ideal if a BST was always close to
complete - i.e. balanced
- To guarantee a balance BST we would have to make
the insertion and deletion algorithms more
complex - e.g. red black trees.