This document discusses binary search trees and threaded binary trees. It defines a binary search tree as a binary tree where every node contains a unique key and the left subtree of a node contains keys smaller than the node's key and the right subtree contains larger keys. It also describes the basic operations of search, insertion, and deletion in a binary search tree. A threaded binary tree is similar to a binary tree but stores pointers to predecessors and successors instead of null pointers to allow linear traversal.
Original Description:
Original Title
Z00820040220174212Z00820010220154080COMP6048Pert12 - Introduction to Binary Search Tree and Threaded Binary Tree Rev
This document discusses binary search trees and threaded binary trees. It defines a binary search tree as a binary tree where every node contains a unique key and the left subtree of a node contains keys smaller than the node's key and the right subtree contains larger keys. It also describes the basic operations of search, insertion, and deletion in a binary search tree. A threaded binary tree is similar to a binary tree but stores pointers to predecessors and successors instead of null pointers to allow linear traversal.
This document discusses binary search trees and threaded binary trees. It defines a binary search tree as a binary tree where every node contains a unique key and the left subtree of a node contains keys smaller than the node's key and the right subtree contains larger keys. It also describes the basic operations of search, insertion, and deletion in a binary search tree. A threaded binary tree is similar to a binary tree but stores pointers to predecessors and successors instead of null pointers to allow linear traversal.
Tree and Threaded Binary Tree Learning Outcomes At the end of this session, student will be able to: • Demonstrate construction process of binary search tree (LO1 & LO3)
COMP6048 - Data Structure 2
Sub Topics Binary Search Tree: - Binary Search Tree Concept - Operations: Search, Insertion, Deletion - Threaded Binary Tree Concept - Advantages
COMP6048 - Data Structure 3
Binary Search Tree Concept Binary Search Tree (BST) is a binary tree with the following properties: – Every node has a key and no two nodes have the same key (keys are unique). – The keys in left sub tree are smaller than the key in the root of the sub tree. – The keys in right sub tree are larger than the key in the root of the sub tree. – The left and right sub trees are also binary search tree (recursively).
COMP6048 - Data Structure 4
Binary Search Tree Concept
Pay attention to the keys stored in each node
COMP6048 - Data Structure 5
Binary Search Tree Operations Binary Search Tree has the following basic operations: – find(x) : find key x in the BST – insert(x) : insert new key x into BST – remove(x) : remove key x from BST
COMP6048 - Data Structure 6
Operations: Search • Because of the property of BST, finding/searching in BST is easy.
• Let the key that we want to search is X.
– We begin at root – If the root contains X then search terminates successfully. – If X is less than root’s key then search recursively on left sub tree, otherwise search recursively on right sub tree.
COMP6048 - Data Structure 7
Operations: Insertion • Inserting into BST is done recursively.
• Let the new node’s key be X,
– Begin at root – If X is less than node’s key then insert X into left sub tree, otherwise insert X into right sub tree – Repeat until we found an empty node to put X (X will always be a new leaf)
COMP6048 - Data Structure 8
Operations: Insertion – Example
Inserting new key (35)
COMP6048 - Data Structure 9
Operations: Insertion – Example
COMP6048 - Data Structure 10
Operations: Insertion – Example
COMP6048 - Data Structure 11
Operations: Insertion – Example
COMP6048 - Data Structure 12
Operations: Deletion • There are 3 cases which should be considered: – If the key is in a leaf, just delete that node – If the key is in a node which has one child, delete that node and connect its child to its parent – If the key is in a node which has two children, find the right most child of its left sub tree (node P), replace its key with P’s key and remove P recursively. (or alternately you can choose the left most child of its right sub tree)
COMP6048 - Data Structure 13
Operations: Deletion – Example
Deleting key (21)
COMP6048 - Data Structure 14
Operations: Deletion – Example
Deleting key (21),
(continue)
COMP6048 - Data Structure 15
Operations: Deletion – Example
Deleting key (37)
COMP6048 - Data Structure 16
Operations: Deletion – Example
Deleting key (37),
(continue)
COMP6048 - Data Structure 17
Operations: Deletion – Example
Deleting key (30)
COMP6048 - Data Structure 18
Operations: Deletion – Example
Deleting key (30),
(continue)
COMP6048 - Data Structure 19
Threaded Binary Tree Concept • A threaded binary tree is same as that of a binary tree but with a difference in storing NULL pointers. • Binary Tree:
COMP6048 - Data Structure 20
Threaded Binary Tree Concept In the linked representation, a number of nodes contain a NULL pointer either in their left or right fields or in both.
This space that is wasted in storing a NULL pointer can be
efficiently used to store some other useful peace of information.
COMP6048- Data Structure 21
Threaded Binary Tree Concept For example, the NULL entries can be replaced to store a pointer to the in-order predecessor, or the in-order successor of the node. These special pointers are called thread and binary trees containing thread are called threaded trees.
In the linked representation of a threaded binary tree,
threads will be denoted using dotted lines
COMP6048 - Data Structure 22
Threaded Binary Tree Concept • Binary Tree without threading
• Linked • representation of • the binary tree
COMP6048 - Data Structure 23
Threaded Binary Tree Concept • In one way threading, a thread will appear either in the right field or the left field of the node. • A one way threaded tree is also called a single threaded tree. • 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. • Such a one way threaded tree is called a left threaded binary tree. • On the contrary, if the thread appears in the right field, then it will point to the in-order successor of the node. Such a one way threaded tree is called a right threaded binary tree.
COMP6048 - Data Structure 24
Threaded Binary Tree Example
Binary tree with
one-way threading
COMP6048 - Data Structure 25
Threaded Binary Tree Example
Binary tree with
two-way threading
COMP6048 - Data Structure 26
Advantages • It enables linear traversal of elements in the tree • Linear traversal eliminates the use of stacks which in turn consume a lot of memory space and computer time • It enables to find the parent of a given element without explicit use of parent pointers • Since nodes contain pointers to in-order predecessor and successor, the threaded tree enables forward and backward traversal of the nodes as given by in-order fashion
COMP6048 - Data Structure 27
Summary • Binary Search Tree (BST) is a binary tree with properties: – Every node has a key and no two nodes have the same key (keys are unique). – Left sub tree’s keys are smaller than root’s key. – Right sub tree’s keys are larger than root’s key. – The left and right sub trees are also binary search tree (recursively). • A threaded binary tree is same as that of a binary tree but with a difference in storing NULL pointers
COMP6048 - Data Structure 28
References • Reema Thareja,. 2014. Data structures using C. OXFOR. New Delhi. ISBN:978-0-19-809930-7 Chapter 10 • Threaded Binary Tree http://quiz.geeksforgeeks.org/threaded-binary-tree/
COMP6048 - Data Structure 29
END Introduction to Binary Search Tree and Threaded Binary Tree