//Given is the list of node and it has parent, value and weight //To calculate total weight of the subtree we need to know the parent and child relation that is Parent and List<children> //we can either use ht to build the structure or modify the node to have this property //Find the root of the tree //And then use DFS to find the tree's weight..go to children first and then sibling private void PrintSubTreeWeight(List <NodeWithWeight> nodes) { //Build ht with key as parent and value as list of children Dictionary <NodeWithWeight, List <NodeWithWeight> > htparentchild = new Dictionary <NodeWithWeight, List <NodeWithWeight> >(); //Build another ht with key as childnode and value as parent ..this is used to find the root //we can use childparent map to find the root but here since we already loop through the nodes, we can find the root without the use of this ht //Dictionary<NodeWithWeight, NodeWithWeight> htchildparentmap = new Dictionary<NodeWithWeight, NodeWithWeight>(); NodeWithWeight root = null; foreach (NodeWithWeight node in nodes) { //we have to build parent and list of chilren if (node.Parent != null) { if (!htparentchild.ContainsKey(node.Parent)) { htparentchild.Add(node.Parent, new List <NodeWithWeight>()); } htparentchild[node.Parent].Add(node); } else { root = node; } } //void function that set the weight prop of the tree //test get the root weight int result = CalculateTreeWeight(root, htparentchild); }
private void button11_Click(object sender, EventArgs e) { //List of Nodes //http://www.careercup.com/question?id=5648527329853440 //id,parent,weight //10,30,1 //30,0,10 //20,30,2 //50,40,3 //40,30,4 //we parse the given input using ht and get the structure of the tree like this // 0 // 30 // 10 20 40 // 50 NodeWithWeight node0 = new NodeWithWeight(0, 0); NodeWithWeight node30 = new NodeWithWeight(30, 10); NodeWithWeight node10 = new NodeWithWeight(10, 1); NodeWithWeight node20 = new NodeWithWeight(20, 2); NodeWithWeight node40 = new NodeWithWeight(40, 4); NodeWithWeight node50 = new NodeWithWeight(50, 3); node30.Parent = node0; node10.Parent = node30; node20.Parent = node30; node40.Parent = node30; node50.Parent = node40; List <NodeWithWeight> nodes = new List <NodeWithWeight>(); nodes.Add(node0); nodes.Add(node30); nodes.Add(node10); nodes.Add(node20); nodes.Add(node40); nodes.Add(node50); PrintSubTreeWeight2(nodes); }
//Given is the list of node and it has parent, value and weight //To calculate total weight of the subtree we need to know the parent and child relation that is Parent and List<children> //we can either use ht to build the structure or modify the node to have this property //Find the root of the tree //And then use DFS to find the tree's weight..go to children first and then sibling private void PrintSubTreeWeight2(List <NodeWithWeight> nodes) { NodeWithWeight root = null; //Change the structure of the tree to have list<childre> foreach (NodeWithWeight node in nodes) { if (node.Parent != null) { node.Parent.Childs.Add(node); } else { root = node; } } int totalweight = CalculateTreeWeight(root); }
private int CalculateTreeWeight(NodeWithWeight root) { if (root == null) { return(0); } int Weight = root.Weight; //get the weight of the subtrees if (root.Childs.Count > 0) { foreach (NodeWithWeight child in root.Childs) { Weight += CalculateTreeWeight(child); } } //set the prop of the tree root.TreeWeight = Weight; return(Weight); }
private int CalculateTreeWeight(NodeWithWeight root, Dictionary <NodeWithWeight, List <NodeWithWeight> > htparentchild) { if (root == null) { return(0); } int Weight = root.Weight; //get the weight of the subtrees if (htparentchild.ContainsKey(root)) { List <NodeWithWeight> childrens = htparentchild[root]; foreach (NodeWithWeight child in childrens) { Weight += CalculateTreeWeight(child, htparentchild); } } //set the prop of the tree root.TreeWeight = Weight; return(Weight); }