Tree is a collection of nodes in which there is a root node and all other nodes are recursively children

Similar documents
Algorithms Chapter 12 Binary Search Trees

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Introduction to Data Structures and Algorithms

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees CMPSC 122

Ordered Lists and Binary Trees

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Symbol Tables. Introduction

TREE BASIC TERMINOLOGIES

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Chapter 14 The Binary Search Tree

Data Structures and Algorithms

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Introduction to Data Structures and Algorithms

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Converting a Number from Decimal to Binary

Lecture 2 February 12, 2003

Data Structure [Question Bank]

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

Binary Search Trees (BST)

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Trees and Huffman Encoding Binary Search Trees

DATA STRUCTURES USING C

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Binary Search Trees. Adnan Aziz. Heaps can perform extract-max, insert efficiently O(log n) worst case

Algorithms and Data Structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Rotation Operation for Binary Search Trees Idea:

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structure with C

A Comparison of Dictionary Implementations

10CS35: Data Structures Using C

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

CSE 326: Data Structures B-Trees and B+ Trees

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Persistent Binary Search Trees

How To Create A Tree From A Tree In Runtime (For A Tree)

Data Structures. Level 6 C Module Descriptor

The ADT Binary Search Tree

From Last Time: Remove (Delete) Operation

Lecture Notes on Binary Search Trees

DATABASE DESIGN - 1DL400

Laboratory Module 6 Red-Black Trees

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Heap Algorithms

PES Institute of Technology-BSC QUESTION BANK

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Binary Heaps. CSE 373 Data Structures

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Lecture Notes on Binary Search Trees

Binary Search Trees. Ric Glassey

Data Structures Fibonacci Heaps, Amortized Analysis

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Binary Search Trees 3/20/14

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Any two nodes which are connected by an edge in a graph are called adjacent node.

Heaps & Priority Queues in the C++ STL 2-3 Trees

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Physical Data Organization

External Memory Geometric Data Structures

Cpt S 223. School of EECS, WSU

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Exam study sheet for CS2711. List of topics

APP INVENTOR. Test Review

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

International Journal of Advanced Research in Computer Science and Software Engineering

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Chapter 13: Query Processing. Basic Steps in Query Processing

Alex. Adam Agnes Allen Arthur

Algorithms and Data Structures

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Section IV.1: Recursive Algorithms and Recursion Trees

An Evaluation of Self-adjusting Binary Search Tree Techniques

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

6 March Array Implementation of Binary Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Data Structures UNIT III. Model Question Answer

What Is Recursion? Recursion. Binary search example postponed to end of lecture

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Chapter 8: Bags and Sets

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Vector storage and access; algorithms in GIS. This is lecture 6

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Transcription:

Tree Tree is a collection of nodes in which there is a root node and all other nodes are recursively children of this root node. What does it mean is that root node has some child nodes, those child nodes have their child nodes and so on. We represent each node of a tree by an object. As with linked lists, we assume that each node contains a key field. The remaining fields of interest are pointers to other nodes, and they vary according to the type of tree. Binary Tree A tree in which each node can have at max only two children is called Binary tree. Binary Search Tree is binary tree. We shall discuss more about BST later.we use the fields parent, leftc, and rightc to store pointers to the parent, left child, and right child of each node in a binary tree T. If parent[node] = NIL, then 'node' is the root. If 'node' has no left child, then leftc[node] = NIL, and similarly for the right child. The root of the entire tree T is pointed to by the attribute root[t]. If root[t] = NIL, then the tree is empty. Rooted trees with unbounded branching The scheme for representing a binary tree can be extended to any class of trees in which the number of children of each node is at most some constant k: we replace the left and righ t fields by child1, child2,..., childk. This scheme no longer works when the number of children of a node is unbounded, since we do not know how many fields (arrays in the multiple -array representation) to allocate in advance. Moreover, even if the number of children k is bounded by a large constant but most nodes have a small number of children, we may waste a lot of memory.

Fortunately, there is a clever scheme for using binary trees to represent trees with arbitrary numbers of children. It has the advantage of using only O(n) space for any n-node rooted tree. The left-child, right-sibling representation is shown below. As before, each node contains a parent pointer parent, and root[t] points to the root of tree T. Instead of having a pointer to each of i ts children, however, each node x has only two pointers: The left-child, right-sibling representation of a tree T. Each node x has fields parent[x], left -child[x], and right- ibling[x]. 1. left-child[x] points to the leftmost child of node x, and 2. right-sibling[x] points to the sibling of x immediately to the right. If node x has no children, then left-child[x] = NIL, and if node x is the rightmost child of its parent, then right -sibling[x] = NIL. Binary Search Tree Search trees are data structures that support many dynamic -set operations, including SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT, and DELETE. Thus, a search tree can be used both as a dictionary and as a priority queue. Basic operations on a binary search tree take time proportional to the height of the tree. For a complete binary tree with n nodes, such operations run in Θ(lg n) worst-case time. If the tree is a linear chain of n nodes, however, the same operations take Θ(n) worst-case time. The expected height of a randomly built binary search tree is O(lg n),so that basic dynamic-set operations on such a tree take Θ(lg n) time on average. In practice, we can t always guarantee that binary search trees are built randomly, but there are variations of binary search trees whose worst-case performance on basic operations can be guaranteed to be

good. One such variation is red-black trees, which have height O(lg n). Another example of variation is B-trees, which are particularly good for maintaining databases on random-access, secondary (disk) storage. What is a binary search tree? A binary search tree is organized, as the name suggests, in a binary tree, as shown in below figure. Such a tree can be represented by a linked data structure in which each node is an object. In addition to a key field and satellite data, each node contains fields left, right, and p that point to the nodes corresponding to its left child, its right child, and its parent, respectively. If a child or the parent is missing, the appropriate field contains the value NIL. The root node is the only node in the tree whose parent field is NIL. The keys in a binary search tree are always stored in such a way as to satisfy the binary search -tree property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] key[x]. If y is a node in the right subtree of x, then key[x] key[y]. The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. This algorithm is so named because the key of the root of a subtree is printed between the values in its left subtree and those in its right subtree. (Similarly, a preorder tree walk prints the root before the values in either subtree, and a postorder tree walk prints the root after the values in its subtrees.) To use the following procedure to print all the elements in a binary search tree T, we call INORDER -TREE-WALK (root[t]).

INORDER-TREE-WALK(x) 1. if x NIL 2. then INORDER-TREE-WALK(left[x]) 3. print key[x] 4. INORDER-TREE-WALK(right[x]) Querying a binary search tree A common operation performed on a binary search tree is searching for a key stored in the tre e. Besides the SEARCH operation, binary search trees can support such queries as MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR. In this section, we shall examine these operations and show that each can be supported in time O(h) on a binary search tree of he ight h. Searching We use the following procedure to search for a node with a given key in a binary search tree. Given a pointer to the root of the tree and a key k, TREE-SEARCH returns a pointer to a node with key k if one exists; otherwise, it returns NIL. TREE-SEARCH (x, k) 1. if x = NIL or k = key[x] 2. then return x 3. if k < key[x] 4. then return TREE-SEARCH(left[x], k) 5. else return TREE-SEARCH(right[x], k) The procedure begins its search at the root and traces a path d ownward in the tree. For each node x it encounters, it compares the key k with key[x]. If the two keys are equal, the search terminates. If k is smaller than key[x], the search continues in the left subtree of x, since the binary -search-tree property implies that k could not be stored in the right subtree. Symmetrically, if k is larger than key[x], the search continues in the right subtree. The nodes encountered during the recursion form a path downward from the root of the tree, and thus the running time o f TREE-SEARCH is O(h), where h is the height of the tree. The same procedure can be written iteratively by "unrolling" the recursion into a while loop. On most computers, this version is more efficient. ITERATIVE-TREE-SEARCH(x, k) 1. while x NIL and k key[x] 2. do if k < key[x] 3. then x left[x] 4. else x right[x] 5. return x

Minimum and maximum An element in a binary search tree whose key is a minimum can always be found by following left child pointers from the root until a NIL is encountered. The following procedure returns a pointer to the minimum element in the subtree rooted at a given node x. TREE-MINIMUM (x) 1. while left[x] NIL 2. do x left[x] 3. return x The binary-search-tree property guarantees that TREE-MINIMUM is correct. If a node x has no left subtree, then since every key in the right subtree of x is at least as large as key[x], the minimum key in the subtree rooted at x is key[x]. If node x has a left subtree, then since no key in the right subtree is smaller than key[x] and every key in the left subtree is not larger than key[x], the minimum key in the subtree rooted at x can be found in the subtree rooted at left[x]. The pseudocode for TREE-MAXIMUM is symmetric. TREE-MAXIMUM(x) 1. while right[x] NIL 2. do x right[x] 3. return x Both of these procedures run in O(h) time on a tree of height h since, as in TREE-SEARCH, the sequence of nodes encountered forms a path downward from the root. Successor and predecessor Given a node in a binary search tree, it is sometimes important to be able to find its successor in the sorted order determined by an inorder tree walk. If all keys are distinct, the successor of a node x is the node with the smallest key greater than key[x]. The structure of a binary search tree allows us to determine the successor of a node without ever comparing k eys. The following procedure returns the successor of a node x in a binary search tree if it exists, and NIL if x has the largest key in the tree. TREE-SUCCESSOR(x) 1. if right[x] NIL 2. then return TREE-MINIMUM (right[x]) 3. y p[x] 4. while y NIL and x = right[y] 5. do x y 6. y p[y]

7. return y The code for TREE-SUCCESSOR is broken into two cases. If the right subtree of node x is nonempty, then the successor of x is just the leftmost node in the right su btree, which is found in second line by calling TREE-MINIMUM(right[x]). On the other hand, if the right subtree of node x is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x. To find y, we simply g o up the tree from x until we encounter a node that is the left child of its parent; this is accomplished by 3rd to 7th lines of TREE-SUCCESSOR. The running time of TREE-SUCCESSOR on a tree of height h is O(h), since we either follow a path up the tree or follow a path down the tree. The procedure TREE- PREDECESSOR, which is symmetric to TREE-SUCCESSOR, also runs in time O(h). Insertion and deletion The operations of insertion and deletion cause the dynamic set represented by a binary search tree to change. The data structure must be modified to reflect this change, but in such a way that the binary-search-tree property continues to hold. As we shall see, modifying the tree to insert a new element is relatively straight-forward, but handling deletion is somewhat more intricate. Insertion To insert a new value v into a binary search tree T, we use the procedure TREE -INSERT. The procedure is passed a node z for which key[z] = v, left[z] = NIL, and right[z] = NIL. It modifies T and some of the fields of z in such a way that z is inserted into an appropriate position in the tree. TREE-INSERT(T, z) 1 y NIL 2 x root[t] 3 while x NIL 4 do y x 5 if key[z] < key[x] 6 then x left[x] 7 else x right[x] 8 p[z] y 9 if y = NIL 10 then root[t] z // Tree T was empty 11 else if key[z] < key[y] 12 then left[y] z 13 else right[y] z Just like the procedures TREE-SEARCH and ITERATIVE-TREE-SEARCH, TREE-INSERT begins at the root of the tree and traces a path downward. The pointer x traces the path, and the pointer y is

maintained as the parent of x. After initialization, the while loop in lines 3-7 causes these two pointers to move down the tree, going left or right depending on the comparison of key[z] with key[x], until x is set to NIL. This NIL occupies the position where we wish to place the input item z. Lines 8-13 set the pointers that cause z to be inserted. Like the other primitive operations on search trees, the procedure TREE-INSERT runs in O(h) time on a tree of height h. Deletion The procedure for deleting a given node z from a binary search tree takes as an argument a pointer to z. The procedure considers the three cases. If z has no children, we modify its parent p[z] to replace z with NIL as its child. If the node has only a single child, we "splice out" z by making a new link between its child and its parent. Finally, if the node has two children, we splice out z's successor y, which has no left child and replace z's key and satellite data with y's key and satellite data. TREE-DELETE(T, z) 1. if left[z] = NIL or right[z] = NIL 2. then y z 3. else y TREE-SUCCESSOR(z) 4. if left[y] NIL 5. then x left[y] 6. else x right[y] 7. if x NIL 8. then p[x] p[y] 9. if p[y] = NIL 10. then root[t] x 11. else if y = left[p[y]] 12. then left[p[y]] x 13. else right[p[y]] x 14. if y z 15. then key[z] key[y] 16. copy y's satellite data into z 17. return y In lines 1-3, the algorithm determines a node y to splice out. The node y is either the input node z (if z has at most 1 child) or the successor of z (if z has two children). Then, in lines 4-6, x is set to the non-nil child of y, or to NIL if y has no children. The node y is spliced out in lines 7-13 by modifying pointers in p[y] and x. Splicing out y is somewhat complicated by the need for proper handling of the boundary conditions, which occur when x = NIL or when y is the root. Finally, in lines 14-16, if the successor of z was the node spliced out, y's key and satellite data are moved to z, overwriting the previous key and satellite data. The node y is returned in line 17 so that the calling procedure can recycle it via the free list. The procedure runs in O( h) time on a tree of height h.

Source: http://www.learnalgorithms.in/#