/// <summary>
 /// preoder -->output all the value
 /// </summary>
 public void PreOrder(Treenode node)
 {
     if (node != null)
     {
         Console.WriteLine(node.Value);
         PreOrder(node.LeftChild);
         PreOrder(node.RightChild);
     }
 }
        /// <summary>
        /// recursive methods,pass in node with the sum. sum will be 0 at very beginning
        /// </summary>
        /// <param name="node"></param>
        /// <param name="sum"></param>
        /// <returns>sum</returns>
        public static int SumOfOdd(Treenode node, int sum)
        {
            if (node == null)
            {
                return(sum);
            }
            if (node.Value % 2 != 0)
            {
                sum += node.Value;
            }

            sum = SumOfOdd(node.LeftChild, sum);
            sum = SumOfOdd(node.RightChild, sum);
            return(sum);
        }
        /// <summary>
        /// add node to a binary-search-tree,tree
        /// </summary>
        /// <param name="node"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        private Treenode addRecursive(Treenode node, int val)
        {
            if (node == null)
            {
                return(new Treenode(val));
            }

            if (val < node.Value)
            {
                node.LeftChild = addRecursive(node.LeftChild, val);
            }
            else if (val > node.Value)
            {
                node.RightChild = addRecursive(node.RightChild, val);
            }
            else
            {
                return(node);
            }
            return(node);
        }
 /// <summary>
 /// get sum of  odd numbers
 /// </summary>
 /// <param name="root"></param>
 /// <param name="sum"></param>
 /// <returns>sum</returns>
 public static int getsum(Treenode root, int sum)
 {
     sum = SumOfOdd(root, sum);
     return(sum);
 }
 public void Add(int val)
 {
     Root = addRecursive(Root, val);
 }
 /// <summary>
 /// constructor
 /// </summary>
 public Tree()
 {
     Root = null;
 }
示例#7
0
 public Treenode(int val)
 {
     Value      = val;
     LeftChild  = null;
     RightChild = null;
 }