Computer Science II - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Computer Science II

Description:

A designated node of the set, r, is called the root of the tree ... The height of a tree is the longest path to a node from the root ... – PowerPoint PPT presentation

Number of Views:114
Avg rating:3.0/5.0
Slides: 63
Provided by: joe49
Category:
Tags: computer | science | tree | trees

less

Transcript and Presenter's Notes

Title: Computer Science II


1
Computer Science II
  • Trees

2
What is a Tree?
  • A tree is a finite, non-empty set of nodes,
    Tr ? T1 ? T2 ? ? Tn,with the following
    properties
  • A designated node of the set, r, is called the
    root of the tree
  • The remaining nodes are partioned inton ?
    subsets, T1, T2, , Tn, each of which is a tree

3
What does a Tree look like?
Root
Nodes
T2
Empty trees
T1
4
Some terminology
  • Degree of a node - the number of subtrees
    associated with that node
  • Leaf - a node with degree 0 (no subtrees)
  • Child - root of a nodes subtree
  • Parent - node that represents the immediate
    predecessor to a subtrees root
  • Siblings - nodes that share the same parent
  • A path is a non-empty sequence of nodes

5
More Terminology
  • The depth of a node is the length of the path to
    the node from its root
  • The height of a tree is the longest path to a
    node from the root
  • The height of a node is the longest path from the
    node to a leaf
  • An ancestor is another node, above, with a path
    to the node in question
  • A descendent is another node, below, which can be
    reached from the node in question

6
Breadth-First Traversal
7
Breadth-First Traversal
1
8
Breadth-First Traversal
1 2
9
Breadth-First Traversal
1 2 3
10
Breadth-First Traversal
1 2 3 4
11
Breadth-First Traversal
1 2 3 4 5
12
Pre-Order Traversal
13
Pre-Order Traversal
1
14
Pre-Order Traversal
1 2
15
Pre-Order Traversal
1 2 4
16
Pre-Order Traversal
1 2 4
17
Pre-Order Traversal
1 2 4 5
18
Pre-Order Traversal
1 2 4 5
19
Pre-Order Traversal
1 2 4 5
20
Pre-Order Traversal
1 2 4 5 3
21
Pre-Order Traversal
1 2 4 5 3
22
Post-Order Traversal
23
Post-Order Traversal
24
Post-Order Traversal
4
25
Post-Order Traversal
4
26
Post-Order Traversal
4 5
27
Post-Order Traversal
4 5 2
28
Post-Order Traversal
4 5 2
29
Post-Order Traversal
4 5 2 3
30
Post-Order Traversal
4 5 2 3 1
31
N-ary Trees
  • Either empty, or ...
  • has a root, R, and exactly N distinctN-ary
    trees as subtrees

R
T2
T3
T1
32
N-ary Trees
  • What is the minimum height for an n-ary tree?
  • Given an n-ary tree of height X, what is the
    maximum number of nodes?
  • Internal nodes?
  • Leaf nodes?

R
T2
T3
T1
33
Binary Trees
  • A binary tree is an n-ary tree where n2

R
TR
TL
34
Expression Trees
  • An expression tree is an binary tree each
    non-leaf node represents an operator and each
    leaf an operand


1
-
2
4
35
Expression Trees
  • How would you use an expression tree to represent
    the following?
  • 1 2
  • (5 - 3) x 2
  • (4 x 5) (6 / 3)
  • Expression trees provide a convenient method for
    converting among postfix, infix and prefix
    notations

36
M-Way Search Trees
  • A finite set of keys (elements)
  • Either the set T is empty, T?, or ...
  • The set consists of n M-way subtrees, T0 thru
    Tn-1, and n-1 keys, k1 thru kn-1, where 2 ? n ? M

37
M-Way Search Trees
  • All keys at a node are distinct and in order
  • All the keys in subtree Ti-1 are less than ki
  • All the keys in subtree Ti are greater than ki

38
Binary Search Trees
D
  • An M-way search tree where M2

E
B
A
C
39
Build a Search Tree
  • Task to build the tree to the left by adding the
    keys in the following order
  • C, A, B, E, D

C
E
A
B
D
40
Build a Search Tree
  • Start by adding the first key as a lone root node

C
41
Build a Search Tree
  • Add A
  • Since A add A as the root of a new left subtree

C
A
42
Build a Search Tree
  • Add B
  • Since B subtree, we add B to that subtree
  • That is, we recursively apply our algorithm to
    the subtree with A at the root

C
A
B
43
Build a Search Tree
  • Add E
  • Since E C, and there is no right subtree,
    we add E as the root of a new right subtree

C
E
A
B
44
Build a Search Tree
  • Add D
  • Since D C, and there is an existing right
    subtree, we add D to that subtree
  • Again, a recursive call!

C
E
A
B
D
45
Removing a Key
  • What if we wanted to remove the key D from this
    search tree?

C
E
A
B
D
46
Removing a Key
  • What if we wanted to remove the key E from this
    search tree?
  • You have 5 minutes to come up with a plan!

C
E
A
B
D
47
Removing a Key
  • What if we wanted to remove the key C from this
    search tree?
  • You have 5 minutes to come up with a plan!

C
E
A
B
D
48
Balancing trees
  • Given our current scheme for building a binary
    search tree...
  • What would the resulting tree look like if we
    added the following keys?A, B, C, D, E, F, G
  • What is the height of this tree?
  • Could you rearrange these nodes to create a new
    binary search tree with a smaller height?

49
Balancing trees
  • When searching for a key, the largest number of
    nodes we need to visit is determined by the
    trees height
  • Can you tell us why, then, it might be
    advantageous to continually balance these search
    trees?

50
AVL Search Trees
  • A binary tree is AVL balanced if...
  • Its empty, or...
  • The heights of the roots two subtrees differ by
    no more than one, and both are AVL balanced
  • Tr,TL,TRhL-hR ? 1

r
1
TL
TR
-1
Adelson-Velskii and Landis
51
Balancing a TreeSingle Rotation (LL)
  • Starting with an AVL balanced tree, ...

B
1
h2
A
0
BR
h1
h
AR
AL
52
Balancing a TreeSingle Rotation (LL)
  • Add an element as usual, and then ...

B
2
h3
A
1
BR
h2
h1
AR
AL
53
Balancing a TreeSingle Rotation (LL)
  • Since the AR is to the left of B,

B
2
L
h3
A
1
BR
h2
L
h1
AR
AL
54
Balancing a TreeSingle Rotation (LL)
  • Make B the right child of A, and ...

A
h3
B
AL
h2
h1
BR
AR
55
Balancing a TreeSingle Rotation (LL)
  • AR the left child of B

0
A
h2
B
0
AL
h1
h
AR
BR
56
Balancing a TreeDouble Rotation (LR)
2
C
  • Add a node as usual,

L
h3
-1
A
CR
R
B
-1
AL
h2
R
BL
BR
h
57
Balancing a TreeDouble Rotation (LR)
2
C
  • Perform a RR rotation on As subtree, and the ...

h2
0
B
CR
h1
A
BR
h
BL
AL
58
Balancing a TreeDouble Rotation (LR)
-1
B
  • Perform a LL rotation on Cs tree

h2
A
C
0
1
h1
BL
AL
BR
CR
h
59
B-Trees
  • An M-way tree where
  • All leaves are on the same level
  • All internal nodes except the root are
    constrained to have at most M non-empty children
    and at least M/2 non-empty children
  • The root has at most M non-empty children

60
B-Trees
  • When to use B-Trees
  • Dataset is too large for memory
  • Why to use B-Trees
  • Reduces disk access
  • Nodes (records) are fewer but larger, thus
    reducing number of disk accesses

61
B-TreesInsertion
  • Insert new element into leaf
  • curNode ? leaf
  • while (curNode overflow)
  • Split curNode into two nodes, same level
  • Promote median element up to parent
  • curNode ? Parent of curNode

62
B-TreesDeletion
  • If (element not a leaf) then
  • Swap with successor
  • curNode ? leaf
  • While (curNode underflow)
  • Try to redistribute elements from sibling via
    parent
  • If possible, merge curNode with sibling and
    element from parent
  • curNode ? Parent of curNode
Write a Comment
User Comments (0)
About PowerShow.com