public HeroNode preOrderSearch(int i) { System.out.println("进入前序查找"); //比较当前结点是不是 if (this.id == i) return this; //1.则判断当前结点的左子节点是否为空,如果不为空,则递归前序查找 //2.如果左递归前序查找,找到结点,则返回 HeroNode node = null; if (this.left != null) { node = this.left.preOrderSearch(i); } if (node != null) return node; //2.当前的结点的右子节点是否为空,如果不空,则继续向右递归前序查找 if (this.right != null) { node = this.right.preOrderSearch(i); } return node; }
public HeroNode infixOrderSearch(int i) { HeroNode node = null; if (this.left != null) { node = this.left.infixOrderSearch(i); } System.out.println("进入中序查找"); if (node != null) return node; //比较当前结点是不是 if (this.id == i) return this; if (this.right != null) { node = this.right.infixOrderSearch(i); } return node; }
public HeroNode postOrderSearch(int i) { HeroNode node = null; if (this.left != null) { node = this.left.postOrderSearch(i); } if (node != null) return node; if (this.right != null) { node = this.right.postOrderSearch(i); } if (node != null) return node; System.out.println("进入后序查找"); if (this.id == i) return this; return node; }
public HeroNode preOrderSearch1(int i) { System.out.println("前序非递归查找~~"); Stack<HeroNode> stack = new Stack<>(); stack.push(this); while (!stack.isEmpty()) { HeroNode node = stack.pop(); if (node.id == i) return node; if (node.right != null) stack.push(node.right); if (node.left != null) stack.push(node.left); } return null; }
public HeroNode infixOrderSearch1(int i) { System.out.println("中序非递归查找~~"); Stack<HeroNode> stack = new Stack<>(); HeroNode node = this; while (!stack.isEmpty() || node != null) { if (node != null) { stack.push(node); node = node.left; } else { node = stack.pop(); if (node.id == i) return node; node = node.right; } } return null; }
public HeroNode postOrderSearch1(int i) { System.out.println("后序非递归查找~~"); Stack<HeroNode> s1 = new Stack<>(); Stack<HeroNode> s2 = new Stack<>(); HeroNode node = this; s1.push(node); while (!s1.isEmpty()){ node=s1.pop(); s2.push(node); if(node.left!=null){ s1.push(node.left); } if(node.right!=null){ s1.push(node.right); } } while (!s2.isEmpty()){ node=s2.pop(); if(node.id==i) return node; } return null; }
public HeroNode postOrderSearch2(int i) { System.out.println("后序非递归查找~~"); Stack<HeroNode> s1 = new Stack<>(); HeroNode node = this; HeroNode lasrnode = null; while (node!=null){ s1.push(node); node=node.left; } while (!s1.isEmpty()){ node=s1.pop(); if(node.right!=null&& node.right!=lasrnode){ s1.push(node); node=node.right; while (node!=null){ s1.push(node); node=node.left; } }else { if(node.id==i) return node; lasrnode=node; } } return null; } }
public void delNode(int i) { if(root==null) return; if(root.id==i) root=null; else root.delNode(i); } public void delNode(int i) { if(this.left!=null&&this.left.id==i){ this.left=null; return; } if(this.right!=null&&this.right.id==i){ this.right=null; return; } if(this.left!=null) this.left.delNode(i); if(this.right!=null) this.right.delNode(i); }
public HeroNode delNode1(HeroNode root,int i) { if (root == null) return root; if (root.id == i) { if (root.right == null) { // 这里第二次操作目标值:最终删除的作用 return root.left; } HeroNode cur = root.right; while (cur.left!=null) { cur = cur.left; } HeroNode node = new HeroNode(root.id, root.name); root.id=cur.id; root.name=cur.name; cur.id=node.id; cur.name=node.name;// 这里第一次操作目标值:交换目标值其右子树最左面节点。仅仅是值替换,引用没有交换 } root.left = delNode1(root.left, i); root.right = delNode1(root.right, i); return root; }