树定义及二叉查找树实现

来源:互联网 发布:申请网络出版 编辑:程序博客网 时间:2024/06/07 11:03

1、树结构:树结构的特点是前驱唯一,后驱不唯一,是典型的一对多的关系。


2、结点的层次和树的深度:结点的层次(level)从根开始定义,层次数为0 的结点是根结点,其子树的根的层次数为1。若结点在L 层,其子树的根就在L+1 层。对于层次为k(k > 0)的每个结点c,都有且仅有一个层次为k-1 的结点p与之对应,p 称为c 的父亲(parent)或父结点。若p 是c 的父亲,则c 称为p 的孩子(child)。父子之间的连线是树的一条边。在树中根结点没有父亲,其余结点只有一个父结点,但是却可能有多个孩子,同一结点的孩子相互称为兄弟(sibling)。树中结点的最大层次数称为树的深度(Depth)或高度。树中结点也有高度,其高度是以该结点为根的树的高度。


3、结点的度与树的度:结点拥有的子树的数目称为结点的度(Degree)。度为0 的结点称为叶子(leaf)或终端结点。度不为0 的结点称为非终端结点或分支结点。


4、路径:在树中k+1 个结点通过k条边连接构成的序列{(v0,v1),(v1,v2), … ,(vk-1,vk)| k ≥ 0},称为长度为k的路径(path)。


5、二叉树定义:每个结点的度均不超过2 的有序树,称为二叉树(binary tree)。


6、满二叉树:高度为k并且有2 k+1-1 个结点的二叉树。


7、完全二叉树:若在一棵满二叉树中,在最下层从最右侧起去掉相邻的若干叶子结点,得到的二叉树即为完全二叉树。满二叉树必为完全二叉树,而完全二叉树不一定是满二叉树。


8、二叉树遍历:

⑴ 先序遍历(DLR)二叉树的操作定义为:
    若二叉树为空,则空操作;否则
    ① 访问根结点;
    ② 先序遍历左子树;
    ③ 先序遍历右子树。
⑵ 中序遍历(LDR)二叉树的操作定义为:
    若二叉树为空,则空操作;否则
    ① 中序遍历左子树;
    ② 访问根结点;
    ③ 中序遍历右子树。
⑶ 后序遍历(LRD)二叉树的操作定义为:
    若二叉树为空,则空操作;否则
    ① 后序遍历左子树;
    ② 后序遍历右子树;
    ③ 访问根结点。

9、二叉查找树:

或者是一棵空树,或者是具有下列性质的二叉树: 

若左子树不空,则左子树上所有结点的值均小于它的根结点的值; 

② 若右子树不空,则右子树上所有结点的值均大于它的根结点的值; 

③ 左、右子树也分别为二叉排序树


10.二叉查找树搜索算法是很简单的,使用递归遍历即可,简单实现如下:

1)树结构定义:

public class TreeNode{    private int key;    private TreeNode leftChild;    private TreeNode rightChild;    public TreeNode(int key)    {        this.key = key;        this.leftChild = null;        this.rightChild = null;    }    public int getKey()    {        return key;    }    public void setKey(int key)    {        this.key = key;    }    public TreeNode getLeftChild()    {        return leftChild;    }    public void setLeftChild(TreeNode leftChild)    {        this.leftChild = leftChild;    }    public TreeNode getRightChild()    {        return rightChild;    }    public void setRightChild(TreeNode rightChild)    {        this.rightChild = rightChild;    }}


2)树生成及搜索实现:

public class BinarySearchTree{    public TreeNode insertTree(TreeNode node, int key)    {        if (null == node)        {            node = new TreeNode(key);        }        else if (key < node.getKey())        {            if (node.getLeftChild() == null)            {                node.setLeftChild(new TreeNode(key));            }            else            {                insertTree(node.getLeftChild(), key);            }        }        else        {            if (node.getRightChild() == null)            {                node.setRightChild(new TreeNode(key));            }            else            {                insertTree(node.getRightChild(), key);            }        }        return node;    }    /**     * 输入root节点和要查找的key值     *      * @param root     * @param key     * @return 查到的节点     */    public TreeNode serach(TreeNode root, int key)    {        if (null == root)        {            return null;        }        if (key < root.getKey())        {            return serach(root.getLeftChild(), key);        }        else if (key > root.getKey())        {            return serach(root.getRightChild(), key);        }        else        {            return root;        }    }}
3)测试类:

public class BinarySearchTreeTest{    /**     * @param args     */    public static void main(String[] args)    {        int[] data = new int[] { 22, 10, 15, 13, 35, 24, 40 };        TreeNode node = new TreeNode(22);        BinarySearchTree s = new BinarySearchTree();        for (int i = 1; i < data.length; i++)        {            s.insertTree(node, data[i]);        }        System.out.println(s.serach(node, 13).getKey());    }}



0 0