Beruflich Dokumente
Kultur Dokumente
B C
D E F G
H I
Inorder sequence: H, D, I, B, E, A, F, C, G
THREADS
To distinguish between normal pointers and
threads, two boolean fields, LeftThread and
RightThread, are added to the record in
memory representation.
t->LeftChild = TRUE
=> t->LeftChild is a thread
t->LeftChild = FALSE
=> t->LeftChild is a pointer to the left child.
THREADS
To avoid dangling threads, a head node is
used in representing a binary tree.
The original tree becomes the left subtree of
the head node.
Empty Binary Tree
TRUE FALSE
MEMORY REPRESENTATION OF
THREADED TREE OF FIGURE 5.20
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
INSERTING A NODE TO A
THREADED BINARY TREE
Inserting a node r as the right child of a node s.
If s has an empty right subtree, then the insertion is
simple and diagram in Figure 5.23(a).
If the right subtree of s is not empty, the this right
subtree is made the right subtree of r after insertion.
When thisis done, r becomes the inorder predecessor of
a node that has a LdeftThread==TRUE field, and
consequently there is an thread which has to be updated
to point to r. The node containing this thread was
previously the inorder successor of s. Figure 5.23(b)
illustrates the insertion for this case.
INSERTION OF R AS A RIGHT CHILD
OF S IN A THREADED BINARY TREE
s s
r r
before after
THREADED TREE EXAMPLE
6
3 8
1 5 7 11
9 13
THREADED TREE TRAVERSAL
We start at the leftmost node in the tree,
print it, and follow its right thread
If we follow a thread to the right, we output
the node and continue to its right
If we follow a link to the right, we go to the
leftmost node, print it, and continue
THREADED TREE TRAVERSAL
Output
6 1
3 8
1 5 7 11
9 13
Start at leftmost node, print it
THREADED TREE TRAVERSAL
Output
6 1
3
3 8
1 5 7 11
9 13
Follow thread to right, print
node
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
1 5 7 11
9 13
Follow thread to right, print
node
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9 13
Follow thread to right, print
node
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9 13
Follow thread to right, print
node
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9
11
9 13 13
3 8 5
6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print
node
THREADED TREE MODIFICATION
Were still wasting pointers, since half of our
leafs pointers are still null
We can add threads to the previous node in
an inorder traversal as well, which we can
use to traverse the tree backwards or even
to do postorder traversals
THREADED TREE MODIFICATION
6
3 8
1 5 7 11
9 13
Advantages of threaded binary tree.
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 TREES
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
INSERTION INTO A BINARY
SEARCH TREE
Template <class Type>
Boolean BST<Type>::Insert(const Element<Type>& x)
// insert x into the binary search tree
{
// Search for x.key, q is the parent of p
BstNode<Type> *p = root; BstNode<Type> *q = 0;
while(p) {
q = p;
if (x.key == p->data.key) return FALSE; // x.key is already in tree
if (x.key < p->data.key) p = p->LeftChild;
else p = p->RightChild;
}
// Perform insertion
p = new BstNode<Type>;
p->LeftChild = p->RightChild = 0; p->data = x;
if (!root) root = p;
else if (x.key < q->data.key) q->LeftChild = p;
else q->RightChild = p;
return TRUE;
}
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)
DELETING FROM A BINARY
SEARCH TREE
30
30
5 40 2
5 40
2 35 80 2 80
DELETING FROM A BINARY
SEARCH TREE
30 5
30 5
30
2
5 40 2
5 40 2 40
2 80 2 80 80
THANK YOU