You are on page 1of 22

Binary Search Trees

Determining the Number of Nodes :


Number of nodes =
Total Nodes(left sub–tree) + Total Nodes(right sub–tree) + 1
Algorithm to calculate:

1
Binary Search Tree

Total nodes of left sub-tree = 1


Total nodes of left sub-tree = 5
Total nodes of tree = (1+5) + 1
Total nodes of tree = 7
2
Binary Search Tree
Determining the Number of Internal Nodes:
Number of Internal Nodes = Total Internal Nodes(left sub–
tree) + Total Internal Nodes(right sub– tree) + 1

3
Binary Search Tree

Total internal nodes of left sub-tree = 0


Total internal nodes of left sub-tree = 3
Total internal nodes of tree = (0+3) + 1
Total internal nodes of tree = 4

4
Binary Search Tree
Determining the Number of External Nodes:
Number of external nodes = Total External Nodes(left sub–
tree) + Total External Nodes (right sub– tree)

5
Binary Search Tree

Total external nodes of the left sub–tree = 1


Total external nodes of the left sub–tree = 2
Total external nodes of the tree = 1 + 2
Total external nodes of the tree = 3

6
Binary Search Tree - BST
Finding the Smallest Node in a BST:
- The value of the leftmost node of the left sub-tree
- If left sub-tree is NULL, then value of root node (smallest)

7
Binary Search Tree - BST
Finding the Largest Node in a BST:

8
Binary Search Tree
Deleting a Binary Search Tree:
To delete/remove an entire BST from the memory.

9
Threaded Binary Tree
- Same as BT but difference in storing NULL pointers.
- to avoid the NULL value the threads are used.

- Left Thread(lth) – Predecessor


- Right Thread(rth) - Successor
10
Threaded Binary Tree
- In the linked representation:
- NULL entries replaced to store a pointer -threads
- Binary trees containing threads - threaded trees.
- Threads denoted using arrows.

11
Threaded Binary Tree
Create Threaded Binary Tree: NULL pointers are avoided,
instead, left NULL ptr --- > inorder predecessor and
right NULL ptr --- > inorder successor
- 2 additional nodes lth & rth – dummy nodes, indicates
thread is present or not.
If thread exists, set lth or rth = 1 else 0.

12
Create Threaded Binary Tree
Example : 10, 20, 30, 40, 50

13
Create Threaded Binary Tree
Example : 10, 20, 30, 40, 50

14
Create Threaded Binary Tree
Example : 10, 20, 30, 40, 50

15
Threaded Binary Tree
- Types:
- 1. One-way threaded tree 2. Two way threaded tree
1-way threaded tree: single-threaded tree or left-threaded BT
- If the thread appears in the left field, then the left field will
be made to point to the in-order predecessor of the node.
2-way threaded tree: right threaded BT or double-threaded
tree or fully threaded binary tree.
- threads will appear in both the left and the right field of the
node.
- If the thread appears in the right field, then it will point to the
in-order successor of the node.

16
Linked representation Binary Tree

17
Linked representation Binary Tree

18
Linked representation Binary Tree

19
Threaded Binary Tree
Advantages TBT:
- Wastage of memory utilized using threads
- Predecessor & successor of any node accessed effectively

Disadvantages:
- Insertion and deletion of nodes – complex and link
manipulation is required (insertion).

20
Threaded Binary Tree

21
THANK YOU

22

You might also like