Sie sind auf Seite 1von 38

THREADED BINARY

TREE AND BINARY


SEARCH TREE
TREES
Definition: A tree is a finite set of one or
more nodes such that:

There is a specially designated node called the


root.
The remaining nodes are partitioned into n 0
disjoint sets T1, , Tn, where each of these sets
is a tree. We call T1, , Tn the subtrees of the
root.
THREADED BINARY TREE

In a linked representation of binary tree,


there are more null links can be replaced by
pointers, called threads to other nodes. A
left null link of the node is replaced with the
address of its inorder predecessor , similarly
a right null link of node is replaced with the
address of its inorder successor.
THREADED TREES
Binary trees have a lot of wasted space: the
leaf nodes each have 2 null pointers
We can use these pointers to help us in
inorder traversals
We have the pointers reference the next
node in an inorder traversal; called threads
We need to know if a pointer is an actual link
or a thread, so we keep a boolean for each
pointer
THREADED BINARY TREE
Threading Rules
A 0 Right Child field at node p is replaced by a
pointer to the node that would be visited after
p when traversing the tree in inorder. That is,
it is replaced by the inorder successor of p.
A 0 Left Child link at node p is replaced by a
pointer to the node that immediately precedes
node p in inorder (i.e., it is replaced by the
inorder predecessor of p).
THREADED TREE FIGURE :

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

LeftThread LeftChild data RightChild RightThread

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

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
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.

Non-recursive preorder traversal can be


implemented without a stack.
Non-recursive inorder traversal can be
implemented without a stack.
Non-recursive postorder traversal can be
implemented without a stack.
BINARY SEARCH TREE
Binary search tree
Every element has a unique key.
The keys in a nonempty left sub tree (right sub tree)
are smaller (larger) than the key in the root of
subtree.
The left and right subtrees are also binary search
trees.
BINARY SEARCH TREE
Heap needs O(n) to perform deletion of a non-priority
queue. This may not be the best solution.
Binary search tree provide a better performance for
search, insertion, and deletion.
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 TREES

30 60
20

5 40 70
15 25

65 80
14 10 22 2

Not binary Binary search


search tree trees
SEARCHING A BINARY SEARCH
TREE
If the root is 0, then this is an empty tree.
No search is needed.
If the root is not 0, compare the x with
the key of root.
If x equals to the key of the root, then its
done.
If x is less than the key of the root, then no
elements in the right subtree can have key
value x. We only need to search the left tree.
If x larger than the key of the root, only the
right subtree is to be searched.
SEARCH BINARY SEARCH TREE
BY RANK
To search a binary search tree by the
ranks of the elements in the tree, we
need additional field LeftSize.
LeftSize is the number of the elements in
the left subtree of a node plus one.
It is obvious that a binary search tree of
height h can be searched by key as well as
by rank in O(h) time.
SEARCHING A BINARY SEARCH
TREE BY RANK
template <class Type>
BstNode <Type>* BST<Type>::Search(int k)
// Search the binary search tree for the kth smallest element
{
BstNode<Type> *t = root;
while(t)
{
if (k == t->LeftSize) return n;
if (k < t->LeftSize) t = t->LeftChild;
else {
k -= LeftSize;
t = t->RightChild;
}
}
return 0;
}
INSERTION TO A BINARY SEARCH
TREE
Before insertion is performed, a search
must be done to make sure that the value
to be inserted is not already in the tree.
If the search fails, then we know the
value is not in the tree. So it can be
inserted into the tree.
It takes O(h) to insert a node to a binary
search tree.
INSERTING INTO A BINARY
SEARCH TREE

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

Das könnte Ihnen auch gefallen