Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs
- using System;
- using System.IO;
- namespace test
- {
- class Program
- {
- public static void Main (string[] args)
- {
- BinaryTree myTree = new BinaryTree ();
- using (StreamReader fileIn = new StreamReader ("/Users/masha/Projects/test/test/input.txt"))
- {
- string line = fileIn.ReadToEnd ();
- string[] mas = line.Split (' ');
- foreach (string item in mas)
- myTree.Add (int.Parse(item));
- int sum = 0;
- sum += Convert.ToInt32(myTree.Walk(myTree.Tree));
- Console.WriteLine ("{0}",sum);
- }
- }
- }
- }
- ____________________________________________________
- BinaryTree.cs
- using System;
- namespace test
- {
- public class BinaryTree
- {
- //вложенный класс, отвечающий за узлы и операции для дерева
- public class Node
- {
- public object inf;
- public Node left;
- public Node right;
- //создание узла
- public Node (object nodeInf)
- {
- inf = nodeInf;
- left = null;
- right = null;
- }
- //добавление узла согласно определению бин. дерева
- public static void Add(ref Node r, object nodeInf)
- {
- if (r == null)
- {
- r = new Node (nodeInf);
- }
- else
- {
- if (((IComparable)(r.inf)).CompareTo(nodeInf) > 0)
- Add (ref r.left, nodeInf);
- else
- Add (ref r.right, nodeInf);
- }
- }
- //поиск листьев
- public static object Sheet(Node x)
- {
- if (x != null)
- {
- if (x.left != null)
- return Sheet (x.left);
- else if (x.right != null)
- return Sheet (x.right);
- else
- return 0;
- }
- return 0;
- }
- }
- //конец вложенного класса
- Node tree;
- public Node Tree
- {
- get{return tree;}
- }
- public object Inf
- {
- set{tree.inf = value;}
- get{return tree.inf;}
- }
- public BinaryTree()
- {
- tree = null;
- }
- private BinaryTree (Node r)
- {
- tree = r;
- }
- public void Add(object nodeInf)
- {
- Node.Add (ref tree, nodeInf);
- }
- public object Sheet()
- {
- return Node.Sheet (tree);
- }
- public int Walk(Node node)
- {
- if (node == null)
- {
- return 0;
- }
- if (node.left == null && node.right == null )
- {
- return (int)node.inf;
- }
- else
- {
- return Walk(node.left) + Walk(node.right);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment