事前注文検索:
思考分析:
- まず、現在のノードの番号が検索されたノードと等しいかどうかを判断し、等しい場合は、現在のノードに戻ります。
- それらが等しくない場合は、現在のノードの左側の子ノードが空であるかどうかを判断し、空でない場合は、順序付けの前に再帰的に検索します。
- 左再帰の先行予約検索は、ノードが見つかった場合は戻り、そうでない場合は現在のノードの右の子ノードが空かどうかを判断し続け、空でない場合は右再帰の先行予約検索を続けます。
//定义一个二叉树
public class binaryTreeDemo {
public static void main(String[] args) {
//先创建一个二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroNode root = new HeroNode(1,"1");
HeroNode node2 = new HeroNode(2,"2");
HeroNode node3 = new HeroNode(3,"3");
HeroNode node4 = new HeroNode(4,"4");
HeroNode node5 = new HeroNode(5,"5");
//说明:手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
//前序遍历
System.out.println("前序查找-------");
binaryTree.setRoot(root);
Node resNode = binaryTree.preOrderSearch(4);
if(resNode!=null) {
System.out.printf("找到了,信息为no=%d name=%s",resNode.getNo(),resNode.getName());;
}else {
System.out.println("没有找到");
}
}
}
class BinaryTree{
//根节点
private Node root;
//一个set方法
public void setRoot(Node root) {
this.root = root;
}
//前序遍历
public Node preOrderSearch(int no) {
if(this.root != null) {
return this.root.preOrderSearch(no);
}else {
return null;
}
}
}
//创建
class Node{
private int no;
private String name;
private Node left;//默认null
private Node right;//默认null
public Node(int no,String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node [no=" + no + ", name=" + name + "]";
}
//no 查找no
//如果找到就返回该Node,如果没有找到就返回null
public Node preOrderSearch(int no) {
//比较当前节点是不是
if(this.no == no) {
return this;
}
//判断当前结点的左子结点是不是为空,如果不为空则递归前序
//如果左递归前序查找找到结点,则返回
Node resNode = null;
if(this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if(resNode != null) {
//说明左子树找到了
return resNode;
}
//否则继续判断当前结点的右子结点是否为空,如果不为空,则继续向右递归前序查找
if(this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
}
中間検索:
思考分析:
- 現在のノードの左側の子ノードが空であるかどうかを判別し、空でない場合は、再帰的に検索します。
- 見つからない場合は戻る
- 現在のノードと比較します。そうでない場合は、適切な再帰的中次検索を続行します。
- 右再帰的な中位検索の場合、見つかった場合は戻り、それ以外の場合はnullを返します。
public class binaryTreeDemo {
public static void main(String[] args) {
//先创建一个二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroNode root = new HeroNode(1,"1");
HeroNode node2 = new HeroNode(2,"2");
HeroNode node3 = new HeroNode(3,"3");
HeroNode node4 = new HeroNode(4,"4");
HeroNode node5 = new HeroNode(5,"5");
//说明:手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
//中序遍历
System.out.println("中序查找-------");
binaryTree.setRoot(root);
Node resNode = binaryTree.centerOrderSearch(4);
if(resNode!=null) {
System.out.printf("找到了,信息为no=%d name=%s",resNode.getNo(),resNode.getName());;
}else {
System.out.println("没有找到");
}
}
}
class BinaryTree{
//根节点
private Node root;
//一个set方法
public void setRoot(Node root) {
this.root = root;
}
//中序遍历
public Node centerOrderSearch(int no) {
if(this.root != null) {
return this.root.centerOrderSearch(no);
}else {
return null;
}
}
}
//创建
class Node{
private int no;
private String name;
private Node left;//默认null
private Node right;//默认null
public Node(int no,String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node [no=" + no + ", name=" + name + "]";
}
//no 查找no
//如果找到就返回该Node,如果没有找到就返回null
public Node centerOrderSearch(int no) {
//判断当前结点的左子结点是不是为空,如果不为空则递归前序
//如果左递归前序查找找到结点,则返回
Node resNode = null;
if(this.left != null) {
resNode = this.left.centerOrderSearch(no);
}
if(resNode != null) {
//说明左子树找到了
return resNode;
}
//比较当前节点是不是
if(this.no == no) {
return this;
}
//否则继续判断当前结点的右子结点是否为空,如果不为空,则继续向右递归前序查找
if(this.right != null) {
resNode = this.right.centerOrderSearch(no);
}
return resNode;
}
}
注文後のトラバーサル:
思考分析:
- 現在のノードの左側の子ノードが空であるかどうかを判別し、空でない場合は、再帰的に検索します
- 見つかった場合は戻り、見つからなかった場合は、現在のノードの右側の子ノードが空かどうかを判断します
- 空でない場合、rightは再帰的にポストオーダー検索を実行し、見つかった場合は戻ります。
- 現在のノードと比較します。はいの場合は戻り、そうでない場合はnullを返します。
public class binaryTreeDemo {
public static void main(String[] args) {
//先创建一个二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroNode root = new HeroNode(1,"1");
HeroNode node2 = new HeroNode(2,"2");
HeroNode node3 = new HeroNode(3,"3");
HeroNode node4 = new HeroNode(4,"4");
HeroNode node5 = new HeroNode(5,"5");
//说明:手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
//中序遍历
System.out.println("后序查找方式-------");
binaryTree.setRoot(root);
Node resNode = binaryTree.postOrderSearch(4);
if(resNode!=null) {
System.out.printf("找到了,信息为no=%d name=%s",resNode.getNo(),resNode.getName());;
}else {
System.out.println("没有找到");
}
}
}
class BinaryTree{
//根节点
private Node root;
//一个set方法
public void setRoot(Node root) {
this.root = root;
}
//中序遍历
public Node postOrderSearch(int no) {
if(this.root != null) {
return this.root.postOrderSearch(no);
}else {
return null;
}
}
}
class Node{
private int no;
private String name;
private Node left;//默认null
private Node right;//默认null
public Node(int no,String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node [no=" + no + ", name=" + name + "]";
}
//no 查找no
//如果找到就返回该Node,如果没有找到就返回null
public Node postOrderSearch(int no) {
//判断当前结点的左子结点是不是为空,如果不为空则递归前序
//如果左递归前序查找找到结点,则返回
Node resNode = null;
if(this.left != null) {
resNode = this.left.postOrderSearch(no);
}
if(resNode != null) {
//说明左子树找到了
return resNode;
}
//否则继续判断当前结点的右子结点是否为空,如果不为空,则继续向右递归前序查找
if(this.right != null) {
resNode = this.right.postOrderSearch(no);
}
if(resNode != null) {
//说明右子树找到了
return resNode;
}
//比较当前节点是不是
if(this.no == no) {
return this;
}
return resNode;
}
}