Programming and Data Structures Programming and Data Structures
Lecture 8 Lecture 8
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Binary trees Binary trees
●
hierarchical data structure:
●
root node address
●
each node has the addresses of the main nodes of the left and right
branches (left and right child, empty in the nodes of the last level - leaves)
●
each node has an address of the parent node (parent, empty in the root) A
right root
left
B
right left
parent C
right parent
D parent parent E parent F
C
Binary search tree (BST) Binary search tree (BST)
●
the left subtree contains only values less than the parent
●
the right subtree contains only values not less than the parent
●
searching requires no more steps than the height of the tree
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
(1) set pred to empty and succ to the root
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
(4) create a new node (creator) with the given value, empty values for the left and right nodes (left and right) and pred as the parent address
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
C parent
creator
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
(4) create a new node (creator) with the given value, empty values for the left and right nodes (left and right) and pred as the parent address
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
C parent
creator
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
(4) create a new node (creator) with the given value, empty values for the left and right nodes (left and right) and pred as the parent address
(5) move the left or right pointer (depending on the value to be inserted) in the node pointed to by pred (or the root when pred is empty) to the new node
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent F
succ pred
C parent
creator right
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
(4) create a new node (creator) with the given value, empty values for the left and right nodes (left and right) and pred as the parent address
(5) move the left or right pointer (depending on the value to be inserted) in the node pointed to by pred (or the root when pred is empty) to the new node
E
Insertion Insertion
D
right root
left
B left
parent E
right parent
A parent parent C parent F
right
(1) set pred to empty and succ to the root
(2) if the value in the node pointed to by succ is greater than the one inserted, set pred to succ, and succ o the address stored in left, otherwise to the address stored in right
(3) go back to step 2. when succ is not empty
(4) create a new node (creator) with the given value, empty values for the left and right nodes (left and right) and pred as the parent address
(5) move the left or right pointer (depending on the value to be inserted) in the node pointed to by pred (or the root when pred is empty) to the new node
E
Leaf removal Leaf removal
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
E
Leaf removal Leaf removal
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
E
Leaf removal Leaf removal
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
E
Leaf removal Leaf removal
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to an empty address
E
Leaf removal Leaf removal
D
right root
left
B left
parent E
right parent
A parent parent C parent F
killer pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to an empty address
E
Leaf removal Leaf removal
D
right root
left
B left
parent E
right parent
A parent parent C parent F
killer pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to an empty address
(4) release the node poined to by the dedicated pointer (killer)
E
Leaf removal Leaf removal
D
right root
left
B left
parent E
right parent
A parent parent F
killer pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to an empty address
(4) release the node poined to by the dedicated pointer (killer)
E
Leaf removal Leaf removal
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has no children (it is a leaf), set a dedicated killer to the node pointed to by succ and set the succ pointer to empty
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to an empty address
(4) release the node poined to by the dedicated pointer (killer)
D
right root
left
B left
parent E
right parent
A parent parent F
E
Removal of a node with one child Removal of a node with one child
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
E
Removal of a node with one child Removal of a node with one child
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
E
Removal of a node with one child Removal of a node with one child
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred killer
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
E
Removal of a node with one child Removal of a node with one child
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred killer
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to succ, and the parent pointer of the node pointed to by succ to pred
E
Removal of a node with one child Removal of a node with one child
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred killer
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to succ, and the parent pointer of the node pointed to by succ to pred
E
Removal of a node with one child Removal of a node with one child
D
right
root left
B
right left
parent E
right parent
A parent parent C parent F
succ pred killer
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to succ, and the parent pointer of the node pointed to by succ to pred
(4) release the node
E
Removal of a node with one child Removal of a node with one child
D
right
root left
B
right left
parent
A parent parent C parent F
succ pred killer
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to succ, and the parent pointer of the node pointed to by succ to pred
(4) release the node
Removal of a node with one child Removal of a node with one child
D
right
root left
B
right left
parent
A parent parent C parent F
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to succ, and the parent pointer of the node pointed to by succ to pred
(4) release the node
Removal of a node with one child Removal of a node with one child
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the searched node has exactly one child, set a dedicated killer to the node pointed to by succ and set the succ to the child's address
(3) set the left or right pointer (with the address stored in killer) of the node pointed to by pred (or the root when pred is empty) to succ, and the parent pointer of the node pointed to by succ to pred
(4) release the node
D
right
root left
B
right left
parent
A parent parent C parent F
Removal of a node with two children Removal of a node with two children
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
Removal of a node with two children Removal of a node with two children
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
succ pred
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
Removal of a node with two children Removal of a node with two children
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
succ E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
Removal of a node with two children Removal of a node with two children
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
succ E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
Removal of a node with two children Removal of a node with two children
D
right root
left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
succ mover
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
Removal of a node with two children Removal of a node with two children
D
right
root left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
succ mover
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
(4) replace the killer node with the mover (change pred->left/right, mover->left and mover->right when succ differs from mover)
Removal of a node with two children Removal of a node with two children
D
right
root left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
succ mover left
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
(4) replace the killer node with the mover (change pred->left/right, mover->left and mover->right when succ differs from mover)
Removal of a node with two children Removal of a node with two children
D
right
root left
B
right left
parent E
right parent
A parent parent C parent F
killer pred
succ mover left
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
(4) replace the killer node with the mover (change pred->left/right, mover->left and mover->right when succ differs from mover)
(5) release the node
Removal of a node with two children Removal of a node with two children
D
right
root left
E right parent
A parent parent C parent F
killer pred
succ mover left
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
(4) replace the killer node with the mover (change pred->left/right, mover->left and mover->right when succ differs from mover)
(5) release the node
Removal of a node with two children Removal of a node with two children
D
right
root left
E right parent
A parent parent C parent F
left
E
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
(4) replace the killer node with the mover (change pred->left/right, mover->left and mover->right when succ differs from mover)
(5) release the node
Removal of a node with two children Removal of a node with two children
(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the operation)
(2) if the node we are looking for has two children, set a dedicated killer to the node pointed to by succ and set the succ pointer to the address of the right child
(3) from a subtree whose root is succ search for the first branch beginning with a node without a left child (mark it with the mover)
(4) replace the killer node with the mover (change pred->left/right, mover->left and mover->right when succ differs from mover)
(5) release the node