Java BST数据结构——类项目——访问嵌套类
问题是:对“person”对象应用二叉搜索树不需要int值。person对象由名称和权重组成。树将组织和搜索的person对象的值是person的“名称”(字符串) 这是一个(叶)树节点:Java BST数据结构——类项目——访问嵌套类,java,search,binary-search-tree,Java,Search,Binary Search Tree,问题是:对“person”对象应用二叉搜索树不需要int值。person对象由名称和权重组成。树将组织和搜索的person对象的值是person的“名称”(字符串) 这是一个(叶)树节点: public class Node { private Node leftChild; private Node rightChild; private Node parent; private Person person; private int height; pu
public class Node {
private Node leftChild;
private Node rightChild;
private Node parent;
private Person person;
private int height;
public Node(Node p, Node l, Node r, Person paul, int h) {
leftChild = l;
rightChild = r;
person = paul;
height = h;
parent = p;
}
public Node(Person paul, Node p) {
this(p, null, null, paul, 0);
}
public Node (Person paul) {
this(null, null, null, paul, 0);
}
public boolean isBalanced() {
if(leftChild == null && rightChild == null)
return true;
else if (leftChild == null)
return rightChild.getHeight() == 0;
else if (rightChild == null)
return leftChild.getHeight() == 0;
return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
}
public void setParent(Person parent) {
parent = new Person( parent.getName(), parent.getWeight());
}
public Node getParent() {
return parent;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getLeftChild() {
return leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getRightChild() {
return rightChild;
}
public void setPerson(Person person) {
Person = new Person(person.getName(), person.getWeight());
}
public Person getPerson() {
return new Person(person.getName(), person.getWeight());
}
public void setHeight() {
if(leftChild == null && rightChild == null)
height = 0;
else if (leftChild == null)
height = rightChild.getHeight() + 1;
else if (rightChild == null)
height = leftChild.getHeight() + 1;
else
height= leftChild.getHeight() >= rightChild.getHeight()?
leftChild.getHeight() + 1: rightChild.getHeight() +1;
}
public int getHeight() {
setHeight();
return height;
}
}
公共类节点{
私有节点leftChild;
私有节点rightChild;
私有节点父节点;
私人;
私人内部高度;
公共节点(节点p、节点l、节点r、人保罗、整数h){
leftChild=l;
rightChild=r;
人=保罗;
高度=h;
父母=p;
}
公共节点(个人保罗,节点p){
这(p,null,null,paul,0);
}
公共节点(个人){
这个(null,null,null,paul,0);
}
公共布尔值是平衡的(){
if(leftChild==null&&rightChild==null)
返回true;
else if(leftChild==null)
返回rightChild.getHeight()==0;
else if(rightChild==null)
返回leftChild.getHeight()==0;
返回Math.abs(leftChild.getHeight()-righchild.getHeight())<2;
}
公共无效集合父对象(个人父对象){
parent=新用户(parent.getName(),parent.getWeight());
}
公共节点getParent(){
返回父母;
}
公共void setLeftChild(节点leftChild){
this.leftChild=leftChild;
}
公共节点getLeftChild(){
返回leftChild;
}
公共无效setRightChild(节点rightChild){
this.rightChild=rightChild;
}
公共节点getRightChild(){
还权子;
}
公众人士{
Person=新的Person(Person.getName(),Person.getWeight());
}
公众人物{
返回新的Person(Person.getName(),Person.getWeight());
}
公共空间设置高度(){
if(leftChild==null&&rightChild==null)
高度=0;
else if(leftChild==null)
高度=rightChild.getHeight()+1;
else if(rightChild==null)
height=leftChild.getHeight()+1;
其他的
高度=leftChild.getHeight()>=rightChild.getHeight()?
leftChild.getHeight()+1:rightChild.getHeight()+1;
}
公共整数getHeight(){
设置高度();
返回高度;
}
}
以下是BinarySearchTree:
public class BinarySearchTree {
private Node root;
public BinarySearchTree(Node root) {
this.root = root;
}
public BinarySearchTree() {
this.root = (Node)null;
}
void setRoot(Node r) {
root = r;
}
Node getRoot() {
return root;
}
public Node findParent(Person person, Node node) {
if (node.getPerson() == person)//Error in code here
return (Node)null; // root itself
else if(node.getLeftChild() != null && node.getLeftChild().getPerson() == person)
return node;
else if(node.getRightChild() != null && node.getRightChild().getPerson() == person)
return node;
else if (node.getPerson().getName().compareTo(" a ") > 0 ){
return findParent(person, node.getLeftChild());}
else
return findParent(person, node.getRightChild());
}
public Node insertNode(Person person) {
return insertNode(person, null, null);
}
public Node insertNode(Person person, Node node, Node parent) {
if(node == (Node)null)
node = new Node(parent, null, null, person, 0);
else if (person.getName().compareTo(node.getPerson().getName()) < 0)
node.setLeftChild(insertNode(person,node.getLeftChild(), node));
else if (person.getName().compareTo(node.getPerson().getName()) > 0)
node.setRightChild(insertNode(person, node.getRightChild(), node));
node.setHeight();
return node;
}
/* in-order traversal for showing inside of tree */
public void traverseInOrder(Node node) {
if(node.getLeftChild() != (Node)null)
traverseInOrder(node.getLeftChild());
System.out.print("Value: " + '\n' + node.getPerson().toString() + ", Height: " +
node.getHeight() + ", Parent: ");
Node n = findParent(node.getPerson(), getRoot());
if(n == (Node)null)
System.out.println("root");
else
System.out.println(n.getPerson().toString() + "");
if(node.getRightChild()!= (Node)null)
traverseInOrder(node.getRightChild());
}
}
public Node(Person paul, Node p) {
this(p, null, null, paul, 0);
}
public Node (Person paul) {
this(null, null, null, paul, 0);
}
public boolean isBalanced() {
if(leftChild == null && rightChild == null)
return true;
else if (leftChild == null)
return rightChild.getHeight() == 0;
else if (rightChild == null)
return leftChild.getHeight() == 0;
return Math.abs(leftChild.getHeight()- rightChild.getHeight()) < 2;
}
public void setParent(Person parent) {
parent = new Person( parent.getName(), parent.getWeight());
//or is the parent supposed to be a null pointer ????
}
public Node getParent() {
return parent;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getLeftChild() {
return leftChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node getRightChild() {
return rightChild;
}
public void setPerson(Person Person) {
Person = new Person(person.getName(), person.getWeight());
}
public Person getPerson() {
return new Person(person.getName(), person.getWeight());
}
public void setHeight() {
if(leftChild == null && rightChild == null)
height = 0;
else if (leftChild == null)
height = rightChild.getHeight() + 1;
else if (rightChild == null)
height = leftChild.getHeight() + 1;
else
height= leftChild.getHeight() >= rightChild.getHeight()?
leftChild.getHeight() + 1: rightChild.getHeight() +1;
}
public int getHeight() {
setHeight();
return height;
}
}
公共类二进制搜索树{
私有节点根;
公共二进制搜索树(节点根){
this.root=根;
}
公共二进制搜索树(){
this.root=(Node)null;
}
void setRoot(节点r){
根=r;
}
节点getRoot(){
返回根;
}
公共节点findParent(个人、节点节点){
if(node.getPerson()==person)//此处的代码中有错误
返回(节点)null;//根本身
else if(node.getLeftChild()!=null&&node.getLeftChild().getPerson()==person)
返回节点;
else if(node.getRightChild()!=null&&node.getRightChild().getPerson()==person)
返回节点;
else if(node.getPerson().getName().compareTo(“a”)>0){
返回findParent(person,node.getLeftChild());}
其他的
返回findParent(person,node.getRightChild());
}
公共节点插入节点(个人){
返回insertNode(person,null,null);
}
公共节点插入节点(个人、节点节点、节点父节点){
如果(节点==(节点)为空)
节点=新节点(父节点,null,null,person,0);
else if(person.getName().compareTo(node.getPerson().getName())<0)
setLeftChild(insertNode(person,node.getLeftChild(),node));
如果(person.getName().compareTo(node.getPerson().getName())>0,则为else
setRightChild(insertNode(person,node.getRightChild(),node));
node.setHeight();
返回节点;
}
/*以显示树内部的顺序遍历*/
公共void遍历器(节点){
if(node.getLeftChild()!=(node)null)
TraverseInNorder(node.getLeftChild());
System.out.print(“值:”+“\n”+node.getPerson().toString()+”,高度:+
node.getHeight()+,父节点:);
Node n=findParent(Node.getPerson(),getRoot());
如果(n==(节点)为空)
System.out.println(“根”);
其他的
System.out.println(n.getPerson().toString()+);
if(node.getRightChild()!=(node)null)
TraverseInNorder(node.getRightChild());
}
}
公共节点(个人保罗,节点p){
这(p,null,null,paul,0);
}
公共节点(个人){
这个(null,null,null,paul,0);
}
公共布尔值是平衡的(){
if(leftChild==null&&rightChild==null)
返回true;
else if(leftChild==null)
返回rightChild.getHeight()==0;
else if(rightChild==null)
返回leftChild.getHeight()==0;
返回Math.abs(leftChild.getHeight()-righchild.getHeight())<2;
}
公共无效集合父对象(个人父对象){
parent=新用户(parent.getName(),parent.getWeight());
//或者父对象应该是空指针????
}
公共节点getParent(){
返回父母;
}
公共void setLeftChild(节点leftChild){
this.leftChild=leftChild;
}
公共节点getLeftChild(){
返回leftChild;
}
公共无效setRightChild(节点rightChild){
this.rightChild=rightChild;
}
公共节点getRightChild(){
还权子;
}
公众人士{
Person=新的Person(Person.getName(),Person.getWeight());
}
公众人物{
返回新的Person(Person.getName(),Person.getWeight());
}
公共空间设置高度(){
if(leftChild==null&&rightChild==null)
高度=0;
else if(leftChild==null)
高度=rightChild.getHeight()+1;
else if(rightChild==null)
height=leftChild.getHeight()+1;
其他的
高度=leftChild.getHeight()>=rightChild.getHeight()?
leftChild.getHeight()+1:rightChild.getHeight()+1;
}
公共整数getHeight(){
设置高度();
返回高度;
}
}
使用
if (node.getPerson() == person)
if (node.getPerson().getName().equals(person.getName()))