20162314 Experiment 2 - Tree

Experiment report of Besti

course:《Program Design & Data Structures》
Class: 1623

Student Name: Wang, Yixiao

Student Number:20162314

Tutor:Mr.Lou、Mr.Wang

Experiment date:2017.10.27

Secret level: Unsecretive

Experiment time:60 minutes

Major/Elective:Major
Experiment order:6

Experiment content

  1. LinkedBinaryTreeTest
    1062769-20171027100200539-323572144.png
  1. BinaryTree
    1062769-20171027100318508-1264398751.png
  1. DecisionTree
    1062769-20171027100717351-96157896.png
  1. Expression Tree
    1062769-20171027100930008-1519922577.png
  1. BinarySearchTree
    1062769-20171027101018555-1532143724.png

6.Analyze Source Code (Red-Black Tree)
1062769-20171028234750898-1605709416.png

Experiment situation

Exp1 LinkedBinaryTreeTest

1062769-20171027101123570-590297243.png

  • It's easy to finish this experiment.
  • To start with , import to form a new LinkedBinaryTree to start the experiment.
  • Next , use the method GetRight GetLeft to ealuation.
  • Then , new element "ABC".
  • Last , Assert.assertequals();

Exp2 BinaryTree

1062769-20171027110138273-1128171912.png

  • To start with , use the number to replace "A B C D ....."
  • Next , new newNode
  • Then, creat BinTree to get left child and right child
  • Use three methods preOrderTraverse,inOrderTraverse,postOrderTraverse.

Exp3 Decision Tree

1062769-20171027111011258-260180677.png

  • To start with , creat a class TwentyQuestionsPlayer
  • String the Questions and the answers as the order
  • New them
  • Write a method play(),Yes =Y , No=N .
  • Next , write a function to finish this experiment.

Exp4 ExpressionTree

1062769-20171027112131695-2100480219.png

Exp5 BinarySearchTreeTest

1062769-20171027112850226-1804927056.png

  • To start with , creat a class BinarySearchTree
  • public method Node find, insert,preorder,inorder,postorder,getMinNode get MaxNode,Delete.
  • creat three new Nodes(id and name);
  • Node n1 n2 n3 n4 ...n7 n8
  • Use method insert to n4 n5 n6 n7 n8
  • Bst.inorder and Systemin.

Exp6 Red-Black Tree (Analyze Source Code)

  • TreeMap底层通过红黑树(Red-Black tree)实现,也就意味着containsKey(), get(), put(), remove()都有着log(n)的时间复杂度。
    1062769-20171028235933914-1994077632.png
  • SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));

  • 结构调整过程包含两个基本操作:左旋(Rotate Left),右旋(RotateRight)。
    1062769-20171029000128430-1849096305.png
  • Rotate Left
  //Rotate Left
private void rotateLeft(Entry<K,V> p) {
    if (p != null) {
        Entry<K,V> r = p.right;
        p.right = r.left;
        if (r.left != null)
            r.left.parent = p;
        r.parent = p.parent;
        if (p.parent == null)
            root = r;
        else if (p.parent.left == p)
            p.parent.left = r;
        else
            p.parent.right = r;
        r.left = p;
        p.parent = r;
    }
}

1062769-20171029000251555-1168084459.png

  • Rotate Right
  //Rotate Right
private void rotateRight(Entry<K,V> p) {
    if (p != null) {
        Entry<K,V> l = p.left;
        p.left = l.right;
        if (l.right != null) l.right.parent = p;
        l.parent = p.parent;
        if (p.parent == null)
            root = l;
        else if (p.parent.right == p)
            p.parent.right = l;
        else p.parent.left = l;
        l.right = p;
        p.parent = l;
    }
}
  • .get()
    1062769-20171029000412070-1024565763.png
  //getEntry()方法
final Entry<K,V> getEntry(Object key) {
    ......
    if (key == null)//不允许key值为null
        throw new NullPointerException();
    Comparable<? super K> k = (Comparable<? super K>) key;//使用元素的自然顺序
    Entry<K,V> p = root;
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0)//向左找
            p = p.left;
        else if (cmp > 0)//向右找
            p = p.right;
        else
            return p;
    }
    return null;
}
  • .put()
  public V put(K key, V value) {
    ......
    int cmp;
    Entry<K,V> parent;
    if (key == null)
        throw new NullPointerException();
    Comparable<? super K> k = (Comparable<? super K>) key;//使用元素的自然顺序
    do {
        parent = t;
        cmp = k.compareTo(t.key);
        if (cmp < 0) t = t.left;//向左找
        else if (cmp > 0) t = t.right;//向右找
        else return t.setValue(value);
    } while (t != null);
    Entry<K,V> e = new Entry<>(key, value, parent);//创建并插入新的entry
    if (cmp < 0) parent.left = e;
    else parent.right = e;
    fixAfterInsertion(e);//调整
    size++;
    return null;
}

1062769-20171029000743101-458047815.jpg

  //红黑树调整函数fixAfterInsertion()
private void fixAfterInsertion(Entry<K,V> x) {
    x.color = RED;
    while (x != null && x != root && x.parent.color == RED) {
        if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
            Entry<K,V> y = rightOf(parentOf(parentOf(x)));
            if (colorOf(y) == RED) {//如果y为null,则视为BLACK
                setColor(parentOf(x), BLACK);              // 情况1
                setColor(y, BLACK);                        // 情况1
                setColor(parentOf(parentOf(x)), RED);      // 情况1
                x = parentOf(parentOf(x));                 // 情况1
            } else {
                if (x == rightOf(parentOf(x))) {
                    x = parentOf(x);                       // 情况2
                    rotateLeft(x);                         // 情况2
                }
                setColor(parentOf(x), BLACK);              // 情况3
                setColor(parentOf(parentOf(x)), RED);      // 情况3
                rotateRight(parentOf(parentOf(x)));        // 情况3
            }
        } else {
            Entry<K,V> y = leftOf(parentOf(parentOf(x)));
            if (colorOf(y) == RED) {
                setColor(parentOf(x), BLACK);              // 情况4
                setColor(y, BLACK);                        // 情况4
                setColor(parentOf(parentOf(x)), RED);      // 情况4
                x = parentOf(parentOf(x));                 // 情况4
            } else {
                if (x == leftOf(parentOf(x))) {
                    x = parentOf(x);                       // 情况5
                    rotateRight(x);                        // 情况5
                }
                setColor(parentOf(x), BLACK);              // 情况6
                setColor(parentOf(parentOf(x)), RED);      // 情况6
                rotateLeft(parentOf(parentOf(x)));         // 情况6
            }
        }
    }
    root.color = BLACK;
}

Code hosting

PSP5.1(Personal Software Process)

StepsTimepercent
requirement45minutes16.7%
design50minutes18.5%
coding1.5hours32.2%
test30minutes11.1%
summary55minutes19.2%

转载于:https://www.cnblogs.com/CS162314/p/7742410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值