首页 > 编程语言 > 详细

二叉树(java版)

时间:2015-02-06 09:32:38      阅读:311      评论:0      收藏:0      [点我收藏+]
//节点类
package com.huowolf.test2;

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

		public Node(int keyData, int otherData) {
			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 setLefNode(Node leftNode) {
			this.leftNode = leftNode;
		}

		public Node getRightNode() {
			return rightNode;
		}

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

public class Tree {
		private Node root;

		public Node getRoot() {
			return root;
		}
		
		//插入方法
		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.setLefNode(newNode);
							return;
						}
					} else {
						current = current.getRightNode();
						if(current == null) {
							parent.setRightNode(newNode);
							return;
						}
					}
				}
			}
		}
		
		//查找方法
		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 change(int keyData, int newOtherData) {
			Node findNode = find(keyData);
			findNode.setOtherData(newOtherData);
		}
		
		//先序遍历
		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 postOrder(Node node) {
			if(node != null) {
				postOrder(node.getLeftNode());
				postOrder(node.getRightNode());
				node.display();
			}
			
		}
}

//测试类
package com.huowolf.test2;

public class Tree_Test {

	public static void main(String[] args) {
		Tree tree = new Tree();
		tree.insert(80, 75);
		tree.insert(49, 49);
		tree.insert(42, 42);
		tree.insert(30, 30);
		tree.insert(45,45);
		tree.insert(150, 110);
		tree.insert(90, 90);
		tree.insert(130, 130);
		tree.insert(82,86);
		
		//修改结点
		tree.change(90, 92);
		Node findNode = tree.find(90);
		findNode.display();
		
		System.out.println("先序遍历:");
		tree.preOrder(tree.getRoot());
		System.out.println("-------------------------");
		
		System.out.println("中序遍历:");
		tree.inOrder(tree.getRoot());
		System.out.println("-------------------------");
		
		System.out.println("后序遍历:");
		tree.postOrder(tree.getRoot());
	}
	
	

}


二叉树(java版)

原文:http://blog.csdn.net/huolang_vip/article/details/43550221

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!