public string PrintNode(bool innerNode = false) { if (Type == NodeType.OP) { string opString = ""; switch (Op) { case OpType.ADD: opString = "+"; break; case OpType.SUB: opString = "-"; break; case OpType.MUL: opString = "*"; break; case OpType.DIV: opString = "/"; break; } if (innerNode == false) { return("(" + NodeA.PrintNode() + opString + NodeB.PrintNode() + ")"); } else { return(NodeA.PrintNode() + opString + NodeB.PrintNode() + " = " + GetNodeValue()); } } else { return(Value + ""); } }
/*查找二叉树节点*/ public static NodeB findNode(NodeB root, int?data) { if (root == null) { return(null); } if (root.Data == data) { return(root); } else if (data > root.Data) { if (root.RightChild == null) { return(null); } return(findNode(root.RightChild, data)); } else { if (root.LeftChild == null) { return(null); } return(findNode(root.LeftChild, data)); } }
/// <summary> /// 添加NodeB网元。IP、友好名不能重复;最多20个节点 /// </summary> /// <param name="ip">网元IP地址</param> /// <param name="friendlyName">网元友好名</param> /// <param name="port">连接端口,默认5000</param> /// <exception cref="CustomException">抛出错误信息,调用者catch到直接使用即可</exception> /// <returns>Element obj</returns> public override Element AddElement(string ip, string friendlyName, ushort port = 5000) { if (HasTwentyNodebs()) { throw new CustomException("当前已存在20个基站"); } string errorInfo = ""; if (HasSameFriendlyName(friendlyName)) { errorInfo = $"友好名为:{friendlyName}的网元已存在"; Log.Error(errorInfo); throw new CustomException(errorInfo); } if (HasSameIpAddr(ip)) { errorInfo = $"地址为:{ip}的网元已存在,网元友好名为:{GetFriendlyNameByIp(ip)}"; Log.Error(errorInfo); throw new CustomException(errorInfo); } Element newNodeb = new NodeB(ip, friendlyName, port); AddElement(ip, newNodeb); return(newNodeb); }
//______________________________________________________________________主界面动态刷新____ #region 添加对象树收藏 /// <summary> /// 将对象树添加到收藏; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddToCollect_Click(object sender, RoutedEventArgs e) { string StrName = StrNodeName; NodeB node = new NodeB("172.27.245.92", "NodeB"); string cfgFile = node.m_ObjTreeDataPath; StreamReader reader = File.OpenText(cfgFile); JObject JObj = new JObject(); JObj = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); IEnumerable <int> AllNodes = from nodes in JObj.First.Next.First select(int) nodes["ObjID"]; int TempCount = 0; foreach (var iter in AllNodes) { var name = (string)JObj.First.Next.First[TempCount]["ObjName"]; if ((name == StrName)) { if (JObj.First.Next.First[TempCount]["ObjCollect"] != null) { int ObjIsCollect = (int)JObj.First.Next.First[TempCount]["ObjCollect"]; int nn = (int)JObj.First.Next.First[TempCount]["ObjCollect"]; JObj.First.Next.First[TempCount]["ObjCollect"] = "1"; nn = (int)JObj.First.Next.First[TempCount]["ObjCollect"]; break; } else { var ObjNodesId = (int)JObj.First.Next.First[TempCount]["ObjID"]; var ObjParentNodesId = (int)JObj.First.Next.First[TempCount]["ObjParentID"]; var ObjChildRenCount = (int)JObj.First.Next.First[TempCount]["ChildRenCount"]; var ObjNameEn = (string)JObj.First.Next.First[TempCount]["ObjNameEn"]; var ObjMibList = (string)JObj.First.Next.First[TempCount]["MIBList"]; JObject NewObjNodes = new JObject(new JProperty("ObjID", ObjNodesId), new JProperty("ObjParentID", ObjParentNodesId), new JProperty("ChildRenCount", ObjChildRenCount), new JProperty("ObjName", name), new JProperty("ObjNameEn", ObjNameEn), new JProperty("MIBList", ObjMibList), new JProperty("ObjCollect", 1)); JObj.First.Next.First[TempCount].Remove(); JObj.First.Next.First[TempCount].AddBeforeSelf(NewObjNodes); break; } } TempCount++; } reader.Close(); FileStream fs = new FileStream(cfgFile, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(fs); sw.Write(JObj); sw.Close(); //File.WriteAllText(cfgFile, JsonConvert.SerializeObject(JObj)); }
/*求树的节点数量*/ public static int size(NodeB root) { if (root == null) { return(0); } return(1 + size(root.LeftChild) + size(root.RightChild)); }
static void Main(string[] args) { var nodeB2 = new NodeB(); var nodeA2 = new NodeA(nodeB2); var nodeA1 = new NodeA(nodeB2, nodeA2, new NodeC()); nodeA1.Accept(new PrintVisitor()); }
static void Bridge_Exemplo1() { var bridge = new Bridge(); var nodeA = new NodeA(); var nodeB = new NodeB(); bridge.ReachTo(nodeA); bridge.ReachTo(nodeB); }
/*创建一颗二叉树,输入参数是一个可变数组*/ public static NodeB createNode(params int[] array) { NodeB root = new NodeB(); foreach (int a in array) { insert(root, a); } return(root); }
/*后序遍历二叉树*/ public static void postOrder(NodeB root) { if (root == null) { return; } postOrder(root.LeftChild); postOrder(root.RightChild); Console.Write(root.Data + "---"); }
public override int GetHashCode() { unchecked { var hashCode = NodeA.GetHashCode(); hashCode = hashCode ^ NodeB.GetHashCode(); hashCode = (hashCode * 397) ^ Distance.GetHashCode(); return(hashCode); } }
/*求树的深度*/ public static int deepth(NodeB root) { if (root == null) { return(0); } else { return(deepth(root.LeftChild) > deepth(root.RightChild) ? deepth(root.LeftChild) + 1 : deepth(root.RightChild) + 1); } }
private void Click_Confirm(object sender, RoutedEventArgs e) { // 判断是否是合理的IP地址; // 判断友好名是否为空; NodeB nodeb = new NodeB(this.IpAddress.Text.ToString(), this.FriendName.Text.ToString()); // 后续需要用Control类管理,第一版只连接一个基站; this.Close(); this.OnClosed(new NodeBArgs(nodeb)); }
/* 二叉树的插入操作*/ public static bool insert(NodeB root, int?data) { if (root == null) { return(false); } if (root.Data == null) { root.Data = data; Console.WriteLine("插入成功:" + data); return(true); } else if (data == root.Data) { return(false); } else if (data > root.Data) { if (root.RightChild == null) { root.RightChild = new NodeB(data); Console.WriteLine("插入成功:" + data); } else { return(insert(root.RightChild, data)); } } else { if (root.LeftChild == null) { root.LeftChild = new NodeB(data); Console.WriteLine("插入成功:" + data); } else { return(insert(root.LeftChild, data)); } } return(true); }
private string m_ObjFilePath; // 暂时用一下,保存JSON文件路径; /// <summary> /// 实验程序先假定一个LMT只连接一个基站,在构造函数中直接读取默认JSON文件; /// </summary> public ObjNodeControl(NodeB node) { m_ObjFilePath = node.m_ObjTreeDataPath; JObject JObj = new JObject(); try { using (StreamReader reader = File.OpenText(m_ObjFilePath)) { JObj = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); } ObjNode.nodeb = node; ParseJObject(JObj); } catch (Exception e) { MessageBox.Show("1加载数据库失败;" + e.ToString()); } }
public int GetNodeValue() { if (Type == NodeType.NUM) { return(Value); } else { switch (Op) { case OpType.ADD: return(NodeA.GetNodeValue() + NodeB.GetNodeValue()); case OpType.SUB: return(NodeA.GetNodeValue() - NodeB.GetNodeValue()); case OpType.MUL: return(NodeA.GetNodeValue() * NodeB.GetNodeValue()); case OpType.DIV: return(NodeA.GetNodeValue() / NodeB.GetNodeValue()); default: return(0); } } }
public static void Run() { HashSet <int> hs = new HashSet <int>(); NodeB root = new NodeB(); Random r = new Random(); //insert(root, 1); insert(root, 2); insert(root, 3); for (int i = 0; i < 3; i++) { int x = r.Next(100); hs.Add(x); if (insert(root, x)) { } } Console.WriteLine("root深度----" + deepth(root)); Console.WriteLine("root长度----" + hs.Count); Console.WriteLine("树的内容"); List <int> l = hs.ToList <int>(); l.Sort(); foreach (int ii in l) { Console.Write(ii + "---"); } Console.WriteLine("\r\n前序遍历"); preOrder(root); Console.WriteLine(); Console.WriteLine("中序遍历"); midOrder(root); Console.WriteLine(); Console.WriteLine("后序遍历"); postOrder(root); Console.WriteLine(); Console.ReadKey(); }
/// <summary> /// 当增加基站的窗口关闭的时候进行的处理; /// 1、通过tcp接口连接基站 /// 2、连接成功后,向基站发送数据同步请求; /// 3、请求成功后,对数据进行解析,并显示在前端界面上; /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddNB_Closed(object sender, EventArgs e) { // 如果参数为空,则表示用户没有添加基站; if (!(e is NodeBArgs) || e == null) { return; } // 第一个版本所有数据先从本地获取; node = (e as NodeBArgs).m_NodeB; ObjNode.main = this; //ObjNode.datagrid = this.MibDataGrid; InitDataBase(); // 创建数据库(第一个版本先加载本地的); // 向基站前端控件填入对应信息; AddNodeBPageToWindow(); // 将基站添加到窗口页签中; if (node != null) { node.Connect(); // 连接基站(第一个版本,暂时只连接一个基站); } }
/*删除节点*/ public static bool deleteNode(NodeB root, int?data) { if (root == null) { return(false); } /*先找到要删除的节点和他的父节点*/ NodeB parent = null; NodeB child = root; while (true) { if (child == null) { return(false); } if (data == child.Data) { break; } else if (data > child.Data) { parent = child; child = child.RightChild; } else if (data < child.Data) { parent = child; child = child.LeftChild; } } /*判断没找到的结果,return*/ if (child == null) { return(false); } /*判断是否为根节点*/ bool isRoot = root.Data == child.Data; if (isRoot) { if (child.LeftChild == null & child.RightChild == null) { root.Data = null; } else if (child.LeftChild == null) { root = root.RightChild; } else if (child.RightChild == null) { root = root.LeftChild; } /*孩子左右子树都不为空,选择右子树种最大节点来代替该节点*/ else { } } /*不是根节点*/ else { /*判断是否为左子树*/ bool isLeft = parent.LeftChild != null && parent.LeftChild.Data == data; /*目标左右都为空*/ if (child.LeftChild == null && child.RightChild == null) { if (isLeft) { parent.LeftChild = null; } else { parent.RightChild = null; } } else if (child.LeftChild == null) { if (isLeft) { parent.LeftChild = child.RightChild; } else { parent.RightChild = child.RightChild; } } else if (child.RightChild == null) { if (isLeft) { parent.LeftChild = child.LeftChild; } else { parent.RightChild = child.LeftChild; } } /*孩子的左右字数都不为空,找出孩子的右树种最小树来代替*/ else { } } return(true); }
private void VisitI(NodeB n) { System.Console.WriteLine("Node B"); }
// COPY CTOR public NodeA(NodeB copy) { this.Name = copy.Name; this.Children = new List<NodeA>(copy.Children.Select(b => new NodeA(b)); //copy other props }
public NodeBArgs(NodeB nb) { m_NodeB = nb; }
public string ToDetailedString(DataFields dataFields) { return(NodeA.ToDetailedString(dataFields) + " => " + NodeB.ToDetailedString(dataFields) + " || " + GetCalculationsAsString()); }
private void Collect_Node_Click(object sender, EventArgs e) { ObjNode Objnode; List <ObjNode> m_NodeList = new List <ObjNode>(); List <ObjNode> RootNodeShow = new List <ObjNode>(); ObjNode Root = new ObjTreeNode(0, 0, "1.0", "收藏节点", @"/"); NodeB node = new NodeB("172.27.245.92", "NodeB"); string cfgFile = node.m_ObjTreeDataPath; StreamReader reader = File.OpenText(cfgFile); JObject JObj = new JObject(); JObj = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); IEnumerable <int> AllNodes = from nodes in JObj.First.Next.First select(int) nodes["ObjID"]; int TempCount = 0; foreach (var iter in AllNodes) { var ObjParentNodes = (int)JObj.First.Next.First[TempCount]["ObjParentID"]; var name = (string)JObj.First.Next.First[TempCount]["ObjName"]; var TableName = (string)JObj.First.Next.First[TempCount]["MibTableName"]; var version = (string)JObj.First.First; if (JObj.First.Next.First[TempCount]["ObjCollect"] == null) { TempCount++; continue; } int ObjCollect = (int)JObj.First.Next.First[TempCount]["ObjCollect"]; Objnode = new ObjTreeNode(iter, ObjParentNodes, version, name, TableName); if (ObjCollect == 1) { int index = m_NodeList.IndexOf(Objnode); if (index < 0) { m_NodeList.Add(Objnode); } } TempCount++; } reader.Close(); ObjNodeControl Ctrl = new ObjNodeControl(node); // 遍历所有节点确认亲子关系; foreach (ObjNode iter in m_NodeList) { //Root.Add(iter); if (Root.SubObj_Lsit != null) { bIsRepeat = false; IsRepeatNode(iter, Root.SubObj_Lsit); if (bIsRepeat) { continue; } } // 遍历所有节点确认亲子关系; foreach (ObjNode iter1 in Ctrl.m_NodeList) { if (iter1.ObjID == iter.ObjID) { Root.Add(iter1); } else if (iter1.ObjID > iter.ObjID) { foreach (ObjNode iterParent in Ctrl.m_NodeList) { if (iterParent.ObjID == iter1.ObjParentID) { iterParent.Add(iter1); } } } } } RootNodeShow.Add(Root); // 将右侧叶节点容器容器加入到对象树子容器中; this.Obj_Collect.Clear(); this.Obj_Collect.SubExpender = this.FavLeaf_Lists; foreach (ObjNode items in RootNodeShow) { items.TraverseCollectChildren(this.Obj_Collect, this.FavLeaf_Lists, 0); } }
public NodeB() { LeftChild = null; RightChild = null; Data = null; }
public NodeA(NodeB node) { Name = node.Name; Children = node.Children.Select(n => new NodeA(n)).ToList(); }
public new string ToString() { return(NodeA.ToString().PadRight(10) + "<-> " + NodeB.ToString().PadRight(10) + "@".PadLeft(5) + distance.ToString("0.00")); }
public NodeB(int?data) { LeftChild = null; RightChild = null; this.Data = data; }
public bool Equals(EdgeDistance other) { return(((NodeA.Equals(other.NodeA) && NodeB.Equals(other.NodeB)) || (NodeA.Equals(other.NodeB) && NodeB.Equals(other.NodeA))) && Distance.Equals(other.Distance)); }