240 likes | 252 Views
Cairo University. Faculty of Computers and Information. Data Structures CS 214 2 nd Term 2012-2013. Binary Search Trees. Chapter 6 in Adam Drozdek. Agenda. Introduction to Binary Trees Implementing Binary Trees Searching Binary Search Trees
E N D
Cairo University Faculty of Computers and Information Data Structures CS 214 2nd Term 2012-2013 Binary Search Trees Chapter 6 in Adam Drozdek
Agenda Introduction to Binary Trees Implementing Binary Trees Searching Binary Search Trees Tree Traversal …1. Breadth-First ….2. Depth-First Insertion ……1. Deletion by Merging …..2. By Copying ……2. AVL Trees . Heaps / Heap Sort .
جاء فيما كتبه الشيخ علي الطنطاوي - رحمه الله تعالى - قصة إمرأة كان لها ولد مسافر للدراسة وكانت إمرأة فقيرة لكنها جوادة كريمة محبة لله وللخير ، فبينما هي ذات يوم على عشائها الذي لا تملك غيره إذا بطارق يطرق عليها الباب ففتحته فإذا هو مسكين يسأل طعاماً . فقامت إلى عشائها فأعطته إياه . وذهب هو ليشبع وباتت هي جائعة لكن محتسبة عند الله الأجر ، ألم الجوع في بطنها لكن فرحة السعادة في قلبها أن سدت جوعة مسكين. ومضت الأيام والليالي وقدم ابنها من سفره وأخذ يحدثها عن سفره فذكر لها من أعجب ما حدث له أن أسداً اعتدى عليه في إحدى الغابات حتى صار بين يديه و جره الأسد لكهف ليفترسه وبينما يهم بذلك جاء رجل عليه ثياب بيض فركل الأسد ركلة أطاحت به وأنقذ الابن. فسأله : من أنت ؟ قال : لقمة بلقمة !! فتعجبت ماذا يريد بهذا الكلام ! فسألته أمه متى حدث هذا الكلام ، فأخبرها فإذا هو نفس اليوم الذي سدت به لقمة ذلك الجائع ، لقمة الجائع أنقذت ولدها أن يكون لقمة لأسد مفترس .فيا سبحان الله صنائع المعروف تقي مصارع السوء . فلا تنس هذا البنك العظيم أن تودع فيه من مالك فإنه يعطي من الفوائد و الأرباح ما لا يعطيها غيره . لقمة بلقمة
6.1 Trees Linked Lists are limited, why? How can I organize a hierarchical structure (of a university for example)
Tree Definitions A tree data structure consists of nodes and arcs. Every node has one parent but may have one or more children. Top node is root. Root has no parent. Nodes with no children are called leaves.
Tree Definitions Each node is reachable from the root with a unique sequence of arcs called path. The number of arcs in a path is the length of the path. The level of a node is the length of the path from the root to the node + 1. The height of a non-empty tree is the maximum level of a node in the tree.
Tree Definitions The height of an empty tree is zero. The height of a single node tree is 1. A single node tree is the only case when the root is also a leaf. The level of any node is between 1 and the tree height. In the extreme case, when every node has one child, how will the tree look like?
Binary Trees • A binary tree is a tree whose nodes have at most two children per node. • A complete binary tree is a binary tree that satisfies the following conditions: • Every non-terminal nodes has 2 children • Leaves are all at the same level • In a complete binary tree, level i+1has 2i nodes.
Complete Binary Trees The number of leaves m = k + 1, where k is the number of non-terminal nodes. Level i+1has 2i nodes and is preceded by 2i-1 non-terminal nodes. So, a complete binary tree has 2i + 2i-1 nodes, i.e., 2i+1-1 nodes.
Binary Search Trees • A binary search tree is a binary tree that satisfies the following conditions: • Every node n in its left sub-tree has a value less than the value stored in n. • Every node n in its right sub-tree has a value greater than the value stored in n.
6.2 Implementing Binary Trees • How can we implement binary trees? • Arrays? • Advantages? • Limitations? • Linked Structures?
Building Binary Trees w Nodes (A few comments on style) private Chapter 6: Binary Trees
template <class T> class BSTNode { private: T key; BSTNode* left; BSTNode* right; public: BSTNode () {left = right = 0;} BSTNode (T& el, BSTNode* l = 0, BSTNode* r = 0){ key = el; left = l; right = r; } BSTNode* getLeft {return left;} BSTNode* getRight {return right;} T getKey {return key;} }
template <class T> class BST { protected: BSTNode<T>* root public: BST () {root = 0;} void clear() {root = 0;} bool isEmpty() {retun root == 0;} T* search (T& el) { BSTNode<T>* p = root; while (p != 0) if (el == p->getKey()) return &p->getKey(); else if (el < p->getKey()) p = -> getLeft(); else p = getRight(); return 0; } }
6.3 Searching in a BST Trace searching for 1, 12, 31 What is the max num of tests when searching in this tee? 3. What is the max num of tests when searching in this tee?
Complexity of Searching in a BST Measured by the number of comparisons doen, which depends on the nodes encountered on the path from the root to the node searched for. So, complexity is the length of the path leading to this node +1; Internal path length IPL is the sum of all path lengths of all nodes = (i-1)li for all levels I where li is the number of nodes in level i. Average path length = IPL / n.
Complexity of Searching in a BST • What is the best case? (proof in book) • When tree is complete except at the lower level (All leaves are in the last two levels, and all non-terminal nodes have 2 children) • For a complete tree with h levels: • But • . • O(log n)
Complexity of Searching in a BST • What is the best case? (El-Ramly’s proof) • Best case is a complete full tree with n nodes • Assume root is level 1 and last level is log n • Num of comparison needed to search for each node is shown on the node. • So at level i, sum of all • comparisons needed • to reach nodes • is i 2i-1 1 2 3 3 3 4 4 4 4 4 4 4 4
Complexity of Searching in a BST • What is the best case? (El-Ramly’s proof) • When z = 2, then this sum is 2 + (n-1) 2n+1 • Total number of comparisons to search for all nodes in the tree is i 2i-1 , for i = 1 to log n • Avg num of comparisons to reach a node in the tree is ( i 2i) / 2n, for i = 1 to log n
Complexity of Searching in a BST • What is the best case? (El-Ramly’s proof) • Avg num of comparisons to reach a node in the tree is ( i 2i) / 2n, for i = 1 to log n • Avg # = (2 + (log n - 1) 2log n+1 ) / 2 n • Avg # = (2 + (log n - 1) 2log n+1 ) / 2 x 2 log n • Avg # = 1/ 2 log n + log n - 1 • lim (1/ 2 log n + log n - 1 ) / log n = 1 • n • Avg # O(log n)
Complexity of Searching in a BST • What is the worst case? • . • O(n) • 3. What is the average case? • O(1.386 log n), i.e., O(log n) • On average, searching is closer to best case than to worst for random data, even without balancing.