.net 学习管理C++;仿制药 我在学校C++中创建了一个小型的BST项目。工作得很好,但现在我正试图弄清楚如何使用泛型(第一个项目使用int)完成同一个项目。就我的一生而言,我看不出我到底在搞什么。希望找到方向
GenericNodeClass.h:.net 学习管理C++;仿制药 我在学校C++中创建了一个小型的BST项目。工作得很好,但现在我正试图弄清楚如何使用泛型(第一个项目使用int)完成同一个项目。就我的一生而言,我看不出我到底在搞什么。希望找到方向,.net,generics,c++-cli,binary-search-tree,.net,Generics,C++ Cli,Binary Search Tree,GenericNodeClass.h: /* GenericNodeClass.h. Created by Ed Thompson for IS375, C++ Intermediate, City University of Seattle. GenericNodeClass.h provides for members for creation of a generic binary tree */ #pragma once #include "stdafx.h" #include
/*
GenericNodeClass.h. Created by Ed Thompson for IS375, C++ Intermediate, City University
of Seattle.
GenericNodeClass.h provides for members for creation of a generic binary tree
*/
#pragma once
#include "stdafx.h"
#include <iostream>
#include <deque>
#include <climits>
using namespace std;
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
generic <typename T> where T : IComparable<T>, IEquatable<T>
ref class Node
{
public:
T data;
T parent;
Node<T> ^ left;
Node<T> ^ right;
// Constructor that takes one parameter:
// a T, representing the value of a new node
Node(T n)
{
data = n;
}
};
/*
通用节点类。Ed Thompson创建的IS375,C++中级,城市大学
西雅图的。
h为成员提供创建通用二叉树的功能
*/
#布拉格语一次
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
使用名称空间系统;
使用名称空间系统::集合;
使用命名空间System::Collections::Generic;
一般式,其中T:i可比,i可比
参考类节点
{
公众:
T数据;
T亲本;
节点^左;
节点^right;
//接受一个参数的构造函数:
//T,表示新节点的值
节点(tn)
{
数据=n;
}
};
BinarySearchTreeClass.h
#include "stdafx.h"
#include <iostream>
#include <deque>
#include <climits>
#include "GenericNodeClass.h"
using namespace std;
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
generic <typename T> where T : IComparable<T> , IEquatable<T>
ref class BinarySearchTreeClass
{
public:
Node<T>^ rootNode;
T _p;
// constructors
// default
BinarySearchTreeClass(){}
BinarySearchTreeClass(T t)
{
// create a new Node in the Binary Search Tree
rootNode = gcnew Node<T> (t);
}
// method to return a node if it exists in the tree
// takes two parameters: an instance of the Node class, and a
// value representing the value of the node to be looked up
Node<T> ^ lookUp(Node<T> ^node, T key)
{
if(node == nullptr)
return node;
if(node->data == key)
return node;
else
{
if (node->data->CompareTo(key) < 0)
return lookUp(node->right, key);
else
return lookUp(node->left, key);
}
}
// method to create new node. Method takes two parameters:
// a parameter of the value for the node data, and a value
// for the node's parent.
Node<T> ^newNode(T key, T parent)
{
Node<T> ^node = gcnew Node<T>(key);
node->data = key;
node->left = nullptr;
node->right = nullptr;
node->parent = parent;
return node;
}
// insertNode method inserts a new node into an existing
// Binary Search Tree. This method takes two parameters:
// an instance of Node<T> (node), and a value of T,
// representing the value of the node being inserted
Node<T> ^insertNode(Node<T> ^node, T input)
{
Node<T> ^returnNode;
if (node != nullptr)
_p = node->data;
// if the node is non-existant, create a new node
// with the value of the parameter 'input' and the
// value of the new node's parent (_p).
if (node == nullptr)
{
// pass class variable _p to set value
// of the new node's parent value.
returnNode = newNode(input, _p);
return returnNode;
}
// if the input parameter value is less than
// or equal to the node value, insert node using
// the left hand node leaf of the current node
// as starting point
if (input->CompareTo(node->data) < 0)
{
// set variable p to value of node->data value (this
// will be the parent of the new node)
_p = node->data;
node->left = insertNode(node->left, input);
}
// if the input parameter value is greater than
// the node value, insert node using the right hand
// node leaf of the current node as starting point
else
{
_p = node->data;
node->right = insertNode(node->right, input);
}
return node;
}
// method to find the left most node in a Binary Search Tree
// takes an instance of Node<T> as parameter
Node<T> ^leftMost(Node<T> ^node)
{
if (node == nullptr)
return nullptr;
while (node->left != nullptr)
node = leftMost(node->left);
return node;
}
// method of return the right most node in a Binary Search Tree
// takes an instance of Node<T> as parameter
Node<T> ^rightMost(Node<T> ^node)
{
if (node == nullptr)
return nullptr;
while (node->right != nullptr)
node = rightMost(node->right);
return node;
}
// method to return the size of a Binary Tree
// takes an instance of Node<T> as parameter
int treeSize(Node<T> ^node)
{
if(node == nullptr || node->left == nullptr && node->right == nullptr)
return 0;
else
return treeSize(node->left) + 1 + treeSize(node->right);
}
// method that prints to console the value of each node from lowest to highest
// takes an instance of Node<T> as parameter
void printTreeInOrder(Node<T> ^node)
{
if (node != nullptr)
{
printTreeInOrder(node->left);
Console::WriteLine(node->data);
printTreeInOrder(node->right);
}
}
// method that prints to console a graphic representation of a binary tree
// takes an instance of Node<T> as parameter
void printGraphicRepresentation(Node<T> ^node)
{
// the width of the tree is the max height /2 (?)
// need to know how wide the tree is
// At treeWidth / 2, print node
// on new line, at nodePosition - 1, if node has a left, print a "/"
// on same line, at nodePosition + 1, if node has a right, print a "\"
}
// method that prints to console all members of a given node
// takes an instance of Node<T> as parameter
void printNodeMembers(Node<T> ^node)
{
Console::WriteLine("Members of Node " + node->data + " include: ");
if (node->parent != nullptr)
Console::WriteLine("Parent: " + node->parent);
else
Console::WriteLine("This node has no parent.");
if (node->left != nullptr)
Console::WriteLine("Left-member: " + node->left->data);
else
Console::WriteLine("This node has no left-member.");
if (node->right != nullptr)
Console::WriteLine("Right-Member: " + node->right->data);
else
Console::WriteLine("This node has no right-member.");
}
};
#包括“stdafx.h”
#包括
#包括
#包括
#包括“GenericNodeClass.h”
使用名称空间std;
使用名称空间系统;
使用名称空间系统::集合;
使用命名空间System::Collections::Generic;
一般式,其中T:i可比,i可比
ref类二进制搜索树类
{
公众:
节点^rootNode;
T_p,;
//建设者
//违约
BinarySearchTreeClass(){}
二进制搜索树类(T)
{
//在二进制搜索树中创建新节点
rootNode=gcnewnode(t);
}
//方法返回树中存在的节点
//接受两个参数:节点类的实例和
//值,表示要查找的节点的值
节点^lookUp(节点^Node,T键)
{
if(node==nullptr)
返回节点;
如果(节点->数据==键)
返回节点;
其他的
{
如果(节点->数据->比较(键)<0)
返回查找(节点->右侧,键);
其他的
返回查找(节点->左,键);
}
}
//方法创建新节点。方法采用两个参数:
//节点数据值的参数,以及
//对于节点的父节点。
节点^newNode(T键,T父节点)
{
节点^Node=gcnew节点(键);
节点->数据=键;
节点->左=空PTR;
节点->右=nullptr;
节点->父节点=父节点;
返回节点;
}
//insertNode方法将新节点插入到现有节点中
//二进制搜索树。此方法采用两个参数:
//Node(Node)的一个实例,值为T,
//表示要插入的节点的值
节点^insertNode(节点^Node,T输入)
{
节点^返回节点;
如果(节点!=nullptr)
_p=节点->数据;
//如果节点不存在,请创建一个新节点
//使用参数“input”的值和
//新节点的父节点的值(\u p)。
if(node==nullptr)
{
//将类变量p传递给设置值
//新节点的父值。
returnNode=newNode(输入,\u p);
返回节点;
}
//如果输入参数值小于
//或等于节点值,使用插入节点
//当前节点的左侧节点叶
//作为起点
如果(输入->比较(节点->数据)<0)
{
//将变量p设置为节点->数据值的值(此
//将是新节点的父节点)
_p=节点->数据;
节点->左=插入节点(节点->左,输入);
}
//如果输入参数值大于
//节点值,使用右手插入节点
//当前节点的节点叶作为起点
其他的
{
_p=节点->数据;
节点->右侧=插入节点(节点->右侧,输入);
}
返回节点;
}
//方法在二叉搜索树中查找最左边的节点
//将节点的实例作为参数
节点^最左侧(节点^节点)
{
if(node==nullptr)
返回空ptr;
while(节点->左!=nullptr)
节点=最左侧(节点->左侧);
返回节点;
}
//返回二叉搜索树中最右边节点的方法
//将节点的实例作为参数
节点^最右侧(节点^节点)
{
if(node==nullptr)
返回空ptr;
while(节点->右侧!=nullptr)
节点=最右侧(节点->右侧);
返回节点;
}
//方法返回二叉树的大小
//将节点的实例作为参数
int treeSize(节点^Node)
{
if(node==nullptr | | node->left==nullptr&&node->right==nullptr)
返回0;
其他的
返回树大小(节点->左)+1+树大小(节点->右);
}
//方法,从最低到最高打印每个节点的值
//将节点的实例作为参数
void printTreeInNorder(节点^Node)
{
如果(节点!=nullptr)
{
PrintTreeInNorder(节点->左侧);
控制台::写线(节点->数据);
PrintTreeInNorder(节点->右侧);
}
}
//方法,该方法向控制台打印二叉树的图形表示
//将节点的实例作为参数
无效PrintGraphicsRepresentation(节点^Node)
{
//树的宽度是最大高度/2(?)
//需要知道这棵树有多宽
//在树宽/2处,打印节点
//在新行中,在节点位置-1处,如果节点有一个左边,则打印一个“/”
//在同一行上,在nodePosition+1处,若节点有右侧,则打印一个“\”
}
//方法,该方法打印到控制台给定节点的所有成员
//将节点的实例作为参数
无效打印节点成员(节点^Node)
{
控制台::WriteLine(“节点成员”+节点->数据+”包括:”;
如果(节点->父节点!=nullptr)
控制台::WriteLine(“父节点:+节点->父节点”);
其他的
Console::WriteLine(“此节点没有父节点”);
如果(节点->左!=nullptr)
控制台::WriteLine(“左成员:”+节点->左->数据);
其他的
Console::WriteLine(“此节点没有左成员”);
如果(节点->右侧!=nullptr)
控制台::WriteLine(“右成员:”+节点->右->数据);
其他的
Console::WriteLine(“此节点没有正确的成员。”);
}
};
<
#include "stdafx.h"
#include "GenericNodeClass.h"
#include "BinarySearchTreeClass.h"
#include <iostream>
using namespace std;
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
int main ()
{
BinarySearchTreeClass<int>^ BTree = gcnew BinarySearchTreeClass<int>();
// instantiate new Node instance with starter value
Node<int> ^rootNode = gcnew Node<int>(5);
// insert additional nodes
BTree->insertNode(rootNode, 3);
BTree->insertNode(rootNode, 7);
BTree->insertNode(rootNode, 4);
BTree->insertNode(rootNode, 6);
BTree->insertNode(rootNode, 2);
BTree->insertNode(rootNode, 9);
BTree->insertNode(rootNode, 1);
BTree->insertNode(rootNode, 8);
BTree->insertNode(rootNode, 0);
BTree->insertNode(rootNode, 10);
BTree->insertNode(rootNode, 12);
BTree->insertNode(rootNode, 11);
Console::WriteLine("The value of the left-most node in this tree is: " + BTree->leftMost(rootNode)->data);
Console::WriteLine("The value of the right-most node in this tree is: " + BTree->rightMost(rootNode)->data);
Console::WriteLine("The size of this Binary Search Tree is: " + BTree->treeSize(rootNode));
Console::WriteLine("Printing Binary Search Tree in order: ");
BTree->printTreeInOrder(rootNode);
// TO EXAMINE THE PROPERTIES OF A GIVEN NODE, CHANGE THE SECOND PARAMETER OF
// THE lookUp METHOD IN THE FOLLOWING LINE OF CODE TO THE NODE DESIRED TO BE EXAMINED
BTree->printNodeMembers(BTree->lookUp(rootNode, 7));
Console::WriteLine();
return 0;
}
generic <typename T> where T : IComparable<T> ref class Node { ... };
generic <typename T> where T : IComparable<T> ref class BinarySearchTreeClass { ... };
if (node->data->CompareTo(key) < 0)
return lookUp(node->right, key);
else
return lookUp(node->left, key);