Trees 2 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Trees 2

Description:

If a binary tree has n nodes, then the total number of links is 2n. ... When insertion occurs on a right subtree of a left child of the nearest ancestor ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 22
Provided by: csupo
Category:
Tags: aright | trees

less

Transcript and Presenter's Notes

Title: Trees 2


1
Trees (2)
  • Threaded Binary Search Trees
  • Tree Balancing AVL Trees
  • Other trees

2
Review of Traversals in Binary Search Tree
  • Preorder
  • Inorder
  • Postorder
  • Recursive
  • Complexity

3
Links in a Binary Tree
  • Consider using a linked structure to implement a
    binary tree.
  • If a binary tree has n nodes, then the total
    number of links is 2n.
  • Since each node except the root has exactly one
    incoming arc, there are only n-1 links point to
    nodes the remaining n1 links are null.

data
4
Threaded Binary Search Trees
  • Since there are more than half of the links not
    used, it would be efficient if we can use them
    for efficient traversals
  • Right-threaded BST use the unused right pointer
    to the parent (inorder successor)
  • Application inorder traversal
  • Observation The inorder successor of a node x
    with right-thread is its parent if x is a left
    child of its parent otherwise, it is the nearest
    ancestor of x that contains x in its left subtree.

5
Example
H
E
K
L
B
F
J
G
M
I
D
A
C
Nodes with no right child
6
Example continue Right pointer
H
E
K
L
B
F
J
G
M
I
D
A
null
C
Nodes with no right child
7
Inorder traversal on the threaded BST
H
E
K
L
B
F
J
G
M
I
D
A
null
C
  • Where is the save?

8
Algorithm for inorder traversal on a threaded BST
  • Initialize a pointer p to the root
  • while p! null
  • while p-gtleft ! null
  • p p-gtleft
  • visit p
  • while p-gtright is a thread
  • pp-gtright
  • visit p
  • pp-gtright

9
Homework
  • Quiz 13.1 problem 3-6

10
Tree Balancing AVL Trees
  • Motivation The order given for creating a BST
    determines the efficiency of the searching (the
    level), as shown in Ch. 10.
  • Review of the tree created by different orders
    (Searching time O(level))
  • O, E, T, C, U, M, P
  • C, O, M, P, U, T, E
  • C, E, M, O, P, T, U
  • AVL keeping a BST balanced as items are inserted
    (Adelson-Velskii Landis)

11
Balance factor
  • Defined for a node x
  • Denote the height of the left subtree of x as l,
    the height of the right subtree of x as r
  • The balance factor bl-r
  • AVL is a BST in which the balance factor of each
    node is 0, 1, or -1

12
AVL examples with balance factors indicated
1
-1
0
0
1
-1
0
0
0
0
0
?
?
0
?
How about this?
?
?
Are these AVL trees?
0
0
13
How to create an AVL tree?
  • Mechanism
  • Simple right rotation (applied to cases where the
    insertion is on a left subtree of a left child of
    the nearest ancestor with balance 2)
  • Simple left rotation (right subtree, right child,
    ancestor balance is -2)
  • Left-right rotation (right subtree, left child,
    ancestor balance 2)
  • Right-left rotation (left subtree, right child,
    ancestor balance -2)

14
Simple right rotation exp
  • Insertion happens on a left subtree of a left
    child of the nearest ancestor with balance 2

2
After rotation, the balance factor becomes 0
1
1
0
insertion
2
0
0
15
Left-right rotation
  • When insertion occurs on a right subtree of a
    left child of the nearest ancestor with balance
    2)

P

2
P
Left rotation first
G
R
O
R
-1
0
Right rotation next Including the ancestor with
balance 2
0
D
O
G
1
Note rotation has to maintain the BST feature
insertion
M
M
D
0
16
Left-Right rotation continue

P
O
O
R


P
G

G
Right rotation next Including the ancestor
R
M

M

D
D
17
Homework Exercise 13.2
  • 6-10 (construct the BST using AVL insertion
    algorithm)

18
Other Trees
  • Binary trees cannot represent all situations
  • 2-3-4 Tree
  • Each node stores at most 3 data values
  • Each internal node is a 2-node, 3-node, or 4-node
  • All the leaves are on the same level

19
2-3-4 Tree example
53
60 70
27 38
16 25
33 36
41 46 48
55 59
65 68
73 75 79
Search item 36
20
B-Trees
  • A B-tree of order m
  • Each node stores at most m-1 data values
  • Each internal node is a 2-node, 3-node, or an
    m-node
  • All the leaves are on the same level
  • B-Trees are useful for organizing data stored in
    secondary memory

21
Tree Graph
  • A tree is a connected acyclic graph
  • An acyclic graph is one that contains no cycles
  • Graph Theory with Applications, J.A. Bondy and
    U.S.R. Murty
  • What is a graph ?
Write a Comment
User Comments (0)
About PowerShow.com