}
public void show() {
//calls the show method of the AnyClass
System.out.print(anyClass.show());
}
}
下面是一个简单的类,它遍历,添加和搜索特定的节点值:
public class BinTree {
BNode theBTRootNode;
public BinTree() // constructor
{
theBTRootNode = null;
}
// ------------------ Addition of the node to the BST-------------------
protected BNode insertAB(BNode theRootNode, BNode myNewNode) {
if (theRootNode == null) {
theRootNode = myNewNode;
//checks if the username is smaller than
//the root object, if smaller appends to the left
} else if (myNewNode.anyClass.surname.compareTo(
theRootNode.anyClass.surname) < 0) {
theRootNode.leftBNode = insertAB(theRootNode.leftBNode, myNewNode);
} else {
// else if bigger appends to the right
theRootNode.rightBNode =
insertAB(theRootNode.rightBNode, myNewNode);
}
return theRootNode;
}
public void insertBST(AnyClass anyClass) {
BNode anyClassBTNode = new BNode(anyClass);
//calls insert above
theBTRootNode = insertAB(theBTRootNode, anyClassBTNode);
}
// ------------------ InOrder traversal-------------------
protected void inorder(BNode theRootNode) {
if (theRootNode != null) {
inorder(theRootNode.leftBNode);
theRootNode.show();
inorder(theRootNode.rightBNode);
}
}
//calls the method to do in order
public void inorderBS
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
T() {
inorder(theBTRootNode);
}
// ----- Search for key name and returns ref.
// to BNode or null if not found--------
protected BNode search(BNode theRootNode, String keyName) {
//if the root is null returns null
if (theRootNode == null) {
return null;
} else {
//checks if they are equal
if (keyName.compareTo(theRootNode.anyClass.surname) == 0) {
return theRootNode;
//checks id the key is smaller than the current
//record if smaller traverses to the left
} else if (keyName.compareTo(theRootNode.anyClass.surname) < 0) {
return search(theRootNode.leftBNode, keyName);
} else {
// if bigger traverses to the left
return search(theRootNode.rightBNode, keyName);
}
}
}
//returns null if no result else returns
//the AnyClass object matched with the keyName
public AnyClass searchBST(String keyName) {
BNode temp = search(theBTRootNode, keyName);
if (temp == null) {
//noresults found
return null;
} else {
//result found
return temp.anyClass;
}
}
}
兴趣点
在遍历列表时,这应该可以帮助您更快地实现应用程序。
您可以将此方法添加到BinTree
类对象中,以将列表转移到二叉树中:
public void populateBinTree(List theList) {
//clearing the root as not to append,
//if you want to append just remove the below line
theBTRootNode = null;
//keeps looping untill reaches the end of the list
for(int i = 0;i < theList.size();i++)
Node temporaryNode = null;
//inserts in the BST
insertBST((AnyClass)theList.get(i));