25.07.2017 Views

Intro-CSharp-Book-v2015

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Глава 17. Дървета и графи 701<br />

if (value.hasParent)<br />

{<br />

throw new ArgumentException(<br />

"The node already has a parent!");<br />

}<br />

}<br />

}<br />

}<br />

value.hasParent = true;<br />

this.rightChild = value;<br />

/// <br />

/// Represents a binary tree structure<br />

/// <br />

/// the type of the values in the<br />

/// tree<br />

public class BinaryTree<br />

{<br />

// The root of the tree<br />

private BinaryTreeNode root;<br />

/// <br />

/// Constructs the tree<br />

/// <br />

/// the value of the root node<br />

/// the left child of the root node<br />

/// the right child of the<br />

/// root node<br />

public BinaryTree(T value, BinaryTree leftChild,<br />

BinaryTree rightChild)<br />

{<br />

if (value == null)<br />

{<br />

throw new ArgumentNullException("Cannot insert null value!");<br />

}<br />

BinaryTreeNode leftChildNode =<br />

leftChild != null ? leftChild.root : null;<br />

BinaryTreeNode rightChildNode =<br />

rightChild != null ? rightChild.root : null;<br />

}<br />

this.root = new BinaryTreeNode(<br />

value, leftChildNode, rightChildNode);<br />

/// <br />

/// Constructs the tree

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

Saved successfully!

Ooh no, something went wrong!