關於我自己

我的相片
Welcome to discuss about : Chinese Traditional Medicine and Acupuncture Please send me the email: tccnchsu@gmail.com Chih-Yu Hsu

最新消息

總瀏覽量

2009年5月7日 星期四

第十一週

第十一週 二元排序樹
------------------------------------------------------------------------------
class BinarySortTree{
protected TreeNode root=null;

public void insert(int insertValue){


TreeNode newNode=new TreeNode();

newNode.info=insertValue;
newNode.llink=null;
newNode.rlink=null;
//System.out.print(newNode.info);

TreeNode current=new TreeNode();

if (root==null)
{
root=newNode;
}
else
{
current=root;
current.displayNode();
if (current.info>insertValue)
current.llink = newNode;
else
current.rlink = newNode;

}


TreeNode trailCurrent=new TreeNode();
if (trailCurrent.info>insertValue)
{

trailCurrent.llink = newNode;
trailCurrent.llink.displayNode();
}
else
trailCurrent.rlink = newNode;



}
public static void main(String[] args) {

BinarySortTree testNode=new BinarySortTree();
testNode.insert(17);
testNode.insert(6);
testNode.insert(23);
}


}

class TreeNode{
int info;
TreeNode llink;
TreeNode rlink;

public void displayNode() // display ourself
{
System.out.print("{");
System.out.print(info);
System.out.print("} ");
}

}

-------------------------------------------------------------------------------------

二元樹排序法是將鑑值依照輸入的順序建立二元樹,第一個鍵值為樹根,當輸入的值比樹根值大,則置入右子樹,
反之則置入左子樹,接著再利用中序走訪二元樹的節點一一走訪,即可得排序之鑑值.
其運作步驟如下:
1 第一筆資料鑑值視為樹根
2.其餘鍵值則個別與樹根比較,若比樹根值大,則置入右子樹,反之則置入左子樹.
3.當鍵值進入下一層子樹時,重複2步驟,直到無數值可用.
4.建好二元樹之後,利用中序走訪即可排序.



17,6, 23, 50, 40, 10, 15, 4, 20

17
6 23 (6<17 左, 23 >17 右)
10 50 (10<17, 左 , 10>6 右)
40
-------------------------------------------------------
17
6 23
10 50
15 40 (15<17 左, 15>6, 右, 15>10 右)

--------------------------------------------------------
17
6 23
4 10 20 50
15 40
--------------------------------------------------------------
二元樹以建立完成

--------------------------------------------------------------
在利用中序走訪

4, 6 , 10, 15, 17, 20, 23, 40, 50
排序完成,此為二元樹排序法(Brinary Tree Sorting)
ex
20,10, 50, 30, 40, 60, 15, 8, 12
http://users.cs.fiu.edu/~weiss/dsj3/code/

--------------------------------------------------------------


javax.swing.tree

Interface TreeNode

--------------------------------------------------------------

class Node
{
public int iData; // data item (key)
public double dData; // data item
public Node leftChild; // this node's left child
public Node rightChild; // this node's right child

public void displayNode() // display ourself
{
System.out.print('{');
System.out.print(iData);
System.out.print(", ");
System.out.print(dData);
System.out.print("} ");
}
} // end class Node

---------------------------------------------------------------
class TreeNode{
int info;
TreeNode llink;
TreeNode rlink;
}

6 則留言:

dragon 提到...

/*d958260 廖仁龍*/
class BinarySortTree{
protected TreeNode root=null;

public void insert(int insertValue){


TreeNode newNode=new TreeNode();

newNode.info=insertValue;
newNode.llink=null;
newNode.rlink=null;
//System.out.print(newNode.info);

TreeNode current=new TreeNode();

if (root==null)
{
root=newNode;
}
else
{
current=root;
if (current.info>insertValue)
current.llink = newNode;
else
current.rlink = newNode;

}


TreeNode trailCurrent=new TreeNode();
if (trailCurrent.info>insertValue)
{
trailCurrent.llink = newNode;
trailCurrent.llink.displayNode();
}
else
trailCurrent.rlink = newNode;
trailCurrent.rlink.displayNode();


}
public static void main(String[] args) {

BinarySortTree testNode=new BinarySortTree();
testNode.insert(17);
testNode.insert(6);
testNode.insert(23);
}


}

class TreeNode{
int info;
TreeNode llink;
TreeNode rlink;

public void displayNode() // display ourself
{
System.out.print("{");
System.out.print(info);
System.out.print("} ");
}

}

1 提到...

/*D9539108 陸靖文*/

class BinaryTreeSort
{
protected TreeNode root=null;

public void Insert(int InsertValue)
{
TreeNode newNode = new TreeNode();
newNode.info = InsertValue;
newNode.Rlink = null;
newNode.Llink = null;
//newNode.displayNode();

TreeNode oldNode = new TreeNode();

if (root==null)
{
root = newNode;

}
else
{
oldNode = root;

if (oldNode.info < InsertValue)
oldNode.Rlink = newNode;

else
oldNode.Llink = newNode;
}


TreeNode trailOldNode = new TreeNode();
if (trailOldNode.info < InsertValue)
{
trailOldNode.Rlink = newNode;

trailOldNode.Rlink.displayNode();
}
else
{
trailOldNode.Llink = newNode;

trailOldNode.Llink.displayNode();
}


}

public static void main(String[] args)
{

BinaryTreeSort testNode = new BinaryTreeSort();
testNode.Insert(17);
testNode.Insert(6);
testNode.Insert(23);
testNode.Insert(50);
testNode.Insert(40);
testNode.Insert(10);
testNode.Insert(15);
testNode.Insert(4);
testNode.Insert(20);

}

}


class TreeNode
{

int info;
TreeNode Llink;
TreeNode Rlink;

public void displayNode()
{


System.out.print("{" +info+ "}");


}

}

歐地 提到...

//第十一週作業 D9539125 黃培熏
class BinaryTreeSort
{
protected TreeNode root = null;
public void Insert(int InsertValue)
{

TreeNode NewNode = new TreeNode();
TreeNode OldNode = new TreeNode();
TreeNode TrailOldNode = new TreeNode();
NewNode.info = InsertValue;
NewNode.Rlink = null;
NewNode.Llink = null;
NewNode.DisplayNode();

if (root == null)
{
root=NewNode;
}
else
{
OldNode=root;

if (OldNode.info < InsertValue)
OldNode.Rlink = NewNode;
else
OldNode.Llink = NewNode;
}

if (TrailOldNode.info < NewNode.info)
TrailOldNode.Rlink=NewNode;
else
TrailOldNode.Llink=NewNode;

}

public static void main(String[] args)
{
BinaryTreeSort TestNode = new BinaryTreeSort();
TestNode.Insert(17);
TestNode.Insert(6);
TestNode.Insert(23);
TestNode.Insert(50);
TestNode.Insert(40);
TestNode.Insert(10);
TestNode.Insert(15);
TestNode.Insert(4);
TestNode.Insert(20);
}

class TreeNode
{
int info;
TreeNode Llink;
TreeNode Rlink;

public void DisplayNode()
{
System.out.print("{");
System.out.print(info);
System.out.print("}");
}
}
}

乃爸 提到...

//應數三甲 D9538864 徐晟哲
class BinaryTreeSort{
protected TreeNode root=null;

public void insert(int insertValue)
{
TreeNode newNode = new TreeNode();
newNode.info=insertValue ;
newNode.llink=null;
newNode.rlink=null;
//System.out.print(newNode.info);

TreeNode current = new TreeNode();
if(root==null)
{
root=newNode;

}

else
{
current=root;

if (current.info>insertValue)
{
current.llink = newNode;
//current.llink.displayNode();
}
else

current.rlink = newNode;


}

TreeNode trailCurrent = new TreeNode();
if(trailCurrent.info>insertValue)
{
trailCurrent.llink = newNode;
trailCurrent.llink.displayNode();
}
else
trailCurrent.rlink = newNode;
trailCurrent.rlink.displayNode();

}
public static void main(String[] args)
{
BinaryTreeSort TestNode = new BinaryTreeSort();
TestNode.insert(17);
TestNode.insert(6);
TestNode.insert(23);
TestNode.insert(50);
TestNode.insert(40);
TestNode.insert(10);
TestNode.insert(15);
TestNode.insert(4);
TestNode.insert(20);

}

}
class TreeNode
{
int info;
TreeNode llink;
TreeNode rlink;
public void displayNode() // display ourself
{
System.out.print("{");
System.out.print(info);
System.out.print("} ");
}

}

提到...

//D9476668 陳秋宇

class BinarySortTree{

protected TreeNode root=null;

public void insert(int insertValue){

TreeNode newNode=new TreeNode();
newNode.info=insertValue;
newNode.llink=null;
newNode.rlink=null;
//System.out.println(newNode.info);

TreeNode current=new TreeNode();

if (root==null)
{
root=newNode;
}

else
{
current=root;
current.displayNode();
if (current.info>insertValue)
current.llink = newNode;
else
current.rlink = newNode;

}




TreeNode trailCurrent=new TreeNode();
if (trailCurrent.info>insertValue)
{
trailCurrent.llink = newNode;
trailCurrent.llink.displayNode();
}
else
trailCurrent.rlink = newNode;
trailCurrent.rlink.displayNode();
}
public static void main(String[] args) {

BinarySortTree testNode=new BinarySortTree();
testNode.insert(17);
testNode.insert(6);
testNode.insert(23);

}

}

class TreeNode{
int info;
TreeNode llink;
TreeNode rlink;



public void displayNode() // display ourself
{
System.out.print("{");
System.out.print(info);
System.out.print("} ");

}
}

提到...

劉羽程
-----------------------------------
class BinarySortTree{
protected TreeNode root=null;

public void insert(int insertValue){


TreeNode newNode=new TreeNode();

newNode.info=insertValue;
newNode.llink=null;
newNode.rlink=null;
//System.out.print(newNode.info);

TreeNode current=new TreeNode();

if (root==null)
{
root=newNode;
}
else
{
current=root;
current.displayNode();
if (current.info>insertValue)
current.llink = newNode;
else
{
current.rlink = newNode;

}
}


TreeNode trailCurrent=new TreeNode();
if (trailCurrent.info>insertValue)
{

trailCurrent.llink = newNode;
trailCurrent.llink.displayNode();
}
else
{
trailCurrent.rlink = newNode;
trailCurrent.rlink.displayNode();
}

}
public static void main(String[] args) {

BinarySortTree testNode=new BinarySortTree();
testNode.insert(17);
testNode.insert(6);
testNode.insert(23);
}


}

class TreeNode{
int info;
TreeNode llink;
TreeNode rlink;

public void displayNode() // display ourself
{
System.out.print("{");
System.out.print(info);
System.out.print("} ");
}

}