【Java数据结构】二叉树

简介:
核心:树中每个节点最多只能有两个子节点(t>=0&&t<=2)

下面实现的是一个二叉排序树(左孩子小于父节点,右孩子大于父节点)

1.插入节点
核心思想:
(1)如果不存在节点,则直接插入。
(2)从根节点开始查找一个相应的节点,即新节点的父节点,当父节点找到后,根据新节点的值来确定新节点是连接到左子节点还是右子节点。

2.查找节点
核心思想:
从根节点开始查找,如果查找到节点值比父节点值要小,则查找其左子树,否则查找其右子树,直到查找到为止,如果不存在节点,则返回null

3.修改节点
核心思想:首先查找节点,找到相应节点后,再进行修改(关键字不要进行修改)。

4.遍历二叉树
遍历二叉树分为三种方法:先序遍历,中序遍历,后序遍历。
先序遍历:访问节点,遍历节点的左子树,遍历节点的右子树。
中序遍历:遍历节点的左子树,访问节点,遍历节点的右子树。
后序遍历:遍历节点的左子树,遍历节点的右子树,访问节点。

树的每个节点模型:
package cn.edu.Tree;

public class Node {
		//关键字
	    private int keyData;
	    
	    //其他数据
	    private int otherData;
	    
	    //左子节点
	    private Node leftNode;
	    
	    //右子节点
	    private Node rightNode;


		public Node(int keyData, int otherData) {
			super();
			this.keyData = keyData;
			this.otherData = otherData;
		}


		public int getKeyData() {
			return keyData;
		}


		public void setKeyData(int keyData) {
			this.keyData = keyData;
		}


		public int getOtherData() {
			return otherData;
		}


		public void setOtherData(int otherData) {
			this.otherData = otherData;
		}


		public Node getLeftNode() {
			return leftNode;
		}


		public void setLeftNode(Node leftNode) {
			this.leftNode = leftNode;
		}


		public Node getRightNode() {
			return rightNode;
		}


		public void setRightNode(Node rightNode) {
			this.rightNode = rightNode;
		}
	    
	    //显示方法
		public void display(){
			System.out.println(this.keyData+" "+this.otherData);
		}
}

二叉树的模型:
package cn.edu.Tree;

public class Tree {
		//根
	    private Node root=null;
	    
	    //插入方法
	    public void insert(int keyData,int otherData){
	    	Node newNode=new Node(keyData,otherData);
	    	
	    	//如果说没有节点
	    	if(root==null){
	    		root=newNode;
	    	}else{
	    	  Node current=root;
	    	  Node parent;
	    	  while(true){
	    	    parent=current;
		    if(keyData<current.getKeyData()){
			    current=current.getLeftNode();
			    if(current==null){
			    	parent.setLeftNode(newNode);
			    	break;
			    }
			   }else{
			    current=current.getRightNode();
			    if(current==null){
			     parent.setRightNode(newNode);
			    	break;
			    }
			   }
	    		}
	    	}
	    }
	    
	    //查找方法
	    public Node find(int keyData){
	    	Node current=root;
	    	while(current.getKeyData()!=keyData){
	    		if(keyData<current.getKeyData()){
	    			current=current.getLeftNode();
	    		}else{
	    			current=current.getRightNode();
	    		}
	    		
	    		if(current==null){
	    			return null;
	    		}
	    	}
	    	return current;
	    }
	    
	    //删除方法
	    public void delete(int keyData){
	    	
	    }
	    
	    //修改方法
	    public void change(int keyData,int otherData){
	    	Node findNode=find(keyData);
	    	findNode.setOtherData(otherData);
	    }
	    
	    //先序遍历
	    public void preOrder(Node node){
	    	if(node!=null){
	    		node.display();
	    		preOrder(node.getLeftNode());
	    		preOrder(node.getRightNode());
	    	}
	    }
	    
	    //中序遍历
	    public void inOrder(Node node){
	    	if(node!=null){
	    		inOrder(node.getLeftNode());
	    		node.display();
	    		inOrder(node.getRightNode());
	    	}
	    }
	    
	    //后序遍历
	    public void endOrder(Node node){
	    	if(node!=null){
	    		endOrder(node.getLeftNode());
	    		endOrder(node.getRightNode());
	    		node.display();
	    	}
	    }
	    
	    public Node getRoot(){
	    	return this.root;
	    }
}

遍历测试:
package cn.edu.Tree;

public class TestTree {
		public static void main(String[] args) {
			Tree tree=new Tree();
			tree.insert(80, 80);
			tree.insert(49, 49);
			tree.insert(42, 42);
			tree.insert(30, 30);
			tree.insert(45, 45);
			tree.insert(90, 90);
			tree.insert(150, 150);
			tree.insert(130, 130);
			tree.insert(82, 82);
			
			
			tree.preOrder(tree.getRoot());
			/* 先序遍历结果
			    80 80
				49 49
				42 42
				30 30
				45 45
				90 90
				82 82
				150 150
				130 130
			 * */
			System.out.println("-----------------------");
			tree.inOrder(tree.getRoot());
			/*中序遍历结果
			    30 30
				42 42
				45 45
				49 49
				80 80
				82 82
				90 90
				130 130
				150 150
			 * */
			System.out.println("-----------------------");
			tree.endOrder(tree.getRoot());
			/*后序遍历结果
			    30 30
				45 45
				42 42
				49 49
				82 82
				130 130
				150 150
				90 90
				80 80


			 * */
			
			/*tree.change(2, 22);
			
			Node findNode=tree.find(2);
			findNode.display();*/
		}
}

转载请注明出处:http://blog.csdn.net/acmman/article/details/50487767
相关文章
|
19天前
|
存储 Java
Java实现二叉树
Java实现二叉树
26 0
|
2天前
|
存储
数据结构——二叉树
数据结构——二叉树
TU^
|
2天前
|
存储 算法
数据结构~~链式二叉树
数据结构~~链式二叉树
TU^
3 0
|
2天前
|
缓存 安全 Java
全面解读ConcurrentHashMap:Java中的高效并发数据结构
全面解读ConcurrentHashMap:Java中的高效并发数据结构
7 2
|
2天前
|
算法 Java 索引
12.12_黑马数据结构与算法笔记Java
12.12_黑马数据结构与算法笔记Java
12 1
|
3天前
|
Java
二叉树搜索 - Java版
二叉树搜索 - Java版
3 0
|
5天前
|
存储
数据结构初阶 初识二叉树
数据结构初阶 初识二叉树
7 0
|
10天前
|
算法
数据结构和算法学习记录——小习题-二叉树的遍历&二叉搜索树
数据结构和算法学习记录——小习题-二叉树的遍历&二叉搜索树
13 0
|
10天前
|
算法
数据结构和算法学习记录——层序遍历(层次遍历)、二叉树遍历的应用(输出二叉树中的叶节点、求二叉树的高度、二元运算表达式树及其遍历、由两种遍历序列确定二叉树)
数据结构和算法学习记录——层序遍历(层次遍历)、二叉树遍历的应用(输出二叉树中的叶节点、求二叉树的高度、二元运算表达式树及其遍历、由两种遍历序列确定二叉树)
12 0
|
10天前
|
存储 算法
数据结构和算法学习记录——二叉树的非递归遍历(中序遍历、先序遍历、后序遍历)
数据结构和算法学习记录——二叉树的非递归遍历(中序遍历、先序遍历、后序遍历)
11 0