Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Trees Lecturer: Abbas Sarraf ([email protected]) Introduction level A B E K 1 C F L G D H M Figure 5.2 : A sample tree Dept. of Computer Eng. & IT, Amirkabir University of Technology I 2 J 3 4 List Representation A 0 B E F 0 C K L 0 G0 I D H M0 Figure 5.3 : List representation of the tree of Figure 5.2 Dept. of Computer Eng. & IT, Amirkabir University of Technology J 0 Left child-right sibling representation Left child-right sibling representation node structure data left child right sibling the left child field of each node points to its leftmost child (if any), and the right sibling field points to its closest right sibling (if any) A B E K L Dept. of Computer Eng. & IT, Amirkabir University of Technology F D C G H M I J Representation as a degree-two tree rotate the right-sibling pointers clockwise by 45 degrees the two children of a node are referred to as the left and right children A A B B B C E K F D G L H M I J Figure 5.7 : Left child-right child tree representation Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Trees level A B B C D D 1 A H 2 C E F G 3 4 I E 5 (a) (b) Figure 5.10 : Skewed and complete binary trees Dept. of Computer Eng. & IT, Amirkabir University of Technology Properties of Binary Trees Lemma 5.2 [Maximum number of nodes] (1) The max number of nodes on level i of a binary tree is 2i-1, i≥ 1 (2) The max number of nodes in a binary tree of depth k is 2k-1, k≤ 1 A full binary tree of depth k a binary tree of depth k having 2k-1 nodes, k≥ 0 Dept. of Computer Eng. & IT, Amirkabir University of Technology 1 2 4 8 3 5 9 10 11 6 12 7 13 14 15 Figure 5.11 : Full binary tree of depth 4 with sequential node numbers Dept. of Computer Eng. & IT, Amirkabir University of Technology A binary tree with n nodes and depth k is complete its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k The height of a complete binary tree with n nodes is log 2 (n 1) Dept. of Computer Eng. & IT, Amirkabir University of Technology data LeftChild data RightChild LeftChild Figure 5.13 : Node representations Dept. of Computer Eng. & IT, Amirkabir University of Technology RightChild root A A B D H B C E F G I D 0 H 0 C E 0 0 F 0 0 G 0 0 I 0 (b) Figure 5.14 : Linked representation for the binary trees of Figure 5.10 Dept. of Computer Eng. & IT, Amirkabir University of Technology 5.3.1 Introduction Tree traversal visiting each node in the tree exactly once a full traversal produces a linear order for the nodes Order of node visit L : move left V : visit node R : move right possible combinations : LVR, LRV, VLR, VRL, RVL, RLV traverse left before right LVR : inorder LRV : postorder VLR : preorder Dept. of Computer Eng. & IT, Amirkabir University of Technology Constructing the original tree from inorder/preorder Constructing the original tree from inorder/postorder Constructing the original tree from preorder/postorder Dept. of Computer Eng. & IT, Amirkabir University of Technology 5.6 Heaps(Cont’) 14 9 12 10 7 8 6 30 3 25 5 6 Figure 5.24 : Max heaps 2 7 10 4 8 11 10 20 6 83 50 Figure 5.25 : Min heaps Dept. of Computer Eng. & IT, Amirkabir University of Technology 21 Heaps 5.6.3 Insertion into Max Heap Examples 20 15 2 10 14 (a) (b) 5 20 21 15 14 15 10 2 (c) 14 20 10 2 (d) Figure 5.26 : Insertion into a max heap Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont’) Implementation need to move from child to parent heap is complete binary tree use formula-based representation Lemma 5.4 : parent(i) is at i 2if i≠ 1 Complexity is O(log n) Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont’) template <class Type> void MaxHeap<Type>::Insert(const Element<Type> &x) // insert x into the max heap { if (n = = MaxSize) {HeapFull(); return;} n++; for (int i = n; 1; ) { if (i = = 1) break; // at root if (x.key <= heap[i/2].key) break; // move from parent to i heap[i] = heap[i/2]; i /= 2; } heap[i] = x; } Program 5.18 : Insertion into a max heap Dept. of Computer Eng. & IT, Amirkabir University of Technology Heaps(Cont’) Deletion from Max Heap Example 2 15 15 14 20 10 14 2 10 6 (a) (b) Figure 5.27 : Deletion from a heap Dept. of Computer Eng. & IT, Amirkabir University of Technology (c) Heaps(Cont’) template <class Type> Element<Type> *MaxHeap<Type>::DeleteMax(Element<Type> &x) // Delete from the max heap { if (!n) {HeapEmpty(); return 0;} x = heap[1]; Element<Type> k = heap[n]; n--; for (int i = 1, j = 2; j <= n; ) { if (j<n) if (heap[j].key < heap[j+1].key) j++; // j points to the larger child if (k.key >= heap[j].key) break; heap[i] = heap[j]; // move child up i = j; j *= 2; // move i and j down } heap[i] = k; return &x; } Program 5.19 : Deletion from a max heap Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees Definition Definition binary tree which may be empty if not empty (1) every element has a distinct key (2) keys in left subtree < root key (3) keys in right subtree > root key (4) left and right subtrees are also binary search trees Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont’) 20 15 12 30 25 5 22 10 (a) 40 2 70 65 (b) Figure 5.28 : Binary trees Dept. of Computer Eng. & IT, Amirkabir University of Technology 60 80 (c) Binary Search Trees(Cont’) Searching Binary Search Tree Recursive search by key value definition of binary search tree is recursive key(element) = x x=root key : element=root x<root key : search left subtree x>root key : search right subtree Complexity : O(h) Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont’) Insertion into Binary Search Tree New element x search x in the tree success : x is in the tree fail : insert x at the point the search terminated 30 30 5 40 2 5 80 (a) Insert 80 2 40 35 (b) Insert 35 Figure 5.29 : Inserting into a binary search tree Dept. of Computer Eng. & IT, Amirkabir University of Technology 80 Binary Search Trees(Cont’) template <class Type> Boolean BST<Type>::Insert(const Element<Type> &x) // Insert x into the binary search tree { // Search for x.key, q is parent of p BstNode<Type> *p = root; BstNode<Type> *q = 0; while(p) { q = p; if (x.key = = p->data.key) return FALSE; // x.key is already in tree if (x.key < p->data.key) p = p->LeftChild; else p = p->RightChild; } // Perform insertion p = new BstNode<Type>; p->LeftChild = p->RightChild = 0; p->data = x; if (!root) root = p; else if (x.key < q->data.key) q->LeftChild = p; else q->RightChild = p; return TRUE; } Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont’) Deletion from Binary Search Tree Leaf node corresponding child field of its parent is set to 0 the node is disposed Nonleaf node with one child the node is disposed child takes the place of the node Nonleaf node with two children node is replaced by either the largest node in its left subtree the smallest node in its right subtree delete the replacing node from the subtree Dept. of Computer Eng. & IT, Amirkabir University of Technology Binary Search Trees(Cont’) Dept. of Computer Eng. & IT, Amirkabir University of Technology