Title: Trees 2
1Trees (2)
- Threaded Binary Search Trees
- Tree Balancing AVL Trees
- Other trees
2Review of Traversals in Binary Search Tree
- Preorder
- Inorder
- Postorder
- Recursive
- Complexity
3Links 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
4Threaded 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.
5Example
H
E
K
L
B
F
J
G
M
I
D
A
C
Nodes with no right child
6Example continue Right pointer
H
E
K
L
B
F
J
G
M
I
D
A
null
C
Nodes with no right child
7Inorder traversal on the threaded BST
H
E
K
L
B
F
J
G
M
I
D
A
null
C
8Algorithm 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
9Homework
10Tree 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)
11Balance 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
12AVL 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
13How 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)
14Simple 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
15Left-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
16Left-Right rotation continue
P
O
O
R
P
G
G
Right rotation next Including the ancestor
R
M
M
D
D
17Homework Exercise 13.2
- 6-10 (construct the BST using AVL insertion
algorithm)
18Other 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
192-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
20B-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
21Tree 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 ?