TREES
 Arrays
 Linked Lists
17 20 29 34 483020 34 29 30 4817
30 216 14 28 48
6 4814 21 28 30
Storing and modifying data
fast searching, slow insertion
slow searching, fast insertion
7
Hierarchically Structure
Trees
 A tree is often used to represent a hierarchy .
 Because the relationships between the items in the
hierarchy suggest the branches of a botanical tree.
 For example, a tree-like organization chart is often
used to represent the lines of responsibility in a
business as shown in Figure
Trees
Trees
 As we see in the figure, the tree is upside-down.
 This is the usual way the data structure is drawn.
 The president is called the ro o t of the tree and the
clerks are the le ave s.
 A tree is extremely useful for certain kinds of
computations.
 For example, suppose we wish to determine the total
salaries paid to employees by division or by
department.
 The total of the salaries in division A can be found by
computing the sum of the salaries paid in
departments A1 and A2 plus the salary of the vice-
president of division A.
Trees
 Similarly, the total of the salaries paid in
department A1 is the sum of the salaries of the
manager of department A1 and of the two clerks
below her.
 Clearly, in order to compute all the totals, it is
necessary to consider the salary of every
employee.
 Therefore, an implementation of this
computation must visit all the employees in the
tree.
 An algorithm that systematically visits all the
items in a tree is called a tre e trave rsal.
Trees in our life
 Tree is an important data structure that
represent a hierarchy
 Trees/hierarchies are very common in our life:
 Tree of species(class/type) (is-a)
 Component tree (part-of)
 Family tree (parent-child)
9
Tree Terminology
 A tre e is a collection of elements (nodes)
 Each node may have 0 or more succe sso rs
 (Unlike a list, which has 0 or 1 successor)
 Each node has e xactly o ne pre de ce sso r
 Except the starting / top node, called the ro o t
 Links from node to its successors are called
branche s
 Successors of a node are called its childre n
 Predecessor of a node is called its pare nt
 Nodes with same parent are sibling s
 Nodes with no children are called le ave s
10
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
A Tree Has a Root Node
ROOT NODE
11
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
Leaf nodes have no children
LEAF NODES
More Terminologies
 Path
 A sequence of edges
 Le ng th o f a path
 number of edges on the path
 De pth/Le ve lof a node
 length of the unique path from the root to that node
 He ig ht of a node
 length of the longest path from that node to a leaf
 all leaves are at height 0
 The height of a tree = the height of the root
= the depth of the deepest leaf
 Ance sto r and de sce ndant
 If there is a path from n1 to n2
 n1 is an ancestor of n2, n2 is a descendant of n1
 Pro pe r ance sto r and pro pe r de sce ndant
13
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
A Tree Has Levels
LEVEL 0
14
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
Level One
LEVEL 1
15
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
Level Two
LEVEL 2
16
Representation Of a General Tree
-- first child/next sibling
 Example for this tree:
A
B D
F G
H
E
C
A
null
First child Next sibling
B
E
null
H
null null
C
null
D
null
F
null null
G
null
Cannot directly access D from A.
ParentPtr
Key value
sibling1st child
Tree Formal Definitions
 A tree is a collection of nodes. The collection can be
empty,
 Otherwise, a tree consists of a distinguished node r,
called the root, and zero or more (sub) trees T1, T2,.
……, Tk, each of whose roots are connected by a
directed edge to r.
 The root of each subtree is said to be a child of r and
r is the parent of each subtree root.
Binary Tree
 The simplest form of tree is a binary tree. A binary tree
consists of
a. a node (called the root node) and
b. left and right sub-trees.
Both the sub-trees are themselves binary trees
 We now have a recursively defined data structure.
Binary Tree
What is a binary tree?
 Each node can have up to two successor
nodes (childre n)
 The predecessor node of a node is called its pare nt
 The "beginning" node is called the ro o t (no parent)
 A node without childre n is called a le af
21
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
A Subtree
LEFT SUBTREE OF ROOT NODE
22
Owner
Jake
Manager Chef
Brad Carol
Waitress Waiter Cook Helper
Joyce Chris Max Len
Another Subtree
RIGHT SUBTREE
OF ROOT NODE
Binary Trees
Some Binary Trees
One node Two nodes
Three nodes
Convert a Generic Tree to a Binary
Tree
Binary Search Trees
 Binary Search Tree Property: The value stored
at a node is greaterthan the value stored at its
left child and less than the value stored at its
right child.
Tree node structure
template<class ItemType>
class TreeNode {
ItemType info;
TreeNode* left;
TreeNode* right;
};
Binary Tree
Notation
In C++ the structure of binary tree
node can be specified as a
class named Node:
class Node{
public:
int key; // key stored in the node
Node *left; // link to left child
Node *right; // link to right child
};
parentp[x]
Using pseudo code convention, the links are
defined as follows:
x -pointer to a node
left[x]- pointer to left child of x right[x]-
pointer to right child of x p[x]-pointer to
parent of x
key[x]-key stored in node x
x key
left[x] right[x]
left child right child
Function RetrieveItem
Function RetrieveItem
 What is the base case(s)?
1) When the key is found
2) The tree is empty (key was not found)
 What is the general case?
Search in the left or right subtrees
Binary Tree Traversal
 Tre e Trave rsalis the process of visiting each node in
the tree exactly one time.
 Tree traversals are of two types
 Depth First Traversal
 Breadth First Traversal
 The three Depth First Traversal techniques are
 Pre o rde r tre e trave rsal
 Ino rde r tre e trave rsal
 Po sto rde r tre e trave rsal
On a visit to node the data stored
·Preorder Tree Traversal
is processed, printed, etc.
A1
In preorder traversal first the root is visited , then left subtree is visited
preorder , and finally the right subtree is visited preorder. B C
2
Preorder: A B C
A
·Inorder Tree Traversal
In inorder traversal first left subtree is visited inorder then root
is visited and finally the right subtree is visited inorder .
2
1
B C
Inorder: B A C
2A
·Postorder Tree Traversal
In postorder traversal first the left subtree is visited post order,
then right subtree is visited postorder, and finally root is visited . B C
1
Postorder:
B C A
In tree traversal each node is visited once only. Traversal is also referred to as walk.
The standard procedures of traversing a tree are known as preorder, inorder, and
postorder.
Binary Tree Traversal
Binary Tree Traversal
Euler Tour
Euler tour traversal of binary tree is a path around the tree
.
Tour starts from the root toward the left child. The tree edges are kept
to the left . Each node is encountered three times: from left, from below
and from right
Postorder lists nodes on left of Euler path. Inorder lists nodes above the path
Postorder lists nodes on the right.
Tree Traversal
Recursive Algorithms
The recursive procedures for tree traversal use
recursive functions. Initially, the pointer to the
root is passed as argument to the function.
POSTORDER(x)
1 if x ≠ nil
2 then POSTORDER(left[x]) ► Traverse left subtree
postorder
3 POSTORDER(right[x]) ► Traverse right subtree postorder
4 print key[x] ►Print key
INORDER(x)
1 if x ≠ nil
2 then INORDER(left[x]) ► Traverse left subtree inorder
3 print key[x] ► Print the key
4 INORDER(right[x]) ►Traverse right subtree inorder
PREORDER(x)
1 if x ≠ nil
2 then print key[x] ► Print key
3 PREORDER(left[x]) ► Traverse left subtree preorder
4 PREORDER(right[x]) ►Traverse right subtree preorder
Inorder Tree Traversal
Iterative Algorithm
The iterative procedure uses a stack to hold addresses of the
nodes waiting to be processed in traversal order. It includes
following steps:
Step #1:Push address of root on to the stack.
Step #2: Traverse the leftmost path of the, pushing the address of each node on to the
stack, and stopping when a leaf is reached.
Step #3: Pop the stack, and process the popped off node.
Step #4: If null is popped, exit, otherwise proceed to next step.
Step#5: If a right child of the processed node exists, push its address to stack.
Step#6: Repeat step # 2
Inorder Traversal
Step#:1Stack Step#:2Stack
Step#:4Step#:3
Step#:6
Step#:5
B
A
D
B
A
H
D
B
A
D
B
A
B
AA
Inorder Traversal-contd
Step#:8
Stack StackStep#:7
Step#:10
Step#:9
Step#:12
Step#:11
E
A
J
E
A
E
AA
B
A
I
B
A
Inorder Traversal-contd
Step#:13 Step#:14Stack
Stack
Step#:16Step#:15
Step#:17 Step#:18
C
C
F
C
A
Inorder Traversal: Example-contd
Step#:19 Step#:20Stack Stack
The inorder traversal of the sample binary tree is given by
H D I B J E A F C G
G
40
Tree size
int TreeSize (root: TreePointer)
begin
if root==null //this is le ft/rig ht child po int o f a le af
then return 0;
else
return 1 + TreeSize(root->left) + TreeSize(root->right);
end;
int TreeSize (root: TreePointer)
begin
if root==null //this is le ft/rig ht child po int o f a le af
then return 0;
else
return 1 + TreeSize(root->left) + TreeSize(root->right);
end;
Size of a Node: the number of
descendants the node has (including
the node itself). The size of root is
the size of a tree. The size of a leaf is
1.
Properties of Binary Trees
 A binary tree is aA binary tree is a fullfull binary tree if and only if:binary tree if and only if:
 Each non leaf node hasEach non leaf node has exactly twoexactly two child nodeschild nodes
 All leaf nodes lies at the same levelAll leaf nodes lies at the same level
 It is calledIt is called fullfull since all possible node slots aresince all possible node slots are
occupiedoccupied
COMPLETE BINARY TREE
A complete binary tree of depth d is the strictly binary
tree all of whose leaves are at level d.
A
CB
F GD
E
H
I
J K
L
M
N O
ALMOST COMPLETE BINARY TREE
A binary tree of depth d is an almost binary tree if
–Any node n at level less than d - 1 has two sons
–For any node n in the tree with a right or left child at level I, the node must have left
child (if it has right child) and all the nodes on the left hand side of the node must have
two children.
Not an almost complete tree
Violate condition 1 Almost complete binary tree
A Full Binary Tree - Example
Complete Binary Trees - Example
BB
AA
CC
DD EE
HH II JJ KK
FF GG
Figure 13.8 A complete binary treeFigure 13.8 A complete binary tree
Strictly Binary Trees
 A binary tree in which every non-leaf node has non-
empty left and right sub-trees
 A strict binary tree with n leaves always contain 2n-1
nodes.
 Level of a node = depth of a node
Almost Complete Binary Tree
 A binary tree of depth d is an almost complete
binary tree if
 Any node n at level less than d - 1 has two sons
(complete tree until level d-1)
 For any node n in the tree with a right or left child
at level d, the node must have left child (if it has
right child) and all the nodes on the left hand side
of the node must have two children.
Almost Complete Binary Tree
Examples:
Not an almost complete tree
Violate condition 1
Not an almost complete tree
Violate condition 2
Almost Complete Binary Tree
Almost complete binary tree
1
2 3
4 5 6 7
8 9
Almost complete binary tree
but not strictly binary tree
7
1
2 3
4
5 6
8 9
10
Numbering of an almost complete binary tree
Numbering of an Almost
Complete Binary Tree
Almost complete binary tree
1
2 3
4 5 6 7
8 9
Almost complete binary tree
but not strictly binary tree
1
2 3
4
5 6 7
8 9
10
n nodes of an almost complete binary tree can be numbered from 1 to n
Function
Insert Item
 Use the
binary
search tree
property to
insert the
new item at
the correct
place
Function
InsertItem
(cont.)
• Implementing
insertion using
recursion
Insert 11
 What is the base case(s)?
The tree is empty
 What is the general case?
Choose the left or right subtree
Function InsertItem (cont.)
template<class ItemType>
void TreeType<ItemType>::InsertItem(ItemType item)
{
Insert(root, item);
}
template<class ItemType>
void Insert(TreeNode<ItemType>*& tree, ItemType item)
{
if(tree == NULL) { // base case
tree = new TreeNode<ItemType>;
tree->info = item;
}
else if(item < tree->info)
Insert(tree->left, item);
else
Insert(tree->right, item);
}
Function InsertItem (cont.)
Binary Tree Traversal
 Breadth First Tree Traversal
 Implementation of this kind of traversal is
straightforward when a queue is used.
 Consider a top down left-to-right, breadth first
traversal.
 After a node is visited, its children, if any are
placed at the end of a queue, and the node at
the beginning of the queue is visited.
Breadth First Traversal
Example
A
C
D
B
E
F
G
I
H
Breadth First Traversal
Example
C
Queue
B
H
A
C
D
B
E
F
G
I
A
Breadth First Traversal
Example
Dqueue C
B
H
A
C
D
B
E
F
G
I
AC
Breadth First Traversal
Example
B
Enqueu D
D
H
A
C
D
B
E
F
G
I
AC
Breadth First Traversal
Example
Dqueue B
D
H
A
C
D
B
E
F
G
I
ACB
Breadth First Traversal
Example
D
Enqueue E, H
E H
H
A
C
D
B
E
F
G
I
ACB
Breadth First Traversal
Example
Dqueue D
E H
H
A
C
D
B
E
F
G
I
ACBD
Breadth First Traversal
Example
Dqueue E
H
H
A
C
D
B
E
F
G
I
ACBDE
Breadth First Traversal
Example
Enqueue F, G
H F G
H
A
C
D
B
E
F
G
I
ACBDE
Breadth First Traversal
Example
Dqueue H
F G
H
A
C
D
B
E
F
G
I
ACBDEH
Breadth First Traversal
Example
I
Enqueue I
F G
H
A
C
D
B
E
F
G
I
ACBDEH
Breadth First Traversal
Example
H
A
C
D
B
E
F
G
I
I
Dqueue F
G
ACBDEHF
Breadth First Traversal
Example
H
A
C
D
B
E
F
G
I
I
Dqueue G
ACBDEHFG
Breadth First Traversal
Example
H
A
C
D
B
E
F
G
I
Dqueue I
ACBDEHFGI
Simple Search Algorithm
 Let us now state a simple search algorithm that will try to give you an
idea about the sort of data structures that will be used while searching,
and the stop criteria for your search. The strength of the algorithm is
such that we will be able to use this algorithm for both Depth First
Search (DFS) and Breadth First Search (BFS).
Let S be the start state
1. Initialize Qwith the start node Q=(S) as the only entry; set Visited =
(S)
2. If Qis empty, fail. Else picknode XfromQ
3. If Xis a goal, return X, we’ve reached the goal
4. (Otherwise) Remove XfromQ
5. Find all the children of state Xnot in Visited
6. Add these to Q; Add Children of Xto Visited
7. Go to Step 2
Simple Search Algorithm
 Here Q represents a priority queue. The algorithm is
simple and doesn’t need much explanation. We will use
this algorithm to implement blind and uninformed
searches. The algorithm however can be used to
implement informed searches as well.
Simple Search Algorithm Applied
to Depth First Search
 Depth First Search dives into a tree deeper and
deeper to fine the goal state.
 As mentioned previously we will give priority to
the element with minimum P(n) hence the node
with the largest value of height will be at the
maximum priority to be picked from Q. The
following sequence of diagrams will show you
how DFS works on a tree using the Simple
Search Algorithm.
We start with a tree containing nodes S, A, B, C, D, E, F, G, and H, with H as
the goal node. In the bottom left table we show the two queues Q and
Visited. According to the Simple Search Algorithm, we initialize Q with the
start node S,
• If Q is not empty, pick the node X with the minimum P(n) (in this case S), as it is
• the only node in Q. Check if X is goal, (in this case X is not the goal). Hence find
• all the children of X not in Visited and add them to Q and Visited. Goto Step 2.
Again check if Q is not empty, pick the node X with the minimum P(n) (in
this case either A or B), as both of them have the same value for P(n).
Check if X is goal, (in this case A is not the goal). Hence, find all the
children of A not in Visited and add them to Q and Visited. Go to Step 2.
Go on following the steps in the Simple Search Algorithm till you find a
goal node.
The diagrams below show you how the algorithm proceeds.
Here, from the 5th row of the table when we remove H and check if it’s the
goal, the algorithm says YES and hence we return H as we have reached
the goal state.
 The diagrambelow also shows that DFS didn’t have to search the
entire search space, ratheronly by traveling in half the tree, the
algorithmwas able to search the solution.
Simple Search Algorithm Applied
to Breadth First Search
 Self study
Problems with DFS and BFS
 ????
Binary Search Trees
Binary Search Tree
Definition
A binary search tree (BST) is organized such that
key in a node is larger than any key in the left
subtree and smaller than any key in right subtree.
Iif x is pointer to a tree node then binary-search-tree property implies
key[x] > key[ left[x] ]
key[x] < key[right[x]] for all x
In BST, nodes with duplicate keys are not allowed.
Binary Search Tree
Binary Search Tree
Operations
Common operations performed on binary search tree are :
·INSERT- Add a new node with a given key
·SEARCH-Search a node with a given key
·DELETE-Delete a node with a given key
·MAXIMUM-Find largest key in the tree or in a subtree
·MINIMUM-Find smallest key in the tree or in a subtree
·SUCCESSOR- Find a node with smallest key which larger than the key in given node
·TRAVERSE- Traverse tree inorder, preorder, or postorder
Binary Search Tree
Insertion-1
Inserting a new node with key 10 into a Binary Search Tree
Binary Search Tree
Insertion-2
Insertion key 10 compared with key in the root node. It is smaller .
Next, the left child is examined.
Binary Search Tree
Insertion-3
Insertion key 10 compared with key in the left child. It is smaller .
Next, the left child is examined.
Binary Search Tree
Insertion-4
Insertion key 10 compared with key in the left child . It is smaller .
Next, the left child is examined.
Binary Search Tree
Insertion-5
Insertion key 10 compared with key in the left child. It is larger .
Next, the right child is examined.
Binary Search Tree
Insertion-6
Insertion key 10 compared with key in the right child. It is smaller .
Next, the left child is examined.
Binary Search Tree
Insertion-7
Insertion key 10 compared with key in the left child. It is larger .
Next, the right child is examined.
Binary Search Tree
Insertion-8
Insertion key 10 compared with key in the right child. It is smaller .
Node with key 11 is a leaf. The new key is inserted as left child of the leaf
being examined
Binary Search
Insertion-9
Tree
key 10 is placed in new left child
Binary Search Tree
INSERT Method
The INSERT Method adds a new node. The pointer to new node is
passed as argument. to the insert method. Starting with root, the
input key is compared successively with left or right child until a leaf
is reached.. The link pointer of the leaf is reset to point to the new
node.
INSERT(T, z) ► z is pointer to new node
1 y←NIL ► y is a temporary pointer used to track the parent
2 x←root[T] ►x is another pointer to track current node. It is set initially to
point to root
3 while x ≠ NIL
4 do y←x
5 if key[z] <key[x] ►Compare given key with the key in node being
examined
6 then x←left[x] ►If given key is smaller proceed to left child
7 else x←right[x] ►If given key is larger proceed to right child
8 p[z]←y ► y holds the pointer to parent of the node being examined
9 if y=NIL ►The node being added is the first node ( has no parent)
10 then root[T] ← z
11 else if key[z] < key[y] ►Check whether new node to be added is left child or
right child
12 then left[y]←z ►If left child, reset the parent node pointer link to left
Binary Search
Searching -1
Binary tree is searched for node
with key=71
Tree
Binary Search Tree
Searching -2
Search key 71 is compared with the key in root. It is larger.
Next, the right child is examined
Binary Search Tree
Searching -3
Search key 71 is compared with the key 85 in right child. It is
smaller.
Next, the left child is examined
Binary Search Tree
Searching -4
Search key 71 is compared with the key 81 in left child. It is smaller.
Next, the left child is examined
Binary Search Tree
Searching -5
Search key 71 is compared with the key 68 in left child. It is larger.
Next, the right child is examined
Binary Search Tree
Searching -6
Search key 71 is compared with the key 76 in left child. It is smaller.
Next, the left child is examined
Binary Search
Searching -7
Search key 71 matches. Search is successful.
Tree
Binary Search Tree
SEARCH Method
SEARCH(x, k) ►x is pointer to root and k is given key
1 while x ≠ NIL and k ≠ key[x] ► Loop until a match is found or a leaf is
reached
2 do if k < key[x] ► Check if given key is smaller
3 then x←left[x] ► Proceed to left child if key smaller
4 else x←right[x] ► Proceed to right child if key is larger
5 return x ►Return NIL if no match found and pointer to node with matched
key
The SEARCH method finds a node with a given key. The search
procedure starts with the root. It successively compares keys in
the left or right children, depending on whether the key in the
parent node is larger or smaller than the given key. The search
terminates when either a matching key is found, or a leaf is
reached. The method returns a pointer to the node which has the
matching key or null pointer if no match is found..
Binary Search Tree
Smallest Key
The smallest key is found by starting with root
node and traversing leftmost path of BST
Binary Search Tree
Minimum key in a Subtree
The minimum key is found by starting with root node of the subtree and
traversing leftmost path of in the subtree
BinarySearchTree
MINIMUM Method
The MINIMUM method finds the smallest key in a binary
search tree or in any of the subtrees. It starts with the root
,or root of subtree, and proceeds down the leftmost path in
the tree or subtree.
The procedure returns pointer to the node with the smallest key.
MINIMUM(x)►x is pointer to root, or root of the subtree
1 while left[x] ≠ NIL ►Move down the leftmost path until
leaf is reached
2 do x ←left[x] ►Move to left child
3 return x ►Return pointer to the leaf , which contains the smallest
key
Binary Search Tree
Largest Key
The largest key is found by starting with root node and
traversing rightmost path of BST
Binary Search Tree
Maximum Key in a Subtree
The maximum key is found by starting with root node of the subtree and
traversing rightmost path of in the subtree
Binary Search Tree
MAXIMUM Method
The MAXIMUM method finds the largest key in a binary
search tree or in any of the subtrees.It starts with the root ,or
root of subtree, and proceeds down the rightmost path in the
tree or subtree.
The procedure returns pointer to the node with the largest key.
MAXIMUM(x) ►x is pointer to root or root of the subtree
1 while right[x] ≠ NIL ►Move down the rightmost path
until leaf is reached
2 do x ←right[x] ►Move to right child
3 return x ►Return pointer to the leaf , which contains the
largest key
Binary Search Tree
Deleting
a leaf node
Leaf Node -1
Node with key 73 is
Binary Search Tree
Deleting Leaf Node -2
First, the node with key 73 is searched. The key 73 is compared
successively with keys 38, 82,47,62,75 until it matches with key in
a node. The search path is highlighted
Binary Search Tree
Deleting Leaf Node-3
Node with key 73 is de-linked from the parent node with
key 75, by setting the left link-pointer in parent node to
null
Binary Search Tree
Deleting Leaf Node-4
Finally, the de-linked node with key 73 is
removed
Binary Search Tree
Deleting Node With Single Child-1
Node with key 7 has a single left child with key 22
Binary Search Tree
Deleting Node With Single Child-2
First, the node with key 7 is searched. The key is compared
successively with keys 38,25,and 6, until a match is found The
search path is highlighted. The sub-tree, rooted at the node to be
deleted, is shown in yellow shade.
Binary Search Tree
Deleting Node With Single Child-3
The node to be deleted with key 7, is de-linked from its parent
node with key 6 and its right child with key 22
The right child link-pointer of parent node with key 6, is reset
to point to the node with key 22
Binary Search Tree
The de-linked node with key 7 is removed
Deleting Node With Single Child-4
Binary Search Tree
The BST after node with 7 has been removed
Deleting Node With Single Child-5
Binary Search Tree
Deleting Node With Both Children-1
The node with key 33 is to be deleted. It
keys 32 and 41.
has both left and right children with
Binary Search Tree
Deleting Node With Both Children-2
First, the node with key 33 is searched. The key 33 is successively
compared with the keys 47 and 25 until a match is found. The
search path is highlighted.
Binary Search Tree
Deleting Node With Both Children-3
In order to delete the node, its in-order successor is searched, which
contains smallest of all the keys contained in the right sub-tree. Sub-
tree is traversed by following the leftmost path, beginning with sub-
tree node with .The search path is shown in yellow shade.
The in-order successor of node with key 33 is the node with key 34.
Binary Search Tree
Deleting Node With Both Children-4
in the node that contains key 33.The key 34 of in-order successor is to be copied
Binary Search Tree
Deleting Node With Both Children-5
The in-order successor with key 34 is de-linked from its parent node with key 38.
Binary Search Tree
Deleting Node Both Children-6
The key 34 in the in-order successor
The in-order successor is deleted.
is copied into the node that holds key 33.
Binary Search Tree
Deleting Node Both Children-7
The node with key 33 is removed. The BST
property is preserved.
Binary Search Tree
SUCCESSOR Method
SUCCESSOR(x) ►x is pointer to given node
1 If right[x] ≠ NIL ►If right subtree exists,then return pointer to node
with smallest key
2 then return MINUMUM(x)
3 y ←p[x] ►Proceed upward to parent node
4 while y ≠ NIL and x=right[y] ►Continue until root is reached, or else a
node with left child is found
5 do x ← y
6 y← p[y] ► Move to parent node
7 return y ► Return pointer to the successor node
The SUCCESSOR method finds the inorder successor of a node with given key. If the right
subtree of the node exists, then the method finds the smallest key in the subtree. If the right
subtree does not exist, then the method proceeds upward until an ancestor with a node with
a left child of its parent is encountered.. It returns pointer to the successor node.
Binary Search Tree
DELETE Method
DELETE(T, z)
1 if left[z]=NIL or right[z]=NIL ►Check if node has no left or right child
2 then y ← z ►if left or right child exists then y holds pointer to current node
3 else y ← SUCCESSOR(z) ►otherwise, y holds pointer to the successor node
4 if left[y] ≠ NIL ► check if the left child exists
5 then x← left[x] ►if so, move to left child
6 else x← right[y] ►otherwise, move to right child
7 if x ≠ NIL
8 then p[x] ← p[y] ► Set pointer of parent node
9 if p[y]=NIL
10 then root[T]← x
11 else if y=left[[p[y]] ►Set pointer of the parent to point to grand parent
12 then left[p[y]]←x
13 else right[p[y]] ←x
14 if y≠ z
15 then key[z]←key[y] ► Copy key of successor node
16 return y
The DELETE method removes a node with given key. After deletion, the tree is restructured
so that the binary-search-tree property is restored. The method deals with three cases(1)
node is a leaf (2) Node is not a leaf and has one child (3) Node is not a leaf ,and has two
children

Tree and Binary Search tree

  • 1.
  • 2.
     Arrays  LinkedLists 17 20 29 34 483020 34 29 30 4817 30 216 14 28 48 6 4814 21 28 30 Storing and modifying data fast searching, slow insertion slow searching, fast insertion 7
  • 3.
  • 4.
    Trees  A treeis often used to represent a hierarchy .  Because the relationships between the items in the hierarchy suggest the branches of a botanical tree.  For example, a tree-like organization chart is often used to represent the lines of responsibility in a business as shown in Figure
  • 5.
  • 6.
    Trees  As wesee in the figure, the tree is upside-down.  This is the usual way the data structure is drawn.  The president is called the ro o t of the tree and the clerks are the le ave s.  A tree is extremely useful for certain kinds of computations.  For example, suppose we wish to determine the total salaries paid to employees by division or by department.  The total of the salaries in division A can be found by computing the sum of the salaries paid in departments A1 and A2 plus the salary of the vice- president of division A.
  • 7.
    Trees  Similarly, thetotal of the salaries paid in department A1 is the sum of the salaries of the manager of department A1 and of the two clerks below her.  Clearly, in order to compute all the totals, it is necessary to consider the salary of every employee.  Therefore, an implementation of this computation must visit all the employees in the tree.  An algorithm that systematically visits all the items in a tree is called a tre e trave rsal.
  • 8.
    Trees in ourlife  Tree is an important data structure that represent a hierarchy  Trees/hierarchies are very common in our life:  Tree of species(class/type) (is-a)  Component tree (part-of)  Family tree (parent-child)
  • 9.
    9 Tree Terminology  Atre e is a collection of elements (nodes)  Each node may have 0 or more succe sso rs  (Unlike a list, which has 0 or 1 successor)  Each node has e xactly o ne pre de ce sso r  Except the starting / top node, called the ro o t  Links from node to its successors are called branche s  Successors of a node are called its childre n  Predecessor of a node is called its pare nt  Nodes with same parent are sibling s  Nodes with no children are called le ave s
  • 10.
    10 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len A Tree Has a Root Node ROOT NODE
  • 11.
    11 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len Leaf nodes have no children LEAF NODES
  • 12.
    More Terminologies  Path A sequence of edges  Le ng th o f a path  number of edges on the path  De pth/Le ve lof a node  length of the unique path from the root to that node  He ig ht of a node  length of the longest path from that node to a leaf  all leaves are at height 0  The height of a tree = the height of the root = the depth of the deepest leaf  Ance sto r and de sce ndant  If there is a path from n1 to n2  n1 is an ancestor of n2, n2 is a descendant of n1  Pro pe r ance sto r and pro pe r de sce ndant
  • 13.
    13 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len A Tree Has Levels LEVEL 0
  • 14.
    14 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len Level One LEVEL 1
  • 15.
    15 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len Level Two LEVEL 2
  • 16.
    16 Representation Of aGeneral Tree -- first child/next sibling  Example for this tree: A B D F G H E C A null First child Next sibling B E null H null null C null D null F null null G null Cannot directly access D from A. ParentPtr Key value sibling1st child
  • 17.
    Tree Formal Definitions A tree is a collection of nodes. The collection can be empty,  Otherwise, a tree consists of a distinguished node r, called the root, and zero or more (sub) trees T1, T2,. ……, Tk, each of whose roots are connected by a directed edge to r.  The root of each subtree is said to be a child of r and r is the parent of each subtree root.
  • 18.
    Binary Tree  Thesimplest form of tree is a binary tree. A binary tree consists of a. a node (called the root node) and b. left and right sub-trees. Both the sub-trees are themselves binary trees  We now have a recursively defined data structure.
  • 19.
  • 20.
    What is abinary tree?  Each node can have up to two successor nodes (childre n)  The predecessor node of a node is called its pare nt  The "beginning" node is called the ro o t (no parent)  A node without childre n is called a le af
  • 21.
    21 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len A Subtree LEFT SUBTREE OF ROOT NODE
  • 22.
    22 Owner Jake Manager Chef Brad Carol WaitressWaiter Cook Helper Joyce Chris Max Len Another Subtree RIGHT SUBTREE OF ROOT NODE
  • 23.
    Binary Trees Some BinaryTrees One node Two nodes Three nodes
  • 24.
    Convert a GenericTree to a Binary Tree
  • 25.
    Binary Search Trees Binary Search Tree Property: The value stored at a node is greaterthan the value stored at its left child and less than the value stored at its right child.
  • 27.
    Tree node structure template<classItemType> class TreeNode { ItemType info; TreeNode* left; TreeNode* right; };
  • 28.
    Binary Tree Notation In C++the structure of binary tree node can be specified as a class named Node: class Node{ public: int key; // key stored in the node Node *left; // link to left child Node *right; // link to right child }; parentp[x] Using pseudo code convention, the links are defined as follows: x -pointer to a node left[x]- pointer to left child of x right[x]- pointer to right child of x p[x]-pointer to parent of x key[x]-key stored in node x x key left[x] right[x] left child right child
  • 29.
  • 30.
    Function RetrieveItem  Whatis the base case(s)? 1) When the key is found 2) The tree is empty (key was not found)  What is the general case? Search in the left or right subtrees
  • 31.
    Binary Tree Traversal Tre e Trave rsalis the process of visiting each node in the tree exactly one time.  Tree traversals are of two types  Depth First Traversal  Breadth First Traversal  The three Depth First Traversal techniques are  Pre o rde r tre e trave rsal  Ino rde r tre e trave rsal  Po sto rde r tre e trave rsal
  • 32.
    On a visitto node the data stored ·Preorder Tree Traversal is processed, printed, etc. A1 In preorder traversal first the root is visited , then left subtree is visited preorder , and finally the right subtree is visited preorder. B C 2 Preorder: A B C A ·Inorder Tree Traversal In inorder traversal first left subtree is visited inorder then root is visited and finally the right subtree is visited inorder . 2 1 B C Inorder: B A C 2A ·Postorder Tree Traversal In postorder traversal first the left subtree is visited post order, then right subtree is visited postorder, and finally root is visited . B C 1 Postorder: B C A In tree traversal each node is visited once only. Traversal is also referred to as walk. The standard procedures of traversing a tree are known as preorder, inorder, and postorder. Binary Tree Traversal
  • 33.
    Binary Tree Traversal EulerTour Euler tour traversal of binary tree is a path around the tree . Tour starts from the root toward the left child. The tree edges are kept to the left . Each node is encountered three times: from left, from below and from right Postorder lists nodes on left of Euler path. Inorder lists nodes above the path Postorder lists nodes on the right.
  • 34.
    Tree Traversal Recursive Algorithms Therecursive procedures for tree traversal use recursive functions. Initially, the pointer to the root is passed as argument to the function. POSTORDER(x) 1 if x ≠ nil 2 then POSTORDER(left[x]) ► Traverse left subtree postorder 3 POSTORDER(right[x]) ► Traverse right subtree postorder 4 print key[x] ►Print key INORDER(x) 1 if x ≠ nil 2 then INORDER(left[x]) ► Traverse left subtree inorder 3 print key[x] ► Print the key 4 INORDER(right[x]) ►Traverse right subtree inorder PREORDER(x) 1 if x ≠ nil 2 then print key[x] ► Print key 3 PREORDER(left[x]) ► Traverse left subtree preorder 4 PREORDER(right[x]) ►Traverse right subtree preorder
  • 35.
    Inorder Tree Traversal IterativeAlgorithm The iterative procedure uses a stack to hold addresses of the nodes waiting to be processed in traversal order. It includes following steps: Step #1:Push address of root on to the stack. Step #2: Traverse the leftmost path of the, pushing the address of each node on to the stack, and stopping when a leaf is reached. Step #3: Pop the stack, and process the popped off node. Step #4: If null is popped, exit, otherwise proceed to next step. Step#5: If a right child of the processed node exists, push its address to stack. Step#6: Repeat step # 2
  • 36.
  • 37.
  • 38.
  • 39.
    Inorder Traversal: Example-contd Step#:19Step#:20Stack Stack The inorder traversal of the sample binary tree is given by H D I B J E A F C G G
  • 40.
    40 Tree size int TreeSize(root: TreePointer) begin if root==null //this is le ft/rig ht child po int o f a le af then return 0; else return 1 + TreeSize(root->left) + TreeSize(root->right); end; int TreeSize (root: TreePointer) begin if root==null //this is le ft/rig ht child po int o f a le af then return 0; else return 1 + TreeSize(root->left) + TreeSize(root->right); end; Size of a Node: the number of descendants the node has (including the node itself). The size of root is the size of a tree. The size of a leaf is 1.
  • 41.
    Properties of BinaryTrees  A binary tree is aA binary tree is a fullfull binary tree if and only if:binary tree if and only if:  Each non leaf node hasEach non leaf node has exactly twoexactly two child nodeschild nodes  All leaf nodes lies at the same levelAll leaf nodes lies at the same level  It is calledIt is called fullfull since all possible node slots aresince all possible node slots are occupiedoccupied
  • 42.
    COMPLETE BINARY TREE Acomplete binary tree of depth d is the strictly binary tree all of whose leaves are at level d. A CB F GD E H I J K L M N O
  • 43.
    ALMOST COMPLETE BINARYTREE A binary tree of depth d is an almost binary tree if –Any node n at level less than d - 1 has two sons –For any node n in the tree with a right or left child at level I, the node must have left child (if it has right child) and all the nodes on the left hand side of the node must have two children. Not an almost complete tree Violate condition 1 Almost complete binary tree
  • 44.
    A Full BinaryTree - Example
  • 45.
    Complete Binary Trees- Example BB AA CC DD EE HH II JJ KK FF GG Figure 13.8 A complete binary treeFigure 13.8 A complete binary tree
  • 46.
    Strictly Binary Trees A binary tree in which every non-leaf node has non- empty left and right sub-trees  A strict binary tree with n leaves always contain 2n-1 nodes.  Level of a node = depth of a node
  • 47.
    Almost Complete BinaryTree  A binary tree of depth d is an almost complete binary tree if  Any node n at level less than d - 1 has two sons (complete tree until level d-1)  For any node n in the tree with a right or left child at level d, the node must have left child (if it has right child) and all the nodes on the left hand side of the node must have two children.
  • 48.
    Almost Complete BinaryTree Examples: Not an almost complete tree Violate condition 1 Not an almost complete tree Violate condition 2
  • 49.
    Almost Complete BinaryTree Almost complete binary tree 1 2 3 4 5 6 7 8 9 Almost complete binary tree but not strictly binary tree 7 1 2 3 4 5 6 8 9 10 Numbering of an almost complete binary tree
  • 50.
    Numbering of anAlmost Complete Binary Tree Almost complete binary tree 1 2 3 4 5 6 7 8 9 Almost complete binary tree but not strictly binary tree 1 2 3 4 5 6 7 8 9 10 n nodes of an almost complete binary tree can be numbered from 1 to n
  • 51.
    Function Insert Item  Usethe binary search tree property to insert the new item at the correct place
  • 52.
  • 53.
     What isthe base case(s)? The tree is empty  What is the general case? Choose the left or right subtree Function InsertItem (cont.)
  • 54.
    template<class ItemType> void TreeType<ItemType>::InsertItem(ItemTypeitem) { Insert(root, item); } template<class ItemType> void Insert(TreeNode<ItemType>*& tree, ItemType item) { if(tree == NULL) { // base case tree = new TreeNode<ItemType>; tree->info = item; } else if(item < tree->info) Insert(tree->left, item); else Insert(tree->right, item); } Function InsertItem (cont.)
  • 55.
    Binary Tree Traversal Breadth First Tree Traversal  Implementation of this kind of traversal is straightforward when a queue is used.  Consider a top down left-to-right, breadth first traversal.  After a node is visited, its children, if any are placed at the end of a queue, and the node at the beginning of the queue is visited.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
    Breadth First Traversal Example DqueueB D H A C D B E F G I ACB
  • 61.
    Breadth First Traversal Example D EnqueueE, H E H H A C D B E F G I ACB
  • 62.
    Breadth First Traversal Example DqueueD E H H A C D B E F G I ACBD
  • 63.
    Breadth First Traversal Example DqueueE H H A C D B E F G I ACBDE
  • 64.
    Breadth First Traversal Example EnqueueF, G H F G H A C D B E F G I ACBDE
  • 65.
    Breadth First Traversal Example DqueueH F G H A C D B E F G I ACBDEH
  • 66.
    Breadth First Traversal Example I EnqueueI F G H A C D B E F G I ACBDEH
  • 67.
  • 68.
  • 69.
  • 70.
    Simple Search Algorithm Let us now state a simple search algorithm that will try to give you an idea about the sort of data structures that will be used while searching, and the stop criteria for your search. The strength of the algorithm is such that we will be able to use this algorithm for both Depth First Search (DFS) and Breadth First Search (BFS). Let S be the start state 1. Initialize Qwith the start node Q=(S) as the only entry; set Visited = (S) 2. If Qis empty, fail. Else picknode XfromQ 3. If Xis a goal, return X, we’ve reached the goal 4. (Otherwise) Remove XfromQ 5. Find all the children of state Xnot in Visited 6. Add these to Q; Add Children of Xto Visited 7. Go to Step 2
  • 71.
    Simple Search Algorithm Here Q represents a priority queue. The algorithm is simple and doesn’t need much explanation. We will use this algorithm to implement blind and uninformed searches. The algorithm however can be used to implement informed searches as well.
  • 72.
    Simple Search AlgorithmApplied to Depth First Search  Depth First Search dives into a tree deeper and deeper to fine the goal state.  As mentioned previously we will give priority to the element with minimum P(n) hence the node with the largest value of height will be at the maximum priority to be picked from Q. The following sequence of diagrams will show you how DFS works on a tree using the Simple Search Algorithm.
  • 73.
    We start witha tree containing nodes S, A, B, C, D, E, F, G, and H, with H as the goal node. In the bottom left table we show the two queues Q and Visited. According to the Simple Search Algorithm, we initialize Q with the start node S,
  • 74.
    • If Qis not empty, pick the node X with the minimum P(n) (in this case S), as it is • the only node in Q. Check if X is goal, (in this case X is not the goal). Hence find • all the children of X not in Visited and add them to Q and Visited. Goto Step 2. Again check if Q is not empty, pick the node X with the minimum P(n) (in this case either A or B), as both of them have the same value for P(n). Check if X is goal, (in this case A is not the goal). Hence, find all the children of A not in Visited and add them to Q and Visited. Go to Step 2.
  • 75.
    Go on followingthe steps in the Simple Search Algorithm till you find a goal node. The diagrams below show you how the algorithm proceeds.
  • 76.
    Here, from the5th row of the table when we remove H and check if it’s the goal, the algorithm says YES and hence we return H as we have reached the goal state.
  • 77.
     The diagrambelowalso shows that DFS didn’t have to search the entire search space, ratheronly by traveling in half the tree, the algorithmwas able to search the solution.
  • 78.
    Simple Search AlgorithmApplied to Breadth First Search  Self study
  • 79.
    Problems with DFSand BFS  ????
  • 80.
  • 81.
    Binary Search Tree Definition Abinary search tree (BST) is organized such that key in a node is larger than any key in the left subtree and smaller than any key in right subtree. Iif x is pointer to a tree node then binary-search-tree property implies key[x] > key[ left[x] ] key[x] < key[right[x]] for all x In BST, nodes with duplicate keys are not allowed. Binary Search Tree
  • 82.
    Binary Search Tree Operations Commonoperations performed on binary search tree are : ·INSERT- Add a new node with a given key ·SEARCH-Search a node with a given key ·DELETE-Delete a node with a given key ·MAXIMUM-Find largest key in the tree or in a subtree ·MINIMUM-Find smallest key in the tree or in a subtree ·SUCCESSOR- Find a node with smallest key which larger than the key in given node ·TRAVERSE- Traverse tree inorder, preorder, or postorder
  • 83.
    Binary Search Tree Insertion-1 Insertinga new node with key 10 into a Binary Search Tree
  • 84.
    Binary Search Tree Insertion-2 Insertionkey 10 compared with key in the root node. It is smaller . Next, the left child is examined.
  • 85.
    Binary Search Tree Insertion-3 Insertionkey 10 compared with key in the left child. It is smaller . Next, the left child is examined.
  • 86.
    Binary Search Tree Insertion-4 Insertionkey 10 compared with key in the left child . It is smaller . Next, the left child is examined.
  • 87.
    Binary Search Tree Insertion-5 Insertionkey 10 compared with key in the left child. It is larger . Next, the right child is examined.
  • 88.
    Binary Search Tree Insertion-6 Insertionkey 10 compared with key in the right child. It is smaller . Next, the left child is examined.
  • 89.
    Binary Search Tree Insertion-7 Insertionkey 10 compared with key in the left child. It is larger . Next, the right child is examined.
  • 90.
    Binary Search Tree Insertion-8 Insertionkey 10 compared with key in the right child. It is smaller . Node with key 11 is a leaf. The new key is inserted as left child of the leaf being examined
  • 91.
    Binary Search Insertion-9 Tree key 10is placed in new left child
  • 92.
    Binary Search Tree INSERTMethod The INSERT Method adds a new node. The pointer to new node is passed as argument. to the insert method. Starting with root, the input key is compared successively with left or right child until a leaf is reached.. The link pointer of the leaf is reset to point to the new node. INSERT(T, z) ► z is pointer to new node 1 y←NIL ► y is a temporary pointer used to track the parent 2 x←root[T] ►x is another pointer to track current node. It is set initially to point to root 3 while x ≠ NIL 4 do y←x 5 if key[z] <key[x] ►Compare given key with the key in node being examined 6 then x←left[x] ►If given key is smaller proceed to left child 7 else x←right[x] ►If given key is larger proceed to right child 8 p[z]←y ► y holds the pointer to parent of the node being examined 9 if y=NIL ►The node being added is the first node ( has no parent) 10 then root[T] ← z 11 else if key[z] < key[y] ►Check whether new node to be added is left child or right child 12 then left[y]←z ►If left child, reset the parent node pointer link to left
  • 93.
    Binary Search Searching -1 Binarytree is searched for node with key=71 Tree
  • 94.
    Binary Search Tree Searching-2 Search key 71 is compared with the key in root. It is larger. Next, the right child is examined
  • 95.
    Binary Search Tree Searching-3 Search key 71 is compared with the key 85 in right child. It is smaller. Next, the left child is examined
  • 96.
    Binary Search Tree Searching-4 Search key 71 is compared with the key 81 in left child. It is smaller. Next, the left child is examined
  • 97.
    Binary Search Tree Searching-5 Search key 71 is compared with the key 68 in left child. It is larger. Next, the right child is examined
  • 98.
    Binary Search Tree Searching-6 Search key 71 is compared with the key 76 in left child. It is smaller. Next, the left child is examined
  • 99.
    Binary Search Searching -7 Searchkey 71 matches. Search is successful. Tree
  • 100.
    Binary Search Tree SEARCHMethod SEARCH(x, k) ►x is pointer to root and k is given key 1 while x ≠ NIL and k ≠ key[x] ► Loop until a match is found or a leaf is reached 2 do if k < key[x] ► Check if given key is smaller 3 then x←left[x] ► Proceed to left child if key smaller 4 else x←right[x] ► Proceed to right child if key is larger 5 return x ►Return NIL if no match found and pointer to node with matched key The SEARCH method finds a node with a given key. The search procedure starts with the root. It successively compares keys in the left or right children, depending on whether the key in the parent node is larger or smaller than the given key. The search terminates when either a matching key is found, or a leaf is reached. The method returns a pointer to the node which has the matching key or null pointer if no match is found..
  • 101.
    Binary Search Tree SmallestKey The smallest key is found by starting with root node and traversing leftmost path of BST
  • 102.
    Binary Search Tree Minimumkey in a Subtree The minimum key is found by starting with root node of the subtree and traversing leftmost path of in the subtree
  • 103.
    BinarySearchTree MINIMUM Method The MINIMUMmethod finds the smallest key in a binary search tree or in any of the subtrees. It starts with the root ,or root of subtree, and proceeds down the leftmost path in the tree or subtree. The procedure returns pointer to the node with the smallest key. MINIMUM(x)►x is pointer to root, or root of the subtree 1 while left[x] ≠ NIL ►Move down the leftmost path until leaf is reached 2 do x ←left[x] ►Move to left child 3 return x ►Return pointer to the leaf , which contains the smallest key
  • 104.
    Binary Search Tree LargestKey The largest key is found by starting with root node and traversing rightmost path of BST
  • 105.
    Binary Search Tree MaximumKey in a Subtree The maximum key is found by starting with root node of the subtree and traversing rightmost path of in the subtree
  • 106.
    Binary Search Tree MAXIMUMMethod The MAXIMUM method finds the largest key in a binary search tree or in any of the subtrees.It starts with the root ,or root of subtree, and proceeds down the rightmost path in the tree or subtree. The procedure returns pointer to the node with the largest key. MAXIMUM(x) ►x is pointer to root or root of the subtree 1 while right[x] ≠ NIL ►Move down the rightmost path until leaf is reached 2 do x ←right[x] ►Move to right child 3 return x ►Return pointer to the leaf , which contains the largest key
  • 107.
    Binary Search Tree Deleting aleaf node Leaf Node -1 Node with key 73 is
  • 108.
    Binary Search Tree DeletingLeaf Node -2 First, the node with key 73 is searched. The key 73 is compared successively with keys 38, 82,47,62,75 until it matches with key in a node. The search path is highlighted
  • 109.
    Binary Search Tree DeletingLeaf Node-3 Node with key 73 is de-linked from the parent node with key 75, by setting the left link-pointer in parent node to null
  • 110.
    Binary Search Tree DeletingLeaf Node-4 Finally, the de-linked node with key 73 is removed
  • 111.
    Binary Search Tree DeletingNode With Single Child-1 Node with key 7 has a single left child with key 22
  • 112.
    Binary Search Tree DeletingNode With Single Child-2 First, the node with key 7 is searched. The key is compared successively with keys 38,25,and 6, until a match is found The search path is highlighted. The sub-tree, rooted at the node to be deleted, is shown in yellow shade.
  • 113.
    Binary Search Tree DeletingNode With Single Child-3 The node to be deleted with key 7, is de-linked from its parent node with key 6 and its right child with key 22 The right child link-pointer of parent node with key 6, is reset to point to the node with key 22
  • 114.
    Binary Search Tree Thede-linked node with key 7 is removed Deleting Node With Single Child-4
  • 115.
    Binary Search Tree TheBST after node with 7 has been removed Deleting Node With Single Child-5
  • 116.
    Binary Search Tree DeletingNode With Both Children-1 The node with key 33 is to be deleted. It keys 32 and 41. has both left and right children with
  • 117.
    Binary Search Tree DeletingNode With Both Children-2 First, the node with key 33 is searched. The key 33 is successively compared with the keys 47 and 25 until a match is found. The search path is highlighted.
  • 118.
    Binary Search Tree DeletingNode With Both Children-3 In order to delete the node, its in-order successor is searched, which contains smallest of all the keys contained in the right sub-tree. Sub- tree is traversed by following the leftmost path, beginning with sub- tree node with .The search path is shown in yellow shade. The in-order successor of node with key 33 is the node with key 34.
  • 119.
    Binary Search Tree DeletingNode With Both Children-4 in the node that contains key 33.The key 34 of in-order successor is to be copied
  • 120.
    Binary Search Tree DeletingNode With Both Children-5 The in-order successor with key 34 is de-linked from its parent node with key 38.
  • 121.
    Binary Search Tree DeletingNode Both Children-6 The key 34 in the in-order successor The in-order successor is deleted. is copied into the node that holds key 33.
  • 122.
    Binary Search Tree DeletingNode Both Children-7 The node with key 33 is removed. The BST property is preserved.
  • 123.
    Binary Search Tree SUCCESSORMethod SUCCESSOR(x) ►x is pointer to given node 1 If right[x] ≠ NIL ►If right subtree exists,then return pointer to node with smallest key 2 then return MINUMUM(x) 3 y ←p[x] ►Proceed upward to parent node 4 while y ≠ NIL and x=right[y] ►Continue until root is reached, or else a node with left child is found 5 do x ← y 6 y← p[y] ► Move to parent node 7 return y ► Return pointer to the successor node The SUCCESSOR method finds the inorder successor of a node with given key. If the right subtree of the node exists, then the method finds the smallest key in the subtree. If the right subtree does not exist, then the method proceeds upward until an ancestor with a node with a left child of its parent is encountered.. It returns pointer to the successor node.
  • 124.
    Binary Search Tree DELETEMethod DELETE(T, z) 1 if left[z]=NIL or right[z]=NIL ►Check if node has no left or right child 2 then y ← z ►if left or right child exists then y holds pointer to current node 3 else y ← SUCCESSOR(z) ►otherwise, y holds pointer to the successor node 4 if left[y] ≠ NIL ► check if the left child exists 5 then x← left[x] ►if so, move to left child 6 else x← right[y] ►otherwise, move to right child 7 if x ≠ NIL 8 then p[x] ← p[y] ► Set pointer of parent node 9 if p[y]=NIL 10 then root[T]← x 11 else if y=left[[p[y]] ►Set pointer of the parent to point to grand parent 12 then left[p[y]]←x 13 else right[p[y]] ←x 14 if y≠ z 15 then key[z]←key[y] ► Copy key of successor node 16 return y The DELETE method removes a node with given key. After deletion, the tree is restructured so that the binary-search-tree property is restored. The method deals with three cases(1) node is a leaf (2) Node is not a leaf and has one child (3) Node is not a leaf ,and has two children