• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
41
0
0

Pełen tekst

(1)

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>

(2)

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

(3)

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

(4)

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

(5)

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

(6)

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

(7)

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

(8)

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

(9)

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

(10)

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

(11)

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

(12)

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

(13)

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

(14)

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

(15)

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

(16)

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

(17)

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

(18)

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

(19)

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

(20)

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

(21)

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

(22)

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

(23)

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

(24)

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

(25)

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

(26)

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

(27)

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

(28)

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

(29)

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

(30)

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

(31)

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)

(32)

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

(33)

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

(34)

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)

(35)

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)

(36)

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)

(37)

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)

(38)

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

(39)

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

(40)

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

(41)

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

D

right

root left

E right parent

A parent

C parent

F parent

left

E

Cytaty

Powiązane dokumenty

● the public method changing the state of an object (modifier) is to check the compliance of such a change with the rules described by invariants. ● if the rules are exceeded,

● destructor, copy constructor and assignment operator (as for bounded) insertion/removal

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer

● during instantiation of a template with a specific type, the compiler replaces each instance of a parametric type with the specified type and compiles the data structure

(1) set the pred pointer on the node preceding the deleted node, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the

(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list

(1) invoke two auxiliary pointers set on the adjacent nodes pred and succ (assume that the pointers next in the preceding nodes pointed to by succ are already inverted, succ is

● input/output streams: iostream, fstream, sstream, iomanip. ● string