Preorder search:
Thinking analysis:
- First judge whether the no of the current node is equal to the searched one, if it is equal, then return to the current node.
- If they are not equal, judge whether the left child node of the current node is empty, and if it is not empty, search recursively before order.
- Left recursive pre-order search, if the node is found, return, otherwise continue to judge whether the right child node of the current node is empty, if not, continue to right recursive pre-order search.
//定义一个二叉树
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;
}
}
Mid-order search:
Thinking analysis:
- Determine whether the left child node of the current node is empty, if not, search recursively.
- Return if found, if not found
- Compare with the current node, if not, continue with the right recursive middle-order search.
- If right recursive middle order search, it will return if found, otherwise it will return 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;
}
}
Post-order traversal:
Thinking analysis:
- Determine whether the left child node of the current node is empty, if it is not empty, then search recursively
- If found, return, if not found, judge whether the right child node of the current node is empty
- If it is not empty, right recursively performs a post-order search, and if found, it returns.
- Compare with the current node, if yes, return otherwise, return 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;
}
}