Академический Документы
Профессиональный Документы
Культура Документы
B C
D E F G
H I
Inorder sequence: H, D, I, B, E, A, F, C, G
Threaded Binary Tree with
Head Node
LeftThread LeftChild data RightChild RightThread
f - f
f A f
f B f f B f
f D f t E t f D f t E t
t H t t I t
Manipulation of Threaded
Binary Tree
Traversal (Inorder Stack)
Insert a Node
Delete a Node
Insert a Node to Threaded
Binary Tree
s s
r r
before after
Insert a Node to Threaded
Binary Tree
s s
r r r
before after
Heaps
Definition
A max (min) tree is a tree in which the key value in
each node is no smaller (larger) than the key
values in its children (if any).
A max heap is a complete binary tree that is also
a max tree.
A min heap is a complete binary tree that is also a
min tree.
We can use the max heap to implement the
priority Queues.
Priority Queues
14 9
12 7 6 3
10 8 6 5
Manipulation of The Heap
20 20
15 2 15 25
14 10 14 10 25
Insertion into a Max Heap
20 20
15 2 15 25
14 10 14 10 21
Deletion from a Max Heap
O(logn)
20 20
15 2 15 2
14 10 14 10
15
Binary Search Tree
Definition
A binary serach tree is a binary tree. It may be
empty. If it is not empty then it satisfies the
following properties:
Every element has a key and no two elements have the
same key (i.e., the keys are distinct)
The keys (if any) in the left subtree are smaller than the
key in the root.
The keys (if any) in the right subtree are larger than the
key in the root.
The left and right subtrees are also binary search trees.
Binary Search Tree Examples
30 60
20
5 40 70
15 25
65 80
14 10 22 2
30 30
5 40 5 40
2 80 2 35 80
Deletion from a Binary
Search Tree
Delete a leaf node
A leaf node which is a right child of its parent
A leaf node which is a left child of its parent
Delete a non-leaf node
A node that has one child
A node that has two children
Replaced by the largest element in its left subtree, or
Replaced by the smallest element in its right subtree
Again, the delete function has complexity of
O(h)
Deletion from a Binary
Search Tree
30
5 40
2 35 80
Deletion from a Binary
Search Tree
30 30
5 40 5 37
2 35 80 2 35 80
32 37 90 32 90
Deletion from a Binary
Search Tree
30
30
5 80
5 40
2 35 90
2 35 80
32 37
32 37 90
Joining and Splitting Binary
Trees
C.ThreeWayJoin(A, x, B)
Creates a binary search tree C that consists of binary
search tree A, B, and element x.
C.TwoWayJoin(A, B)
Joins two binary search trees A and B to obtain a single
binary search tree C.
A.Split(i, B, x, C)
Binary search tree A splits into three parts: B (a binary
search tree that contains all elements of A that have key
less than i); if A contains a key i than this element is copied
into x and a pointer to x returned; C is a binary search tree
that contains all records of A that have key larger than i
ThreeWayJoin(A, x, B)
81
30 90
5 40 81 85 94
2 35 80 84 92
A x B
C.TwoWayJoin(A, B)
84
80
30 90
5 40 85 94
2 35 80 84 92
A B
A.Split(i, B, x, C)
30 x i = 30
5 40
2 35 80
B
75 81
A C
A.Split(i, B, x, C)
i = 80 30 t Y L Z R
30 L 81
5 40 t R
5 40 L C
2 35 80 t
2 35 75 80
75 81
x
A B