二叉树的三种遍历
2018-03-24 17:06
225 查看
Public class Node {
// 关键字
private int keyData;
// 其他数据
private int otherData;
// 左子节点
private Node leftNode;
// 右子节点
private Node rightNode;
public Node(int keyData, int otherDate) {
this.keyData = keyData;
this.otherData = otherDate;
}
public int getKeyData() {
return keyData;
}
public void setKeyData(int keyData) {
this.keyData = keyData;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public int getOtherData() {
return otherData;
}
public void setOtherData(int otherData) {
this.otherData = otherData;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
// 显示方法
public void display(){
System.out.println(keyData + "," + otherData);
}
}
ublic class Tree {
// 根
private Node 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.setLeftNode(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 endOrder(Node node) {
if (node != null) {
endOrder(node.getLeftNode());
endOrder(node.getRightNode());
node.display();
}
}
public Node getRoot() {
return root;
}
}
// 关键字
private int keyData;
// 其他数据
private int otherData;
// 左子节点
private Node leftNode;
// 右子节点
private Node rightNode;
public Node(int keyData, int otherDate) {
this.keyData = keyData;
this.otherData = otherDate;
}
public int getKeyData() {
return keyData;
}
public void setKeyData(int keyData) {
this.keyData = keyData;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public int getOtherData() {
return otherData;
}
public void setOtherData(int otherData) {
this.otherData = otherData;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
// 显示方法
public void display(){
System.out.println(keyData + "," + otherData);
}
}
ublic class Tree {
// 根
private Node 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.setLeftNode(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 endOrder(Node node) {
if (node != null) {
endOrder(node.getLeftNode());
endOrder(node.getRightNode());
node.display();
}
}
public Node getRoot() {
return root;
}
}
相关文章推荐
- 再谈二叉树:三种遍历六种实现
- 二叉树的先中后序三种遍历,和先序的建立,代码详解!!
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 二叉树的三种遍历方式:递归、栈、循环 分类: C/C++ 数据结构与算法 2015-06-21 09:47 269人阅读 评论(0) 收藏
- 二叉树的创建,插入,删除,输出,求高度,求度以及三种遍历方式实现
- 二叉树三种遍历的非递归算法(背诵版)
- 重温数据结构:二叉树的常见方法及三种遍历方式 Java 实现
- 二叉树的顺序存储和三种遍历(二)
- 二叉树的三种非递归遍历和morris遍历
- 二叉树的三种遍历
- 二叉树三种遍历非递归算法
- 二叉树三种遍历方式 递归与非递归实现 总结
- 看懂二叉树的三种遍历
- Java中创建二叉树并实现三种遍历方式
- 数据结构(二叉树系列)先序创建三种遍历和求深度(递归实现)
- 二叉树三种遍历递归及非递归实现
- 二叉树的三种遍历
- 二叉树中的三种遍历方式
- 二叉树的非递归的三种遍历
- 二叉树 三种遍历构造二叉树+层次遍历 题目 PAT 树的遍历