CMPT 225 - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

CMPT 225

Description:

vn. where vi is a parent of vi 1 (1 i n) ... Create an empty dictionary. Insert. Delete. Look up ... create new node. find position. insert new node. 43. 43 ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 48
Provided by: johne78
Category:
Tags: cmpt | bst | create | john | preorder | property

less

Transcript and Presenter's Notes

Title: CMPT 225


1
CMPT 225
  • Binary Search Trees

2
Objectives
  • Understand tree terminology
  • Understand and implement tree traversals
  • Define the binary search tree property
  • Implement binary search trees

3
Trees
  • 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

4
Is 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)
5
Tree 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

6
More 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

7
Tree Terminology Example
A
path from A to D to G
B
D
subtree rooted at B
leaves C,E,F,G
G
E
F
8
Binary Tree
A
B
C
right child of A
left subtree of A
F
G
D
E
right subtree of C
H
I
J
9
Measuring 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)

10
Height 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
11
Binary 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)

12
Perfect and Complete Binary Trees
Complete binary tree
Perfect binary tree
13
Balanced Binary Trees
14
Unbalanced Binary Trees
15
Binary 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

16
InOrder Traversal Algorithm
  • // InOrder traversal algorithm
  • inOrder(Node n)
  • if (n ! null)
  • inOrder(n.leftChild)
  • visit(n)
  • inOrder(n.rightChild)

17
PreOrder 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)
18
PostOrder 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
19
Binary 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)

20
Problem 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

21
Dictionary 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

22
Binary 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

23
BST Example
17
13
27
9
39
16
20
11
24
BST 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)
25
BST 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
26
BST 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

27
BST Search Example
click on a node to show its value
17
13
27
9
39
16
20
11
28
BST 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)

29
BST Insertion Example
insert 43 create new node find position insert
new node
30
BST 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

31
BST 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

32
BST Deletion target is a leaf
delete 30
33
BST Deletion target has one child
delete 79 replace with subtree
34
BST Deletion target has one child
delete 79 after deletion
35
BST Deletion target has 2 children
delete 32
find successor and detach
36
BST Deletion target has 2 children
delete 32
find successor
attach target nodes children to successor
37
BST Deletion target has 2 children
delete 32
find successor
attach target nodes children to successor
make successor child of targets parent
38
BST Deletion target has 2 children
delete 32
note successor had no subtree
39
BST 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
40
BST Deletion target has 2 children
delete 63
find predecessor
attach predecessors subtree to its parent
41
BST Deletion target has 2 children
delete 63
find predecessor
attach subtree
attach targets children to predecessor
42
BST Deletion target has 2 children
delete 63
find predecessor
attach subtree
attach children
attach predecssor to targets parent
43
BST Deletion target has 2 children
delete 63
44
BST 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?

45
Height of a BST
  • Insert 7
  • Insert 4
  • Insert 1
  • Insert 9
  • Insert 5
  • Its a complete tree!

height ?log(5)? 2
46
Height of a BST
  • Insert 9
  • Insert 1
  • Insert 7
  • Insert 4
  • Insert 5
  • Its a linked list!

height n 1 4 O(n)
47
Balanced 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.
Write a Comment
User Comments (0)
About PowerShow.com