数据结构之树

  • Post author:
  • Post category:其他


树是一种递归结构!

具有n个节点的数据集。

常见由二叉树,二叉查找树,红黑树,B+tree,B-tree等。

下面通过一段代码来实现简单的二叉树:

首先二叉树:

每个节点至多由两个子树。i层的节点总数不大于2^i-1。度为2就指的是子树为2.

层数和深度是指树的下延数。

二叉树又分为完全二叉树(除了最后一层其它各层的子树都满)和满二叉树(与完全二叉树不同在于所有层数都是满的达到了此树的度)。

//定义二叉树对象

public class BinaryTree {


private TreeNode root;// 根节点

public BinaryTree() {


// TODO Auto-generated constructor stub

}

public BinaryTree(TreeNode root) {


this.root = root;

}

public TreeNode getRoot() {


return root;

}

public void setRoot(TreeNode root) {


this.root = root;

}

/*

* 定义节点内部类

*/

private static class TreeNode {


// 数据部分

private String data = null;

// 左节点的引用

private TreeNode left;

// 右节点的引用

private TreeNode right;

public TreeNode() {


// TODO Auto-generated constructor stub

}

//含参构造

public TreeNode(String data, TreeNode left, TreeNode right) {


super();

this.data = data;

this.left = left;

this.right = right;

}

public String getData() {


return data;

}

public void setData(String data) {


this.data = data;

}

public TreeNode getLeft() {


return left;

}

public void setLeft(TreeNode left) {


this.left = left;

}

public TreeNode getRight() {


return right;

}

public void setRight(TreeNode right) {


this.right = right;

}

}

/**

* 返回父节点

*

* @param element

* @return

*/

public TreeNode GetParent(TreeNode element) {


return (root==null||root==element)

?null:parent(root,element);

}

public TreeNode parent(TreeNode subTree, TreeNode element) {


if (subTree==null) return null;

if (subTree.getLeft()==element||subTree.getRight()==element)

//返回父节点地址

return subTree;

TreeNode p;

//现在左子树中找,如果左子树中没有找到,才到右子树去找

if ((p=parent(subTree.getLeft(), element))!=null)

//递归在左子树中搜索

return p;

else

//递归在右子树中搜索

return parent(subTree.getRight(), element);

}

/**

* 节点个数

* @return*/

public int getSize(){


return getNum(root);

}

private int getNum(TreeNode node) {


if (node==null)

{


return 0;

}

else

{


int i=getNum(node.getLeft());

int j=getNum(node.getRight());

return j+i+1;

}

}

/**

* 获取树的高度

* @return*/

public int getHeight()

{


return getHeight(root);

}

private int getHeight(TreeNode node)

{


if (node==null)

return 0;

else

{


int i=getHeight(node.getLeft());

int j=getHeight(node.getRight());

return (i<j)?(j+1):(i+1);

}

}

/**

* 前序遍历

*

* @param node*/

public void preOrder(TreeNode node)

{


if (node!=null) {


System.out.println(node.getData());

preOrder(node.getLeft());

preOrder(node.getRight());

}

}

/**

* 中序遍历

*

* @param node*/

public void inOrder(TreeNode node)

{


if (node!=null) {


inOrder(node.getLeft());

System.out.println(node.getData());

inOrder(node.getRight());

}

}

/**

* 后序遍历

*

* @param node*/

public void postOrder(TreeNode node)

{


if (node!=null) {


postOrder(node.getLeft());

postOrder(node.getRight());

System.out.println(node.getData());

}

}

/**

* 插入操作

* @param String value*/

public void insert(String value){


TreeNode newNode = new TreeNode(value,null,null);

if (root==null) {


root=newNode;

root.setLeft(null);

root.setRight(null);

}else{


TreeNode currentNode=root;

TreeNode parentNode;

while (true) {


parentNode=currentNode;

//放右子树节点

if (currentNode==null) {


currentNode.data=value;

currentNode.left=null;

currentNode.right=null;

return;

}

if (currentNode.getLeft()==null) {


currentNode=currentNode.left;

}else if (currentNode.getRight()==null) {


currentNode=currentNode.right;

}else {


if (getHeight(root.right)>getHeight(root.left)) {

}

}

}

}

}

public static void main(String[] args) {


TreeNode l12 = new TreeNode(“left12”, null, null);

TreeNode r12 = new TreeNode(“right12”, null, null);

TreeNode l22 = new TreeNode(“left22”, null, null);

TreeNode r22 = new TreeNode(“right22”, null, null);

TreeNode l1 = new TreeNode(“left1”,l12,r12);//根节点左子树

TreeNode r1 = new TreeNode(“right1”,l22,r22);//根节点右子树

//创建根节点

TreeNode root = new TreeNode(“root”,l1,r1);

BinaryTree bt = new BinaryTree(root);

System.out.println(“======先序遍历======”);

bt.preOrder(bt.getRoot());

System.out.println(“======中序遍历======”);

bt.inOrder(bt.getRoot());

System.out.println(“======后序遍历======”);

bt.postOrder(bt.getRoot());

System.out.println(“===========”);

System.out.println(bt.getHeight());

System.out.println(bt.getSize());

System.out.println(bt.GetParent(r22).getData());

}

}

//理解的还是不够深入,对于一些具体细节还是由疑问???

转载于:https://www.cnblogs.com/plas/p/9963738.html