一、概述:
1、特征:路径、根、父节点、子节点、子树、访问节点、遍历、层、关键字
2、二叉树:
1)每个节点最多有两个子节点
2)每个节点之间只有一条路径
3)小于父节点的在左边,大于的在右边。
二、实现二叉树:
1、创建子节点:Node
/**
* @项目名称 Java_DataStruct
* @包名 com.java.tree
* @类名 Node
* @author chenlin
* @date 2011年6月18日 下午9:49:19
*/
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 Node() {
super();
}
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("keyData=" + keyData + ", otherData=" + otherData + "");
}
}
2、创建数:Tree
1)插入节点:
如果不存在,则直接插入
从根节点开始查找一个相应的节点,即其父节点,然后确定是做节点还是有节点
/**
* 插入节点
* @param node
*/
public void insert(Node node){
if (root == null) {
root = node;
}else {
Node current = root;
Node parent;
while(true){
parent = current;
if (node.getKeyData() < current.getKeyData()) {
System.out.println("left node == " + node.getKeyData());
System.out.println("left current == " + current.getKeyData());
current = current.getLeftNode();
if (current == null) {
//到最后一个节点时,把新的节点放上去
parent.setLeftNode(node);
//设置好了,就不能往下走了
return;
}
}else {
System.out.println("right node == " + node.getKeyData());
System.out.println("right current == " + current.getKeyData());
current = current.getRightNode();
if (current == null) {
//到最后一个节点时,把新的节点放上去
parent.setRightNode(node);
return;
}
}
}
}
}
2)查找节点:
从根节点开始查找,如果比父节点小,则查找坐标,否则查右边,没找到返回null
/**
* 查找节点
* @param node
* @return
*/
public Node search(Node node){
Node current = root;
//查找得使用循环
while (node.getKeyData() != current.getKeyData()) {
//如果小于,从左边不断地找
if (node.getKeyData() < current.getKeyData()) {
current = current.getLeftNode();
}else {
current = current.getRightNode();
}
if (current == null) {
return null;
}
}
return current;
}
/**
* 查找节点
* @param node
* @return
*/
public Node search(int key){
Node current = root;
while (current.getKeyData() != key) {
if (key < current.getKeyData()) {
current = current.getLeftNode();
}else {
current = current.getRightNode();
}
if (current == null) {
return null;
}
}
return current;
}
3)修改节点:
先查找到节点,替换原来节点的otherData
/**
* 修改时,key不能修改,只能修改其它otherData
* @param key
* @param newNode
*/
public void update(int key, Node newNode){
Node node = search(key);
node.setOtherData(newNode.getOtherData());
}
4)遍历
二叉树遍历的三种思想:先序遍历、中序遍历、后序遍历
5)先序遍历:
访问节点–>遍历左节点–>遍历右节点
/**
* 先序遍历
* @param node
*/
public void preList(Node node){
if (node != null) {
node.display();
preList(node.getLeftNode());
preList(node.getRightNode());
}
}
/**
* 中序遍历
* @param node
*/
public void centerList(Node node){
if (node != null) {
centerList(node.getLeftNode());
node.display();
centerList(node.getRightNode());
}
}
/**
* 后序遍历
* @param node
*/
public void backList(Node node){
if (node != null) {
backList(node.getLeftNode());
backList(node.getRightNode());
node.display();
}
}
三、测试:
Tree tree = new Tree();
tree.insert(40, 40);
tree.insert(36, 36);
tree.insert(32, 32);
tree.insert(30, 30);
tree.insert(31, 31);
tree.insert(33, 33);
tree.insert(34, 34);
tree.insert(50, 50);
tree.insert(48, 48);
tree.insert(45, 45);
tree.insert(52, 52);
tree.insert(56, 56);
tree.preList(tree.getRoot());
System.out.println("--------------------------------");
tree.centerList(tree.getRoot());
System.out.println("--------------------------------");
tree.backList(tree.getRoot());
插入结构图:
结果:
keyData=40, otherData=40
keyData=36, otherData=36
keyData=32, otherData=32
keyData=30, otherData=30
keyData=31, otherData=31
keyData=33, otherData=33
keyData=34, otherData=34
keyData=50, otherData=50
keyData=48, otherData=48
keyData=45, otherData=45
keyData=52, otherData=52
keyData=56, otherData=56
keyData=30, otherData=30
keyData=31, otherData=31
keyData=32, otherData=32
keyData=33, otherData=33
keyData=34, otherData=34
keyData=36, otherData=36
keyData=40, otherData=40
keyData=45, otherData=45
keyData=48, otherData=48
keyData=50, otherData=50
keyData=52, otherData=52
keyData=56, otherData=56
keyData=31, otherData=31
keyData=30, otherData=30
keyData=34, otherData=34
keyData=33, otherData=33
keyData=32, otherData=32
keyData=36, otherData=36
keyData=45, otherData=45
keyData=48, otherData=48
keyData=56, otherData=56
keyData=52, otherData=52
keyData=50, otherData=50
keyData=40, otherData=40