threaded binary tree and binary search tree

38
THREADED BINARY TREE AND BINARY SEARCH TREE

Upload: siddhi-shrivas

Post on 18-Aug-2015

73 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED BINARY TREE AND BINARY

SEARCH TREE

Page 2: 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.

Page 3: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 4: THREADED BINARY TREE AND BINARY SEARCH TREE

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

Page 5: THREADED BINARY TREE AND BINARY SEARCH TREE

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

Page 6: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE FIGURE :

A

H I

B

D E

C

GF

Inorder sequence: H, D, I, B, E, A, F, C, G

Page 7: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 8: THREADED BINARY TREE AND BINARY SEARCH TREE

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

LeftThread LeftChild RightChild RightThreaddata

Page 9: THREADED BINARY TREE AND BINARY SEARCH TREE

MEMORY REPRESENTATION OF THREADED TREE OF FIGURE 5.20

f - f

f A f

f B f

f D f

t H t t I t

t E t

f B f

f D f t E t

Page 10: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 11: THREADED BINARY TREE AND BINARY SEARCH TREE

INSERTION OF R AS A RIGHT CHILD OF S IN A THREADED BINARY TREE

s

r

s

r

before after

Page 12: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE EXAMPLE

8

75

3

11

13

1

6

9

Page 13: THREADED BINARY TREE AND BINARY SEARCH TREE

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

Page 14: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Start at leftmost node, print it

Output1

Page 15: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output13

Page 16: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output135

Page 17: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output1356

Page 18: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output13567

Page 19: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output135678

Page 20: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output135678

Page 21: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output13567891113

Page 22: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE TRAVERSAL

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output135678911

Page 23: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE MODIFICATION We’re 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

Page 24: THREADED BINARY TREE AND BINARY SEARCH TREE

THREADED TREE MODIFICATION

8

75

3

11

13

1

6

9

Page 25: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 26: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 27: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 28: THREADED BINARY TREE AND BINARY SEARCH TREE

BINARY TREES

20

15 25

14 10 22

30

5 40

2

60

70

8065

Not binary search tree

Binary search trees

Page 29: THREADED BINARY TREE AND BINARY SEARCH TREE

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 it’s

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.

Page 30: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 31: THREADED BINARY TREE AND BINARY SEARCH TREE

SEARCHING A BINARY SEARCH TREE BY RANKtemplate <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;

}

Page 32: THREADED BINARY TREE AND BINARY SEARCH TREE

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.

Page 33: THREADED BINARY TREE AND BINARY SEARCH TREE

INSERTING INTO A BINARY SEARCH TREE

30

5 40

2 80

30

5 40

2 8035

Page 34: THREADED BINARY TREE AND BINARY SEARCH TREE

INSERTION INTO A BINARY SEARCH TREETemplate <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 pBstNode<Type> *p = root; BstNode<Type> *q = 0;while(p) {

q = p;if (x.key == p->data.key) return FALSE; // x.key is already in treeif (x.key < p->data.key) p = p->LeftChild;else p = p->RightChild;

}

// Perform insertionp = 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;

}

Page 35: THREADED BINARY TREE AND BINARY SEARCH TREE

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)

Page 36: THREADED BINARY TREE AND BINARY SEARCH TREE

25

DELETING FROM A BINARY SEARCH TREE

30

40

2 80

30

5 40

2 8035

30

Page 37: THREADED BINARY TREE AND BINARY SEARCH TREE

DELETING FROM A BINARY SEARCH TREE

25

30

40

2 80

30

25

30

40

2 80

5

22

30

40

80

5

Page 38: THREADED BINARY TREE AND BINARY SEARCH TREE

THANK YOU