package com.example.helloworld;
public class Tree2 {
private static int LEFT = 0;
private static int RIGHT = 1;
Node2 mRoot;
public Tree2(){
mRoot = new Node2();
mRoot.index = 0;
}
public boolean addNode(int nodeIndex, int direction, Node2 node){
Node2 currentNode = searchNode(nodeIndex);
if(currentNode == null){
return false;
}
node.parent = currentNode;
if(direction == LEFT) {
currentNode.lChild = node;
}
if(direction == RIGHT){
currentNode.rChild = node;
}
return true;
}
public Node2 searchNode(int nodeIndex) {
return mRoot.searchNode(nodeIndex);
}
public boolean deleteNode(int nodeIndex) {
Node2 node = searchNode(nodeIndex);
if(node == null){
return false;
}
node.deleteNode();
return true;
}
public void preOrderTraveser(){
mRoot.preOrderTraveser();
}
public void inOrderTraveser(){
mRoot.inOrderTraveser();
}
public void postOrderTraveser(){
mRoot.postOrderTraveser();
}
}
class Node2 {
public int val;
public int index;
public Node2 parent;
public Node2 lChild;
public Node2 rChild;
public Node2(){}
public Node2(int index, int val){
this.index = index;
this.val = val;
this.parent = null;
this.lChild = null;
this.rChild = null;
}
public Node2 searchNode(int nodeIndex){
if(this.index == nodeIndex){ return this;}
Node2 node;
if(this.lChild != null){
node = this.lChild.searchNode(nodeIndex);
if(node != null) {
return node;
}
}
if(this.rChild != null){
node = this.rChild.searchNode(nodeIndex);
if(node != null) {
return node;
}
}
return null;
}
public boolean deleteNode(){
if(this.lChild != null){
this.lChild.deleteNode();
}
if(this.rChild != null) {
this.rChild.deleteNode();
}
if(this.parent != null){
if(this.parent.lChild.index == this.index){
this.parent.lChild = null;
}
if(this.parent.rChild.index == this.index){
this.parent.rChild = null;
}
}
return true;
}
public void preOrderTraveser(){
System.out.println(this.index + " " + this.val);
if(this.lChild != null){
this.lChild.preOrderTraveser();
}
if(this.rChild != null){
this.rChild.preOrderTraveser();
}
}
public void inOrderTraveser(){
if(this.lChild != null){
this.lChild.inOrderTraveser();
}
System.out.println(this.index + " " + this.val);
if(this.rChild != null){
this.rChild.inOrderTraveser();
}
}
public void postOrderTraveser(){
if(this.lChild != null){
this.lChild.postOrderTraveser();
}
if(this.rChild != null){
this.rChild.postOrderTraveser();
}
System.out.println(this.index + " " + this.val);
}
}
public class Demo {
public static void main(String[] args) {
// int[] a = new int[]{-2,-3,-3,7,-3,0,5,0,-8,-4,-1,2};
// longestConsecutive(a);
// int root = 3;
// BinaryTree bTree = new BinaryTree(10, 3);
// bTree.addNode(0, 0, 5);
// bTree.addNode(0, 1, 8);
//
// bTree.addNode(1, 0, 2);
// bTree.addNode(1, 1, 6);
//
// bTree.addNode(2, 0, 9);
// bTree.addNode(2, 1, 7);
// bTree.treeTraverse();
// System.out.println(bTree.searchNode(2));
// System.out.println(bTree.deleteNode(6));
// bTree.treeTraverse();
Node2 node1 = new Node2(1, 5);
Node2 node2 = new Node2(2,8);
Node2 node3 = new Node2(3,2);
Node2 node4 = new Node2(4, 6);
Node2 node5 = new Node2(5, 9);
Node2 node6 = new Node2(6, 7);
Tree2 lTree = new Tree2();
lTree.addNode(0, 0, node1);
lTree.addNode(0, 1, node2);
lTree.addNode(1, 0, node3);
lTree.addNode(1, 1, node4);
lTree.addNode(2, 0, node5);
lTree.addNode(2, 1, node6);
// lTree.preOrderTraveser();
// lTree.inOrderTraveser();
lTree.postOrderTraveser();
}
}
打开App,阅读手记