zoukankan      html  css  js  c++  java
  • 二叉树java

    二叉树先定义一下数的每一个节点的结构

    class Node{
        public int data;        //里面有一个int数据
        public Node leftChild;  //左右节点左右孩子
        public Node rightChild;
    
    }
    public class TreeApp {
    
        private Node root; //根节点                        
    
        public TreeApp(){root = null;} 
    
        public void insert(int id) {   //插入
            Node newNode = new Node(); //定义新的要插入的节点
            newNode.data = id;         //新节点里面的数据
            if(root == null){root = newNode;}  //判断有没有根节点,没有根节点,这个新的为根节点
            else{
                Node current = root;
                Node parent;
                while(true)
                {
                    parent = current;
                    {
                        if(id < current.data)
                        {
                            current = current.leftChild;
                            if (current == null)
                            {
                                parent.leftChild = newNode;
                                return ;
                            }
                        }
                        else
                        {
                            current = current.rightChild;
                            if(current == null)
                            {
                                parent.rightChild = newNode;
                                return ;
                            }
    
                        }
                    }
    
                }
            }
        }
    
        //中序遍历
        public void inOrder(Node localRoot) {
            if(localRoot != null)
            {
                inOrder(localRoot.leftChild);
                System.out.print(localRoot.data + " ");
                inOrder(localRoot.rightChild);
            }
        }
    
        //前序遍历
        public void preOrder(Node localRoot) {
            if(localRoot != null)
            {
                System.out.print(localRoot.data + " ");
                preOrder(localRoot.leftChild);
                preOrder(localRoot.rightChild);
            }
        }
    
        //后序遍历
        public void postOrder(Node localRoot) {
            if(localRoot != null)
            {
                postOrder(localRoot.leftChild);
                postOrder(localRoot.rightChild);
                System.out.print(localRoot.data + " ");
            }
        }
    
        //得到要删除节点的后继节点
        public Node getSuccessor(Node delNode){
            Node successorParent = delNode;
            Node successor = delNode;
            Node current = delNode.rightChild;
            while(current != null)
            {
                successorParent = successor;
                successor = current;
                current = current.leftChild;
            }
            if(successor != delNode.rightChild)
            {
                successorParent.leftChild = successor.rightChild;
                successor.rightChild = delNode.rightChild;
            }
            return successor;
        }
    
        public boolean delete(int key)
        {
            Node current = root;
            Node parent = root;
            boolean isLeftChild = true;
    
            while(current.data != key)  //找到要删除的节点
            {
                parent = current;
                if(key < current.data){
                    isLeftChild = true;
                    current = current.leftChild;
                }
                else
                {
                    isLeftChild = false;
                    current = current.rightChild;
                }
                if(current == null)return false;    //没找到就返回错误
            }
    
            //如果要删除的节点没有子节点
            if (current.leftChild == null && current.rightChild == null)
            {
                if (current == root)
                {
                    root = null;
                }
                else if(isLeftChild)
                {
                    parent.leftChild = null;
                }
                else
                    parent.rightChild = null;
            }
    
            //如果要删除的节点没有左子树
            else if(current.leftChild == null)
            {
                if(current == root)
                {
                    root = current.rightChild;
                }
                else if(isLeftChild)
                {
                    parent.leftChild = current.rightChild;
                }
                else
                    parent.rightChild = current.rightChild;
            }
            //如果要删除的节点没有右子树
            else if(current.rightChild == null)
            {
                if(current == root)
                {
                    root = current.leftChild;
                }
                else if(isLeftChild)
                {
                    parent.leftChild = current.leftChild;
                }
                else
                    parent.rightChild = current.leftChild;
            }
    
            //要删除的节点有左右两个子树,删除的节点要替换成它的中序后继
            else
            {
                Node successor = getSuccessor(current);
                if(current == root)
                {
                    root = successor;
                }
                else if(isLeftChild)
                {
                    parent.leftChild = successor;
                }
                else
                    parent.rightChild = successor;
                successor.leftChild = current.leftChild;
            }
            return true;
        }
    
        public void displayTree()
        {
            Stack globalStack = new Stack();
            globalStack.push(root);
            int nBlanks = 32;
            boolean isRowEmpty = false;
            System.out.println("...........................................");
            while (isRowEmpty == false)
            {
                Stack localStack = new Stack();
                isRowEmpty = true;
    
                for(int j = 0; j < nBlanks;j++)
                {
                    System.out.print(' ');
                }
                while(globalStack.isEmpty()==false)
                {
                    Node temp = (Node)globalStack.pop();
                    if(temp != null) {
                        System.out.print(temp.data);
                        localStack.push(temp.leftChild);
                        localStack.push(temp.rightChild);
                        if (temp.leftChild != null || temp.rightChild != null)
                        {
                            isRowEmpty = false;
                        }
                    }
                    else
                    {
                        System.out.print("--");
                        localStack.push(null);
                        localStack.push(null);
                    }
                        for (int j = 0; j < nBlanks * 2 - 2; j++) {
                            System.out.print(' ');
                        }
                    }
                    System.out.println();
                    nBlanks /= 2;
                    while(localStack.isEmpty() == false)
                        globalStack.push(localStack.pop());
            }
        }
    
    
        public static void main(String[] args) {
            TreeApp myTree = new TreeApp();
            myTree.insert(10);
            myTree.insert(6);
            myTree.insert(14);
            myTree.insert(8);
            myTree.insert(12);
            myTree.insert(16);
            myTree.insert(3);
            myTree.insert(7);
            myTree.insert(15);
            myTree.insert(19);
            myTree.insert(20);
            myTree.insert(17);
    
            System.out.print("前序遍历:");
          myTree.preOrder(myTree.root);
            System.out.println();
            System.out.print("中序遍历:");
          myTree.inOrder(myTree.root);
            System.out.println();
            System.out.print("后序遍历:");
          myTree.postOrder(myTree.root);
            System.out.println();
    
            System.out.println("删除"+myTree.delete(8));
          myTree.displayTree();
        }
    
    }
  • 相关阅读:
    AOP的实现原理——动态代理
    反射
    代理模式
    sprig——jar包
    spring事务管理
    [c++基础]3/5原则--拷贝构造函数+拷贝赋值操作符
    [c++面试准备]--vector对象是如何增长的
    c++面试须知
    redhat--1
    kvm配置虚拟机[待整理]
  • 原文地址:https://www.cnblogs.com/lhh666/p/11797817.html
Copyright © 2011-2022 走看看