Построение двоичного дерева на основе модифицированной схемы хранения деревьев общего вида "left child" - "right sibling" (LCRS) - page 5

Построение двоичного дерева на основе модифицированной схемы хранения…
5
return leaf;
}
if(leaf->_parent == NULL)
{ leaf->_leftChild = new BinaryTree;
leaf->_leftChild ->_parent = leaf;
leaf = leaf->_leftChild; leaf ->_data = data;
leaf ->_leftChild = NULL;
leaf->_rightSibling = NULL;
return leaf;
}
if(leaf->_parent->_leftChild == leaf)
{ leaf ->_rightSibling = new BinaryTree;
leaf->_rightSibling->_parent = leaf->_parent;
leaf = leaf->_rightSibling;
leaf->_data = data; leaf->_leftChild = NULL;
leaf->_rightSibling = NULL;
return leaf;
}
else
if(leaf->_parent->_rightSibling != NULL)
{ leaf->_parent->_rightSibling->_leftChild =
new BinaryTree;
leaf->_parent->_rightSibling->_leftChild-
>_parent = leaf->_parent->_rightSibling;
leaf->_rightSibling = leaf->_parent-
>_rightSibling->_leftChild;
leaf=leaf->_parent->_rightSibling-
>_leftChild;
leaf->_data = data;
leaf->_leftChild = NULL;
leaf ->_rightSibling = NULL;
return leaf;
}
else
{ while (leaf->_parent!=NULL)
{ leaf = leaf ->_parent; }
while (leaf->_leftChild!=NULL)
{ leaf= leaf->_leftChild;
}
leaf->_leftChild = new BinaryTree;
leaf->_leftChild ->_parent = leaf;
leaf = leaf->_leftChild;
leaf ->_data = data;leaf->_leftChild =
NULL;
leaf->_rightSibling = NULL;
return leaf;
}
}
1,2,3,4 6,7,8,9
Powered by FlippingBook