复习笔记二 二叉排序树(C++ C#)
来源:互联网 发布:网络测试仪精明鼠 编辑:程序博客网 时间:2024/06/04 01:19
#include"iostream.h"
#include"string.h"
class node //节点类
{
public:
int data;
node *l;
node *r;
node(int p_data);
node(node *p_node);
};
node::node(int p_data)
{
data=p_data;
l=NULL;
r=NULL;
}
node::node(node *p_node) //拷贝构造
{
data=p_node->data;
l=p_node->l;
r=p_node->r;
}
class btree //二叉树
{
public:
node *Root; //根
btree(){Root=NULL;}
node add(node *p,int p_data);//构造二叉排序树
void display(node *p);
node add(int p_data);
};
node btree::add(int p_data)
{
if(Root==NULL)
{
Root=new node(p_data);
}
else
{
add(Root,p_data);
}
return *Root;
}
node btree::add(node *p,int p_data)
{
if(p==NULL)
{
p=new node(p_data);
}
else if(p_data<p->data)
{
p->l=new node(add(p->l,p_data));
}
else
{
p->r=new node(add(p->r,p_data));
}
return *p;
}
void btree::display(node *p)
{
cout<<"***********"<<endl;
if(p==NULL)
return;
else
{
display(p->l);
cout<<p->data<<endl;
display(p->r);
}
}
void main()
{
btree mybtree;
int a;
cin>>a;
for(int i=0;i<a;i++)
{
int data;
cin>>data;
mybtree.add(mybtree.Root,data);
}
mybtree.display(mybtree.Root);
}
C#版
//Program.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉排序树
{
class Program
{
static void Main(string[] args)
{
Node n = null;
tree mytree = new tree();
int p = int.Parse(Console.ReadLine());
for (int i = 0; i < p; i++)
{
int data = int.Parse(Console.ReadLine());
n = mytree.Add(n, data);
}
mytree.display(n);
Console.ReadLine();
}
}
}
//Node.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉排序树
{
public class Node
{
public Node Left = null;
public Node Right = null;
public int Data;
public Node(int data)
{
Data = data;
}
}
}
//tree.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉排序树
{
public class tree
{
public Node Add(Node p, int p_data)
{
if (p == null)
{
p = new Node(p_data);
}
else
{
if (p_data < p.Data)
{
p.Left = Add(p.Left, p_data);
}
else
{
p.Right = Add(p.Right, p_data);
}
}
return p; ;
}
public void display(Node h)
{
Console.WriteLine("****");
while (h != null)
{
Console.WriteLine(h.Data.ToString());
h = h.Left;
}
}
}
}
- 复习笔记二 二叉排序树(C++ C#)
- C语言复习笔记二
- C语言复习笔记二:输入输出
- C语言复习笔记(二)
- 二叉排序树C语言实现二
- C语言复习二
- c复习笔记
- C语言复习笔记
- C语言复习笔记
- C笔记复习
- C--复习笔记
- C & C++ 复习笔记
- 二叉排序树(c++)
- 二叉排序树 c
- C总结复习(二)
- C/C++语言复习二
- C复习笔记(1)-6.16
- C复习笔记(2)-6.18
- 在SQLServer2005中,新建一个用户,并分配一个角色
- win2000命令大全
- 主题:本人工作几年的经验总结,不看后悔!
- 开始→运行(cmd)命令大全
- 快速排序算法复习
- 复习笔记二 二叉排序树(C++ C#)
- UML-结构建模(一)
- D014SP02版本随感
- 架构蓝图--软件架构 "4+1" 视图模型
- 业务建模和领域模型
- Python:定时运行脚本
- 软件开发技术之个人见解
- 只言片语
- ipcrm命令