编程“稀疏矩阵转置并显示”。稀疏矩阵的转置。

2024-11-22 08:17:32
推荐回答(1个)
回答(1):

第二题:C#编写1.Node:///

/// Class Node of binary tree/// public class Node{private string value = string.Empty;/// /// Gets or sets the value of this Node/// public string Value{get { return this.value; }set { this.value = value; }}private Node leftChild = null;/// /// Gets or sets the left child of this Node/// public Node LeftChild{get { return leftChild; }set { leftChild = value; }}private Node rightChild = null;/// /// Gets or sets the right child of this Node/// public Node RightChild{get { return rightChild; }set { rightChild = value; }}} 2.Binary Tree:/// /// Class BinaryTree/// public class BinaryTree{/// /// Get the count of branch nodes/// /// The root node/// Count of branch nodespublic static int GetBranchNodesCount(Node root){int countOfBranchNodes = 0;MyQueue queue = new MyQueue();queue.EnQueue(root);while (!queue.IsEmpty()){Node tempNode = queue.DeQueue();if (IsHaveChilds(tempNode)){countOfBranchNodes++;}if (tempNode.LeftChild != null){queue.EnQueue(tempNode.LeftChild);}if (tempNode.RightChild != null){queue.EnQueue(tempNode.RightChild);}}return countOfBranchNodes;}/// /// Get the count of leaf nodes/// /// The root node/// Count of leaf nodespublic static int GetLeafNodesCount(Node root){int countOfLeafNodes = 0;MyQueue queue = new MyQueue();queue.EnQueue(root);while (!queue.IsEmpty()){Node tempNode = queue.DeQueue();if (!IsHaveChilds(tempNode)){countOfLeafNodes++;}if (tempNode.LeftChild != null){queue.EnQueue(tempNode.LeftChild);}if (tempNode.RightChild != null){queue.EnQueue(tempNode.RightChild);}}return countOfLeafNodes;}/// /// Check the node have childs or not/// /// The check node/// Ture if have childs , otherwise falseprivate static bool IsHaveChilds(Node node){if (node.LeftChild != null || node.RightChild != null){return true;}else{return false;}}} 3.MyQueue:/// /// Use a LinkedList to create a Queue (First In First Out)/// /// The queue's value's typepublic class MyQueue{private LinkedList lst = new LinkedList();/// /// Push a new object in a queue's tail/// /// the new objectpublic void EnQueue(T obj){lst.AddLast(obj);}/// /// Pop the queue's head out of the queue /// /// the pop up datapublic T DeQueue(){LinkedListNode t = lst.First;lst.RemoveFirst();return t.Value;}/// /// return the queue is empty or not/// /// true if empty , otherwise falsepublic bool IsEmpty(){return lst.Count == 0 ? true : false;}} 代码很容易看懂,就不多解释了