Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure & Algorithm 09 – Binary Search Tree JJCAO.

Similar presentations


Presentation on theme: "Data Structure & Algorithm 09 – Binary Search Tree JJCAO."— Presentation transcript:

1 Data Structure & Algorithm 09 – Binary Search Tree JJCAO

2 Dictionary ADT 2

3 Priority Queue ADT Priority Queue – a set of elements S, each with a key: insert(S,x) - insert element x into S S <- S U {x} max(S) - return element of S with largest key extract-max(S) - remove and return element of S with largest key Possible implementations: Heap 3

4 DynamicArray StackQueueHeap PriorityQueue HashTable Dictionary 4

5 (Binary) Search Trees ADT Operations: Initialize - the data structure Search - For an element with a given key Insert - a new element Delete - a specified element Predecessor - return element with closest smaller key Successor - return element with closest larger key Join – combine two dictionaries to make a larger one PrintSorted - print the dictionary in sorted order Minimum - return the element with smallest key Maximum - return the element with largest key 5 Dictionary

6 Binary Search Trees 6

7 Different binary search trees can represent the same set of values. The worst-case running time for most search-tree operations is proportional to the height of the tree. (a) A binary search tree on 6 nodes with height 2. (b) A less efficient binary search tree with height 4 that contains the same keys. 7

8 class Node{ int m_key; //data item (key) double m_data; //data item Node* m_leftChild; //this node’s left child Node* m_rightChild; //this node’s right child }; 8 class Tree{ private: Node* pRoot; //first node of tree public: Tree() : pRoot(NULL) { } Node* search(int key) { /*body not shown*/ } Node* min(int key) ) { /*body not shown*/ } … }; … }; Array is not work here since it may be incomplete.

9 BST: Search 9

10 Running Time: O(h) 10

11 Tree Walks 11

12 InOrder Walk An InOrder traversal of a BST prints the elements in sorted order, in O(n) time. 12

13 Tree Walks 13

14 BST: Minimum/Maximum 14

15 BST: Successor Running Time: O(h) 15

16 BST: Insert Running Time: O(h) 16

17 Randomly built binary search trees Theorem: If we insert n random elements into an initially empty Binary Search Tree, then the average node depth is O(lgn) Assume: Trees are formed by insertions only All input permutations are equally likely 17

18 BST: Delete Running Time: O(h) 18

19 Deletion: leaf 19

20 Deletion: single child 20

21 Deletion: two children 21

22 Balanced Trees ⇒ all leaves have similar depth - O(lgn) 22

23 Balanced Trees: 2-3-4 Trees 2-nodes – as in a BST – hold 1 key and two children 3-nodes hold 2 keys and have 3 links to children 4-nodes hold 3 keys and have 4 links to children 23

24 Insert Do an unsuccessful search if search terminated at a 2-node, turn it into a 3-node if search terminated in a 3-node, turn it into a 4-node if search terminates at a 4-node, – split 4-node into one 2-node and one 3-node – pass one key back up to its parent 24

25 Insert - Splitting 4-nodes 25

26 Properties of n node 2-3-4 trees Property 1: Worst-case Search takes O(log n) time Property 2: Worst-case for Insert makes O(log n) splits Average-case Insert seems to be < 1 split 26

27 Balanced Trees: Red-Black Trees RB-tree => std::set, std::map STL 源码剖析 源码之前,了无秘密 天下大事,必作于细 27

28 Over! 28

29 29 Teach yourself data structures and algorithms in 24 hours

30 30


Download ppt "Data Structure & Algorithm 09 – Binary Search Tree JJCAO."

Similar presentations


Ads by Google