求平衡二叉树的算法(50分)

  • 主题发起人 xiaolin0522
  • 开始时间
X

xiaolin0522

Unregistered / Unconfirmed
GUEST, unregistred user!
c,pascal 都可以,算法描述也可以。
 
请实现平衡二叉树的动态插入和调整过程的演示。
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
type
pointer=^node;
node=record
data:real;
left,right:pointer;
end;
var
x:real;
root:pointer;
{$R *.DFM}
procedure add_tree(x:real;
var p:pointer);
begin
if p=nil
then
begin
// 加入一节点
new(p);
with p^do
begin
data:=x;
left:=nil;
right:=nil;
end
end
else
with p^do
if x<data
then
add_tree(x,left) //加入左子树
else
add_tree(x,right)//加入右子树
end;
procedure inorder(p:pointer);

begin
if p<>nil then
begin
inorder(p^.left);
form1.listbox2.items.add(floattostr(p^.data));
inorder(p^.right);
end
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
listbox1.clear;
i:=0;
root:=nil;
x:=strtofloat(memo1.lines[0]);
listbox1.items.add(floattostr(x));
while x>0do
begin
i:=i+1;
add_tree(x,root);
x:=strtofloat(memo1.lines);
if (x>0) then
listbox1.items.add(floattostr(x));
end;
end;

 
上面好像 只是 实现了 二叉树吧。
 
我有C++描述的,要不要?
 
要啊,当然要
有注释和算法说明,希望偶能看懂
xiaolin0522@163.com
 
xiaolin0522@163.com
 
http://www.vchelp.net/vchelp/archive.asp?type_id=51&amp;class_id=1&amp;cata_id=7&amp;article_id=390
 
我有一个完整的版本已在Delphi下通过各种测试。其实我从网上搜得很多算法都不是太完
整,不是插入结点有问题便是删除结点有问题,其中删除结点有问题的问题更多一些。希望
你能够看得懂源码并能够按你自己的要求修改。我实现的是双平衡,你只需要单平衡,更简
单。
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=1920407
 
多人接受答案了。
 
顶部