【经典回放】多种语言系列数据结构算法:二叉树(JavaScript版)

1 二叉树类的设计以及二叉树遍历

要完成二叉树的类设计,最好把链表下的Node.js复制过来,相比链表的结点,二叉树仅仅是多了一个结点指针而已。略加修改后,就是:

function TNODE(DATA)
{
this.Data=DATA;
this.lChild=null;
this.rChild=null;
this.SetLChild=function (LCHILD)
	{
	this.lChild=LCHILD;
	}
this.GetLChild=function ()
	{
	return this.lChild;
	}
this.SetRChild=function (RCHILD)
	{
	this.rChild=RCHILD;
	}
this.GetRChild=function ()
	{
	return this.rChild;
	}
this.GetData=function ()
	{
	return this.Data;
	}
}

在这个类中,用:

SetLChild()、GetLChild()  两个方法来设置、读出该结点的左孩子;

SetRChild()、GetRChild()  两个方法来设置、读出该结点的右孩子;

SetData()、GetData()     两个方法来设置、读出该结点的数值。

要测试这个类,也很容易,注意再次复制二叉树下的a1.html,略加修改就是:

<html>
	<head&gt

猜你喜欢

转载自blog.csdn.net/lucky51222/article/details/106732822
今日推荐