23
Binary Search Tree 88621159 Data Structures and Algorithms 2/2561

Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

  • Upload
    others

  • View
    13

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Binary Search Tree

88621159 Data Structures and Algorithms 2/2561

Page 2: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Tree

Tree เป็นโครงสรา้งหลกัอีกชนิดหนึง่สามารถน ามาใช้ในการจัดการกับข้อมูล

คล้ายกับการจัดเก็บด้วย Linked-List เพียงแต่มีเงื่อนไข และวิธีการในการจัดการที่ต่างกัน

Page 3: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Tree

Path หมายถึงเส้นทางจาก node ใด node หนึ่งไปยัง node สุดท้ายที่อยู่ในเส้นทางนั้น

Edge หมายถึงเส้นทางเชื่อมต่อระหว่าง node สอง node (บางครั้งเรียกว่า link)

Root หมายถึง node ที่อยู่บนสุดใน tree นั้นๆ ซึ่งมีเพียง node เดียวเท่านั้น

Parent คือ โหนดที่อยู่ในล าดับบนของอีกโหนดหนึ่ง

Child คือ โหนดที่อยู่ในล าดับล่างของอีกโหนดหนึ่ง

Sibling คือ โหนดที่อยู่ในระดับเดียวกัน

Leaf คือ โหนดที่อยู่ในต าแหน่งล่างสุดของ tree

Sub-tree คือ กลุ่มของ node ที่อยู่ใน tree โดยมี node ใด node หนึ่งเป็น root

Page 4: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Tree

Root

EdgePath

A เป็น RootA เป็น Parent ของ B

B เป็น Child ของ A

B และ C เป็น Sibling

D E F C เป็น Leaf

B D E F เป็น Sub-tree ของ A

Leaf

Page 5: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Binary Tree

Binary tree เป็น tree ท่ี node แต่ละ node สามารถที่จะมีลูก (children) ได้สูงสุด 2 node อยู่ทางซ้ายหนึ่ง node อยู่ทางขวาหนึ่ง node

Page 6: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Binary Search Tree (BST)

• เป็น Binary tree ที่มีข้อก ำหนดพิเศษเฉพำะตัวคือข้อมูลของ node

• ลูกที่อยู่ทำงซ้ำยของ node parent จะต้องมีค่ำน้อยกว่ำค่ำของข้อมูลที่อยู่ใน node parent

• และ ค่ำของข้อมูลที่อยู่ใน node ลูกที่อยู่ทำงขวำของ node parent จะมีค่ำมำกกว่ำค่ำของข้อมูลที่อยู่ใน node parent

click

Page 7: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Java Implementation

• To implement: use two links per Node.

• A Node is comprised of:• A item.• A reference to the left subtree.• A reference to the right subtree.

private class Node {private T item;private Node left;private Node right;

private Node(T item) {this.item = item;left = null;right = null;

}}

Page 8: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

BST: Skeletonclass BST<T extends Comparable<T>> {

private Node root;

private class Node {private T item;private Node left;private Node right;

private Node(T item) {this.item = item;left = null;right = null;

}}

BST() { root = null; }

public void insert(T item) {...}private Node insert(T item, Node node) {...}public boolean search(T item) {...}private boolean search(T item, Node node) {...}

}

requires T to provide compareTo() method;

BST. Allow generic item

Page 9: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Recursion on Trees9

เรำสำมำรถเขยีน Recursive methods เพื่อปฏิบัติกำรบน trees ได้

Base case empty tree leaf node

Recursive case แก้ปัญหำต้นไม้ย่อยทำงด้ำนซ้ำย และ ด้ำนขวำ น ำค ำตอบของปัญหำต้นไม้ย่อย ๆ มำรวมกันเพื่อให้ได้ค ำตอบ

ของปัญหำต้นไม้ที่ใหญ่กว่ำ

Page 10: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

BST: insertpublic void insert(T item) {

root = insert(item, root);}

private Node insert(T item, Node node) {if(node == null)

return new Node(item);

int cmp = item.compareTo(node.item);

if(cmp < 0)node.left = insert(item, node.left);

else if(cmp > 0)node.right = insert(item, node.right);

return node;}

Page 11: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

BST: search

public boolean search(T item) {return search(item, root);

}

private boolean search(T item, Node node) {while(node != null) {

int cmp = item.compareTo(node.item);if(cmp < 0)

node = node.left;else if(cmp > 0)

node = node.right;else

return true; //found item}

return false; //no item found}

Page 12: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Binary Tree Traversals

• Preorder traversal

• Postorder traversal

• Inorder traversal

class BST<T extends Comparable<T>> {.........

//print data of node in Pre-order public void preorder() {…}private void preorder(Node node) {…}

//print data of node in Post-order public void postorder() {…}private void postorder(Node node) {…}

//print data of node in in-order public void inorder() {…}private void inorder(Node node) {…}

}

Page 13: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Preorder traversal

10 5 2 8 7 14

click

5

10

2

14

8

7

• Visit node.• Recursively visit left subtree.• Recursively visit right subtree.

Page 14: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Preorder traversal

public void preorder() {preorder(root);

}

private void preorder(Node node) {if (node == null)

return;

System.out.print(node.item + " ");preorder(node.left);preorder(node.right);

}

Page 15: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Postorder traversal

2 7 8 5 14 10

click

5

10

2

14

8

7

• Recursively visit left subtree.• Recursively visit right subtree.• Visit node.

Page 16: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Postorder traversal

//print data of node in Post-order fashionpublic void postorder() {

postorder(root);}

private void postorder(Node node) {if(node==null)

return;

postorder(node.left);postorder(node.right);System.out.print(node.item + " ");

}

Page 17: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Inorder traversal

• Recursively visit left subtree.• Visit node.• Recursively visit right subtree.

2 5 7 8 10 14

click

5

10

2

14

8

7

Page 18: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Inorder traversal

//print data of node in in-order fashionpublic void inorder() {

inorder(root);}

private void inorder(Node node) {if (node == null)

return;

inorder(node.left);System.out.print(node.item + " ");inorder(node.right);

}

Page 19: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

19

BST: Analysis

• Running time per put/get. • There are many BSTs that correspond to same set of keys.• Cost is proportional to depth of node.

we

be

at no

go pi

ifdo of

hi me

hi

at no

do if pi

mebe go weof

number of nodes on path from root to node

depth = 4

depth = 3

depth = 2

depth = 5

depth = 1

Page 20: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

BST: Analysis

• Best case. If tree is perfectly balanced, depth is at most log N.

Page 21: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

BST: Analysis

• Worst case. If tree is unbalanced, depth is N.

Page 22: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

BST: Analysis

• Average case. If keys are inserted in random order,average depth is 2 log N.

Page 23: Binary Search Tree - Burapha UniversityBinary Search Tree (BST) •เป น Binary tree ท ม ข อก ำหนดพ เศษเฉพำะต วค อข อม ลของ

Binary Search Tree

Input: 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10

Preorder: 7 1 0 3 2 5 4 6 9 8 10Inorder: 0 1 2 3 4 5 6 7 8 9 10Postorder: 0 2 4 6 5 3 1 8 10 9 7