标签:
A binary tree is defined as a tree where each node can have no more than two children.
Building a Binary Search Tree:
首先创建一个节点Class
public class BtNode { public int Data { get; set; } public BtNode Left { get; set; } public BtNode Right { get; set; } public void DisplayNode() { Console.Write("Data: {0}, ", this.Data); } }
然后创建BST Class 提供Insert 方法:
public class BinarySearchTree { private BtNode root = null; public void Insert(int data) { BtNode newNode = new BtNode(); if (this.root == null) { this.root = newNode; } else { // Add to children nodes. BtNode current = new BtNode(); BtNode parent = new BtNode(); while(true) { parent = current; if (data < current.Data) { current = current.Left; if (current == null ) { parent.Left = newNode; break; } } else { current = current.Right; if (current == null) { parent.Right = newNode; break; } } } } } }
标签:
原文地址:http://www.cnblogs.com/fdyang/p/4981175.html