My procedure works fine with a Do-While but I think it must do a lot of iterations.
Is there a faster way to remove nodes without childs? Remember if you remove a node and it is a single node from its parent node then it must be removed later again.
int counter=0;
private void EliminaParentsWithoutChilds()
{
do
{
counter= 0;
foreach (TreeNode node in treeView1.Nodes)
{
RemoveEmptyNodes(node);
}
}
while (counter > 0);
}
private void RemoveEmptyNodes(TreeNode node)
{
if (node.Nodes.Count > 0)
{
foreach (TreeNode childNode in node.Nodes)
{
RemoveEmptyNodes(childNode);
}
}
else
{
node.Remove();
counter++;
}
}
You can create a method that tell you when a node is empty or has only one child in all descendants:
private static bool IsUniqueOrEmptyNode(TreeNode node)
{
return node.Nodes.Count == 0 ||
(node.Nodes.Count == 1 && IsUniqueOrEmptyNode(node.Nodes[0]));
}
Next method remove only the top parent that hasn't childs or that contains only one a single direct child:
private static void RemoveEmptyNodes(TreeNodeCollection nodes)
{
for (int i = nodes.Count - 1; i >= 0; i--)
{
if (IsUniqueOrEmptyNode(nodes[i]))
{
nodes.RemoveAt(i);
}
else
{
RemoveEmptyNodes(nodes[i].Nodes);
}
}
}
And a helper method to apply to a TreeView:
private static void RemoveEmptyNodes(TreeView tree)
{
RemoveEmptyNodes(tree.Nodes);
}
Related
first post and a new coder so bare with me if you need more info than I am giving. I am trying to create a treeview with checkboxes in a hierarchy (see pic). My issue is i want to create some sort of recursion which deselects and selects child nodes when parent nodes are checked or vice versa.
I am using VS with winforms and been googling for 2 days on how to do this, unfortunately the examples online are either too advanced for me or dont work. I found a Tutorial on how to do this exactly with indeterminate checkboxes as well which would be a big bonus but it is for WPF.
I managed to create buttons which are able to (un)check all buttons with some examples online. Please can someone guide, a beginner who is finding programming AMAZING so far, in the right direction :)
private void button_checkAllNodes_Click(object sender, EventArgs e)
{
checkAllNodes(treeView1.Nodes);
}
private void button_uncheckAllNodes_Click(object sender, EventArgs e)
{
UncheckAllNodes(treeView1.Nodes);
}
public void checkAllNodes(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
node.Checked = true;
checkChildren(node, true);
}
}
public void UncheckAllNodes(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
node.Checked = false;
checkChildren(node, false);
}
}
private void checkChildren(TreeNode rootNode, bool isChecked)
{
foreach (TreeNode node in rootNode.Nodes)
{
checkChildren(node, isChecked);
node.Checked = isChecked;
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
Picture Treeview with (un)check All buttons
Let's create a couple of extension methods for the TreeNode type, one that gets all the children of a node, and another that gets it's parents.
// Within your project's namespace...
static class TreeViewExtensions
{
public static IEnumerable<TreeNode> Children(this TreeNode node)
{
foreach (TreeNode n in node.Nodes)
{
yield return n;
foreach (TreeNode child in Children(n))
yield return child;
}
}
public static IEnumerable<TreeNode> Parents(this TreeNode node)
{
var p = node.Parent;
while (p != null)
{
yield return p;
p = p.Parent;
}
}
}
Now, all what you need to do is to handle the TreeView.AfterCheck event to toggle the Checked property of the nodes that the extension methods yield.
// +
using System.Linq;
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.Unknown) return;
foreach (TreeNode n in e.Node.Children())
n.Checked = e.Node.Checked;
// Comment this if you don't need it.
foreach (TreeNode p in e.Node.Parents())
p.Checked = p.Nodes.OfType<TreeNode>().Any(n => n.Checked);
}
Soon, you'll notice sometimes this solution won't work as it should when you click rapidly over the check boxes since they don't receive the mouse double click messages by default. Then, follow this post or this to solve this problem. For now click slowly.
If you prefer though to use buttons to toggle the check state, then delete the AfterCheck handler and do instead:
private void btnCheckAll_Click(object sender, EventArgs e)
{
ToggleCheck(treeView1.SelectedNode, true);
}
private void btnUncheckAll_Click(object sender, EventArgs e)
{
ToggleCheck(treeView1.SelectedNode, false);
}
private void ToggleCheck(TreeNode node, bool state)
{
node.Checked = state;
foreach (TreeNode n in node.Children())
n.Checked = state;
// Optional...
foreach (TreeNode n in node.Parents())
n.Checked = state;
}
I agree with #jdweng , you are using recursion in checkChildren(). The base case is missing.
In recursion checkChildren , add base case before
foreach (TreeNode node in rootNode.Nodes)
if node is Null : rootNode=isChecked
i tried to fix a code, which is a LinkedList. The task is to Remove the last X elements of the list.
I tried it with RemoveRange, but VS don't accept my solution and says, that RemoveRange doesn't exist.
var list = new DoublyLinkedList<string>();
list.Add("A");
list.Add("B");
list.Add("C");
list.Add("D");
list.Add("E");
list.RemoveLast(2);
This is the Code in the Program (Main).
In a second class there should be the method RemoveLast, but i dont get a working code. Can someone explain me, how i get the RemoveLast?
using System;
using System.Collections;
using System.Collections.Generic;
namespace Test
{
public class DoublyLinkedList<T> : IEnumerable<T>
{
public void RemoveLast(int v)
{
int remove = Math.Max(0, this.Count - v);
this.RemoveRange(v, this.Count - v);
}
}
}
RemoveRange is red underlined
Thank you for your help!
Full DoublyLinkedList:
`using System;
using System.Collections;
using System.Collections.Generic;
namespace Test
{
public class DoublyLinkedList<T> : IEnumerable<T>
{
public void RemoveLast(int v)
{
int remove = Math.Max(0, this.Count - v);
this.RemoveRange(v, this.Count - v);
}
private sealed class Node
{
public T Item { get; set; }
public Node Previous { get; set; }
public Node Next { get; set; }
}
private Node first, last;
public int Count { get; private set; }
public void Add(T item)
{
Node newItem = new Node() { Item = item, Next = null, Previous = null };
if (first == null)
{
first = newItem;
last = newItem;
}
else
{
last.Next = newItem;
newItem.Previous = last;
last = newItem;
}
Count++;
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
Node node = first;
while (node != null)
{
yield return node.Item;
node = node.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
public override string ToString()
{
string s = "";
Node node = first;
while (node != null)
{
s += node.Item.ToString() + " -> ";
node = node.Next;
}
s += "Count: " + Count.ToString();
return s;
}
private Node find(T item)
{
Node node = first;
while (node != null)
{
if (node.Item.Equals(item))
return node;
node = node.Next;
}
return null;
}
private Node findPrevious(T item)
{
Node previousNode = null;
Node node = first;
while (node != null)
{
if (node.Item.Equals(item))
return previousNode;
previousNode = node;
node = node.Next;
}
return null;
}
}
}`
You do know there is already a double linked list class, don't you?
System.Collections.Generic.LinkedList? My advice would be to use that class.
If it is too much work to redesign your code, for instance because your DoublyLinkedList is already used a lot, my advice would be to make DoublyLinkedList an adapter for LinkedList:
class DoublyLinkedList<T> : IEnumerable<T>, IEnumerable
{
private readonly LinkedList<T> linkedList = new LinkedList<T>();
public int Count => this.linkedList.Count;
public void Add(T item)
{
this.LinkedList.Add(item);
}
public IEnumerator<T> GetEnumerator()
{
return this.LinkedList.GetEnumerator();
}
... // etc.
}
You need to add a method to remove the last N items from your list. For example
RemoveLast(10) is supposed to remove the last 10 elements from your doubly linked list. If your list has 10 or less elements, this would clear your complete list.
void Clear()
{
this.LinkedList.Clear();
}
void RemoveLast()
{
if (this.LinkedList.Count != 0)
this.linkedList.RemoveLast();
}
void RemoveLast(int removeCount)
{
if (this.Count <= removeCount)
{
this.linkedList.Clear();
}
else
{
for (int i=0; i<removeCount; ++i)
{
this.RemoveLast();
}
}
}
It might be that your supervisor is stubborn and does not follow your advise to reuse fully tested trustworthy .NET classes. In that case you'll have to change the RemoveLast() method.
void Clear()
{
this.first = null;
this.last = null;
this.count = 0;
}
void RemoveLast()
{
switch (this.Count)
{
case 0:
// empty list; do nothing
break;
case 1:
// removing the last element of the list
this.Clear();
break;
default:
var lastNode = this.last;
// because more than one element I'm certain there is a previous node
var previousNode = lastNode.Previous;
var previousNode.Next = null;
this.last = previousNode;
--this.count;
break;
}
}
Here is how you can implement RemoveLast(int n) in your DoublyLinkedList:
// Removes last "n" elements.
public void RemoveLast(int n)
{
for (int i = 0; i < n; i++)
RemoveLast();
}
// Removes the last element.
public void RemoveLast()
{
// List is empty. No need to remove elements.
if (first == null)
{
return;
}
// List contains only one element. Remove it.
else if (first == last)
{
first = null;
last = null;
}
// List contains more than one element. Remove the last.
else
{
// The next two lines make "last" to point to the element before the last.
last = last.Previous;
last.Next = null;
}
Count--;
}
Here is complete sample.
If RemoveRange is not available, you can easiliy roll your own implementation that works on any enumerable without Linq in this way (this code is an idea as I do not have access to all your code).
using System;
using System.Collections;
using System.Collections.Generic;
public void RemoveRange(int count)
{
if (count > this.Count)
throw new ArgumentOutOfRangeException(nameof(count));
while (count > 0)
{
RemoveTail();
count--;
}
}
private void RemoveTail()
{
if (this.Count == 0)
return;
var previous = last.Previous;
if (previous != null)
{
previous.Next = null;
last = previous;
this.Count--;
}
else
{
// this implies count == 1
last = null;
first = null;
this.Count = 0;
}
}
Essentially, you can expose your RemoveRange method and then perform an agnostic removal of the last node (tail) one by one.
This answer has been edited to reflect the code changes.
So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a singly linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would be 2, 3, 4 and 1 gets moved to the future category).
public class UnderflowException : Exception
{
public UnderflowException(string s) : base(s) { }
}
public class OverflowException : Exception
{
public OverflowException(string s) : base(s) { }
}
class History
{
protected class IntListNode
{
public string Data;
public IntListNode Next;
public IntListNode(string data)
{
Data = data;
}
public IntListNode(string data, IntListNode next)
{
Next = next;
}
}
protected IntListNode first;
private int i;
public void PrintAll()
{
int j = 0;
IntListNode node = first;
Console.WriteLine("Privious things that you have viewed.");
while (node != null)
{
if (counter <= j)
{
break;
}
Console.WriteLine(node.Data);
node = node.Next;
j++;
}
Console.WriteLine("Things to go forward to.");
while (node != null)
{
Console.WriteLine(node.Data);
node = node.Next;
}
}
private int counter;
public void MoveBackwards()
{
if (counter >= 0)
{
counter = counter - 1;
}
else
{
throw new UnderflowException("underflow");
}
}
public void MoveForwards()
{
if (counter > i)
{
throw new OverflowException("overflow");
}
else
{
counter++;
}
}
public void VisitPage(string desc)
{
IntListNode n = new IntListNode(desc);
n.Next = this.first;
this.first = n;
counter++;
i = counter;
}
}
When I already have items in the list and ask it to move one backwards it takes the first node not the last in the list. from earlier example wanting it to go from 1, 2, 3, 4 use the go backwards command and have the history display 1, 2, 3 and the forward display 4.
Here's an example based on your code, though changed slightly.
Instead of tracking counter and i for our state, I just keep track of two nodes: head (the first one) and current (the one the user is on right now). I'm also inserting new pages at the current.Next node instead of at the head node because that's how I'm used to using a linked list.
By doing this, it makes navigation easy. To go forward, we just set current = current.Next, and to go backward we start at the head and move forward until we find the node whose Next is pointing to current. Then we set current to that node.
To print out the history, we just start at the head and keep moving Next. When we see that Next == current, we know we're at the current page (and I print that in a different color). Then we can keep printing the Next nodes to show the future nodes, until Next is null.
Note that this is really navigation history, not a complete record of browsing history, because if you go back and then visit a new page, you lose the page you went back from.
Hope this helps:
class History
{
private class Node
{
public string Data { get; set; }
public Node Next { get; set; }
public Node(string data) { Data = data; }
}
private Node head;
private Node current;
public void VisitNewPage(string desc)
{
// Create a node for this page
var node = new Node(desc);
// If it's our first page, set the head
if (head == null) head = node;
// Update our current.Next pointer
if (current != null) current.Next = node;
// Set this page as our current page
current = node;
}
public void MoveBackwards()
{
// Can't move backwards from the head
if (current == head) return;
var previous = head;
// Find the node that's behind (pointing to) the current node
while (previous.Next != current)
{
previous = previous.Next;
}
// Make that node our new current
current = previous;
}
public void MoveForwards()
{
// Just move to the next node
if (current.Next != null) current = current.Next;
}
public void PrintCurrent()
{
Console.WriteLine($"You are on page: {current.Data}");
}
public void PrintHistory()
{
Console.WriteLine("\nBrowsing History");
if (head == null)
{
Console.WriteLine("[Empty]");
return;
}
var node = head;
// Print previous pages
while (node != current)
{
Console.WriteLine($" - {node.Data}");
node = node.Next;
}
// Print current page in green
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($" - {node.Data}");
Console.ResetColor();
node = node.Next;
// Print next pages
while (node != null)
{
Console.WriteLine($" - {node.Data}");
node = node.Next;
}
Console.WriteLine();
}
}
Sample Usage
Here's a simple infinite loop that lets you visit new sites, move forwards, backwards, and print the history:
private static void Main()
{
var history = new History();
while (true)
{
Console.Write("Enter new page to visit, [b]ack, [f]orward, or [p]rint: ");
var input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input)) continue;
switch (input.ToLower())
{
case "b":
case "back":
history.MoveBackwards();
break;
case "f":
case "forward":
history.MoveForwards();
break;
case "p":
case "print":
history.PrintHistory();
break;
default:
history.VisitNewPage(input);
break;
}
}
}
Output
I have a tree view with checkboxes in c#, I want that when the user checks one node all the nodes that there are on the levels below automatic checked also.
Does anyone know about way to do that without run with recorsive fnction on all the tree each time that the user checks some node?
Thanks
//this function returns the treeView.
public TreeView GetTreeView()
{
getSubject();
// fill the treeview with all subjects.
foreach (Subject subject in subjects)
{
//for each root subject fill all the his children.
if (subject.subjestId == subject.parentSubject)
{
TreeNode node = new TreeNode(subject.subjectString, subject.subjestId, subject.subjestId);
addChild(node, subject.subjestId);
tv.Nodes.Add(node);
}
}
return tv;
}
// for each subject return sub subjects.
private void addChild(TreeNode node, int parentId)
{
foreach (Subject subject in subjects)
{
if (subject.parentSubject == parentId && subject.parentSubject != subject.subjestId)
{
TreeNode childNode = new TreeNode(subject.subjectString, subject.subjestId, subject.subjestId);
addChild(childNode, subject.subjestId);
node.Nodes.Add(childNode);
}
}
}
Recursion. Like this:
bool busy = false;
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
if (busy) return;
busy = true;
try {
checkNodes(e.Node, e.Node.Checked);
}
finally {
busy = false;
}
}
private void checkNodes(TreeNode node, bool check) {
foreach (TreeNode child in node.Nodes) {
child.Checked = check;
checkNodes(child, check);
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e) {
foreach (TreeNode child in e.Node.Nodes) {
child.Checked = e.Node.Checked;
}
}
This is a better solution
private void trvMenuList_AfterCheck(object sender, TreeViewEventArgs e)
{
SetChildrenChecked(e.Node, e.Node.Checked);
}
private void SetChildrenChecked(TreeNode treeNode, bool checkedState)
{
foreach (TreeNode item in treeNode.Nodes)
{
if (item.Checked != checkedState)
{
item.Checked = checkedState;
}
SetChildrenChecked(item, item.Checked);
}
}
As a number of the answers state, create a recursive 'set checked to children' function, then call it AfterCheck on the tree.
The framework unfortunately gives you a call back to AfterCheck even if you set the check value in code, and although this may not be noticeable in small trees adds a massive amount of exponential extra work for your app to do. To avoid it, filter AfterCheck to only fire your new function if it has been triggered by user.
private void tree_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
SetChildrenChecked(e.Node);
}
}
private void SetChildrenChecked(TreeNode treeNode)
{
foreach (TreeNode item in treeNode.Nodes)
{
if (item.Checked != treeNode.Checked)
{
item.Checked = treeNode.Checked;
}
if (item.Nodes.Count > 0)
{
SetChildrenChecked(item);
}
}
}
I expanded on the answer a little; by updating the parent as well. [The DisplayException method inside the catch is just a popup window that I always use; you can do your own]
private bool _busy = false;
private void treeViewPassFail_AfterCheck(object sender, TreeViewEventArgs e)
{
try
{
if (_busy)
{
return;
}
_busy = true;
CheckNodes(e.Node, e.Node.Checked);
CheckParent(e.Node.Parent);
}
catch(Exception ex)
{
DisplayException(ex);
}
finally
{
_busy = false;
}
}
private void CheckNodes(TreeNode node, bool check)
{
foreach(TreeNode child in node.Nodes)
{
child.Checked = check;
CheckNodes(child, check);
}
}
private void CheckParent(TreeNode parent)
{
if (parent != null)
{
bool allChecked = true;
foreach (TreeNode node in parent.Nodes)
{
allChecked &= node.Checked;
}
parent.Checked = allChecked;
}
}
If you want to do it in WinForms then I think you have to do it manually by recursion - I don't know any better way.
I have a very strange issue. Basically I have created a class called TreeNode that represents a node in a tree. I then create the tree by adding all the nodes to a List.
class TreeNode
{
private TreeNode parent, lChild, rChild;
private int key, val;
public int Key
{
get { return key; }
set { key = value; }
}
public int Val
{
get { return val; }
set { val = value; }
}
public TreeNode Parent
{
get { return parent; }
set { parent = value; }
}
public TreeNode LChild
{
get { return lChild; }
}
public TreeNode RChild
{
get { return rChild; }
}
public TreeNode(int k, int v)
{
key = k;
val = v;
}
public void SetChild(TreeNode leftChild, TreeNode rightChild)
{
this.lChild = leftChild;
this.rChild = rightChild;
}
public bool isLeaf()
{
if (this.lChild == null && this.rChild == null)
{
return true;
} else
{
return false;
}
}
public bool isParent()
{
if (this.parent == null)
{
return true;
}
else
{
return false;
}
}
public void SetParent(TreeNode Parent)
{
this.parent = Parent;
}
}
So if i put a breakpoint just after the creation of the tree and hover over the list in Visual Studio I can see the structure of the tree - with all the references working perfectly from the root down to the leaves.
If however I do the following:
TreeNode test = newTree[newTree.Count - 1];
please note:
private List<TreeNode> newTree = new List<TreeNode>();
which returns the root node - and hover over again I can do down one level (i.e. left child or right child) but these children do not have any references for their children after that.
I'm wondering if I'm losing the reference in memory to the other nodes in the list as the test node is not part of the list?
Any help would be greatly appreciated.
Thanks
Tom
you sure you don't have (note no space between new and tree in your code)
TreeNode test = new Tree[newTree.Count - 1];
Which would create a new empty array of tree (probably not what you intended), and leave your original tree un-rooted and inaccessible.
Can you make sure your code is correct, please?
Seems as though I found the issue - I wasn't correctly updating some of the parent nodes with their relevant child nodes - problem solved.
Thanks for your help
Tom