25.07.2017 Views

Intro-CSharp-Book-v2015

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

714 Въведение в програмирането със C#<br />

19<br />

13 35<br />

7 16<br />

23<br />

11 17<br />

Предлагаме следния примерен код, който реализира описания алгоритъм:<br />

/// <br />

/// Removes an element from the tree if exists<br />

/// <br />

/// the value to be deleted<br />

public void Remove(T value)<br />

{<br />

BinaryTreeNode nodeToDelete = Find(value);<br />

if (nodeToDelete == null)<br />

{<br />

return;<br />

}<br />

}<br />

Remove(nodeToDelete);<br />

private void Remove(BinaryTreeNode node)<br />

{<br />

// Case 3: If the node has two children.<br />

// Note that if we get here at the end<br />

// the node will be with at most one child<br />

if (node.leftChild != null && node.rightChild != null)<br />

{<br />

BinaryTreeNode replacement = node.rightChild;<br />

while (replacement.leftChild != null)<br />

{<br />

replacement = replacement.leftChild;<br />

}<br />

node.value = replacement.value;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!