Java数据结构(二叉数基础与遍历篇)

来源:互联网 发布:大数据涂子沛 编辑:程序博客网 时间:2024/04/29 09:41

二叉树:在计算机科学中,树是一种重要的非线性数据结构,直观地看,它是数据元素(在树中称为结点)按分支关系组织起来的结构。二叉树是每个节点最多有两个子树的有序树。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。值得注意的是,二叉树不是树的特殊情形。在图论中,二叉树是一个连通的无环图,并且每一个顶点的度不大于3。有根二叉树还要满足根结点的度不大于2。有了根结点后,每个顶点定义了唯一的根结点,和最多2个子结点。然而,没有足够的信息来区分左结点和右结点。


Node节点:

package ch06BinaryTree;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 setLeftNode(Node leftNode) {this.leftNode = leftNode;}public Node getRightNode() {return rightNode;}public void setRightNode(Node rightNode) {this.rightNode = rightNode;}public void dispaly(){System.out.println(keyData + "," + otherData);}}

二叉树:

package ch06BinaryTree;public 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(current.getKeyData() > keyData)current = current.getLeftNode();elsecurrent = current.getRightNode();if(current == null)return null;}return current;}// 修改方法public void change(int keyData, int otherData){Node findNode = find(keyData);findNode.setOtherData(otherData);}// 先序遍历public void preOrder(Node node){if(node != null){node.dispaly();preOrder(node.getLeftNode());preOrder(node.getRightNode());}}// 中序遍历public void inOrder(Node node){if(node != null){inOrder(node.getLeftNode());node.dispaly();inOrder(node.getRightNode());}}// 后续遍历public void endOrder(Node node){if(node != null){endOrder(node.getLeftNode());endOrder(node.getRightNode());node.dispaly();}}// 得到根节点public Node getRoot(){return root;}}

测试:

package ch06BinaryTree;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.find(30).dispaly();//tree.change(30, 520);//tree.find(30).dispaly();tree.preOrder(tree.getRoot());System.out.println("---------");tree.inOrder(tree.getRoot());System.out.println("---------");tree.endOrder(tree.getRoot());}}

结果:

80,8049,4942,4230,3045,4590,9082,82150,150130,130---------30,3042,4245,4549,4980,8082,8290,90130,130150,150---------30,3045,4542,4249,4982,82130,130150,15090,9080,80

以下来自百度百科:

遍历是对树的一种最基本的运算,所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。由于二叉树是非线性结构,因此,树的遍历实质上是将二叉树的各个结点转换成为一个线性序列来表示。
设L、D、R分别表示遍历左子树、访问根结点和遍历右子树, 则对一棵二叉树的遍历有三种情况:DLR(称为先根次序遍历),LDR(称为中根次序遍历),LRD (称为后根次序遍历)。
(1)先序遍历
首先访问根,再先序遍历左(右)子树,最后先序遍历右(左)子树,C语言代码如下:
void XXBL(tree* root)
{
//Do Something with root
if (root->lchild!=NULL)
XXBL(root->lchild);
if (root->rchild!=NULL)
XXBL(root->rchild);
}
(2)中序遍历
首先中序遍历左(右)子树,再访问根,最后中序遍历右(左)子树,C语言代码如下
void ZXBL(tree* root)
{
if(root->lchild!=NULL)
ZXBL(root->lchild);
//Do Something with root
if(root->rchild!=NULL)
ZXBL(root->rchild);
}
(3)后序遍历
首先后序遍历左(右)子树,再后序遍历右(左)子树,最后访问根,C语言代码如下
void HXBL(tree* root)
{
if (root->lchild!=NULL)
HXBL(root->lchild);
if (root->rchild!=NULL)
HXBL(root->rchild);
//Do Something with root
}

原创粉丝点击