zoukankan      html  css  js  c++  java
  • 线索二叉树的构建

    package org.lyk.main;
    
    import java.util.Scanner;
    
    enum NodeType
    {
        child, thread;
    }
    
    class Node<T extends Comparable<T>>
    {
        private static Node<String> pre = null;
        Node<T> left;
        NodeType leftType = NodeType.child;
        T data;
        Node<T> right;
        NodeType rightType = NodeType.child;
    
        @Override
        public String toString()
        {
            StringBuffer info = new StringBuffer();
            info.append("leftType:" + this.leftType + " ");
            if (this.leftType.equals(NodeType.child))
            {
                info.append("leftChild:" + this.left.data  + " ");
            }
            else if(this.leftType.equals(NodeType.thread))
            {
                info.append("pre data:" + this.left.data  + " ") ;
            }
            
            info.append(" data: " + this.data + " ");
            info.append("rightType:" + rightType + " ");
            
            
            if(this.rightType.equals(NodeType.child))
            {
                    info.append(" rightChild:" + this.right.data  + " ");
            }
            else if(this.rightType.equals(NodeType.thread))
            {
                info.append("pre Child:" + this.right.data + " ");
            }
    
            return info.toString();
        }
    
        
    
        void add(T data)
        {
            if (data.compareTo(this.data) < 0)
            {
                if (null != this.left)
                    this.left.add(data);
                else
                {
                    this.left = new Node<T>();
                    this.left.data = data;
                }
            } else
            {
                if (null != this.right)
                    this.right.add(data);
                else
                {
                    this.right = new Node<T>();
                    this.right.data = data;
                }
            }
        }
    
        public static <T extends Comparable<T>> Node<T> add_first(Node<T> node, Scanner scanner)
        {
            String data = input(scanner);
            if ("*".equals(data))
            {
                node = null;
            } else
            {
                node = new Node<T>();
                node.data = (T) data;
                node.left = Node.add_first(node.left, scanner);
                node.right = Node.add_first(node.right, scanner);
            }
            return node;
        }
    
        private static String input(Scanner scanner)
        {
            scanner.useDelimiter("
    ");
            System.out.print("请输入:");
            String data = (String) scanner.next();
            return data;
        }
    
        void get_first()
        {
            System.out.print(this.data);
            if (null != this.left)
            {
                this.left.get_first();
            }
            if (null != this.right)
            {
                this.right.get_first();
            }
        }
    
        public void get_middle()
        {
            if (null != this.left)
                this.left.get_middle();
            System.out.println(this);
            if (null != this.right)
                this.right.get_middle();
        }
        
        public void getMiddleDetails()
        {
            if (null != this.left && this.leftType.equals(NodeType.child))
                this.left.getMiddleDetails();
            System.out.println(this);
            if (null != this.right && this.rightType.equals(NodeType.child))
                this.right.getMiddleDetails();
        }
    
        public void get_last()
        {
            if (null != this.left)
                this.left.get_last();
            if (null != this.right)
                this.right.get_last();
            System.out.print(this.data);
        }
    
        public void setThread(Node<T> pre)
        {
            
            if (null != this.left)
            {
                this.left.setThread(this);
            }
             
            if (null == this.left)
            {
                this.leftType = NodeType.thread;
                this.left = pre;
            }
            if (null == this.right)
            {
                this.rightType = NodeType.thread;
                this.right = pre;
            } 
            
            if (null != this.right && this.rightType.equals(NodeType.child))
                this.right.setThread(this);
        }
    }
    
    class Tree<T extends Comparable<T>>
    {
        Node<T> root;
    
        public void add_first(Scanner scanner)
        {
            this.root = Node.add_first(this.root, scanner);
        }
    
        public void get_first()
        {
            if (null != root)
            {
                root.get_first();
            }
        }
    
        public void getMiddle()
        {
            if (null != this.root)
                this.root.get_middle();
        }
        
        public void getMiddleDetails()
        {
            if (null != this.root)
                this.root.getMiddleDetails();
        }
    
        public void get_last()
        {
            if (null != this.root)
                this.root.get_last();
        }
    
        public void setThread()
        {
            if (null != this.root)
            {
                this.root.setThread(null);
            }
        }
    }
    
    public class Main
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            Tree<String> bt = new Tree<>();
            bt.add_first(scanner);
            System.out.println("add successed!!");
            bt.setThread();
            System.out.println("set successed!!!");
            System.out.println("中序输出:");
            bt.getMiddleDetails();
            System.out.println("///~ main done");
        }
    }
  • 相关阅读:
    2013年工作中用到的10个命令:11-20
    2013年工作中用到的10个命令:11-20
    2013年工作中用到的10个命令:1-10
    OpenJDK源码研究笔记(十):枚举的高级用法,枚举实现接口,竟是别有洞天
    OpenJDK源码研究笔记(十):枚举的高级用法,枚举实现接口,竟是别有洞天
    Java实现 LeetCode 240 搜索二维矩阵 II(二)
    Java实现 LeetCode 239 滑动窗口最大值
    Java实现 LeetCode 239 滑动窗口最大值
    Java实现 LeetCode 239 滑动窗口最大值
    Java实现 LeetCode 238 除自身以外数组的乘积
  • 原文地址:https://www.cnblogs.com/kuillldan/p/6043492.html
Copyright © 2011-2022 走看看