复习笔记二 二叉排序树(C++ C#)
2007-12-24 19:41
489 查看
C++ 版
#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;
}
}
}
}
文章引用自:
#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;
}
}
}
}
文章引用自:
相关文章推荐
- .Net学习笔记----2015-07-21(C#基础复习03,简单工厂和抽象类)
- .Net学习笔记----2015-07-23(C#基础复习11,字符串复习)
- C++要点复习笔记
- C#复习笔记(1)
- c#复习笔记1
- C/C++知识点复习笔记18/3/7
- 【学习笔记】C++传递字符串到C#
- 【C++】笔记四:C++类通俗点说—— C结构体复习
- C++复习笔记(3)类的多态性
- PInvoke复习之C# C++ 字符串传递
- .Net学习笔记----2015-07-21(C#基础复习05,里氏转换)
- C/C++知识点复习笔记18/2/28
- 标准C++复习&学习笔记
- c# 语法笔记 适用于从C++转过的,用于速查
- C#复习笔记(3)
- 复习C++ 走一遍基础 留下笔记----{二}
- 复习C++ 走一遍基础 留下笔记----{三}
- 【C#学习笔记】调用C++生成的DLL
- c++复习笔记(动态数组)
- C#转学C++ MFC笔记一