CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
A Binary Tree root leaf. A Binary Tree root leaf descendent of root parent of leaf.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 10 Search Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
Fundamentals of Python: From First Programs Through Data Structures
Week 10: Heap and Priority queue. Any feature here?
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Binary Trees Chapter 6.
Data Structures and Algorithms Session 13 Ver. 1.0 Objectives In this session, you will learn to: Store data in a tree Implement a binary tree Implement.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
COSC2007 Data Structures II
Tree.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Data Strcutures.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
CSC 211 Data Structures Lecture 13
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Starting at Binary Trees
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
NURUL HASLINDA NGAH BP-37 SEMESTER II 2013/2014.  At the end of this chapter you’ll be learnt about:  Binary tree  Binary search tree  Traversing.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Binary Tree.
Trees (Unit 7).
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Trees Binary Trees Extended Binary Trees. Tree A Tree consist of Nodes connected by Edges. Node is represented by Circle Edges as Lines or Arrows A Tree.
Data Structures and Design in Java © Rick Mercer
Trees.
Data Structures Binary Trees 1.
Trees ---- Soujanya.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Hashing Exercises.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Heap Chapter 9 Objectives Upon completion you will be able to:
Binary Trees, Binary Search Trees
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

CHAPTER 71 TREE

Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T 1,..., T n, where each of these sets is a tree. We call T 1,..., T n the sub trees of the root. If T contains a root R, then two trees T1 and T2 are called respectively the left and right sub tree of R. if T1 is non empty then its root is called left successor of R similarly if T2 is non empty then its root is called right successor of R. The diagram of binary tree is as fallows CHAPTER 72

Example K L J E F A C H B G D CHAPTER 7

Representation of Diagram The following tree T contain 11 nodes, represented from letter A to L. the node is root of tree. The left downward slanted line from a node N indicates a left successor of N and right downward slanted line from N indicates a right successor of n B is left successor and C is right successor of the node A. Any node N in a binary tree T has either 0, 1 or 2 successors. The nodes with no successor are called terminal node. Terminologies Suppose a node A in tree T with left successor S1 and right successor S2. then A is called the parent of S1 and S2. S1 is called the left child of A and S2 is called right child of A. Furthermore S1 and S2 are said to be siblings. A terminal node is called a leaf, and a path ending in a leaf is called a branch. CHAPTER 74

Each node in a binary tree T is assigned a level number, the root R of tree T is assigned the level number 0 and every other node is assigned a level number which is 1 more than the level number of its parent. The nodes which have same level number is said to belong with same generation. The depth of a tree T is the maximum number of nodes in a branch of T. Complete Binary Tree Any binary tree is said to be completed if they have at most two children. Accordingly one can show that level r of T can have at most 2 nodes. A tree is said to be complete if all its level except possibly the last have the maximum number of possible nodes, and if all the nodes at the last level appear as far as possible. The formula for parent, left and right children is Left children is = 2*k Right children is = 2*K+1 Parent of node is [N/2] CHAPTER 7 5

Extended Binary Tree. A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children. In such case, the nodes with 2 children are called internal nodes and the nodes with 0 children are called external nodes. Some times internal and external nodes distinguished by using circle for internal and square used for external. The term extended binary tree” comes from the following operation. Consider any binary tree T. then T may be converted into 2-tree by replacing each empty sub tree by a new node. Representation of Binary Tree in memory Let T be a binary tree. There are two ways of representing T in memory. The first and usual way is called the link representation of T and is analogous to the way linked lists are represented in memory. The second way which uses single array called the sequential representation of T. the main requirement of any representation of T is that one should have direct access to root R of T. CHAPTER 76

Linked representation of Binary tree. Consider a binary tree T. unless otherwise stated, T will be maintained in memory by mean of linked representation which uses three parallel array INFO, LEFT, and Right, and a pointer variable ROOT. For each node N of T will correspond to a location K such that (1) INFO[K] contain data at the node N. (2) LEFT[K] contain the location of the left child of node N. (3) RIGHT[K] contain the location of the right child of node N. Furthermore, ROOT will contain the location of the root R of T. if any sub tree is empty, then the corresponding pointer will contain the null value. CHAPTER 77

Sequential representation of Binary Tree. Suppose T is a binary tree that is completed or nearly completed. Then there is an efficient way of maintaining T in memory called the sequential representation of T. this representation uses only a single linear array TREE as follows : (a) The root R of T is stored in Tree[1] (b) If a node N occupies Tree [K], then its left child is stored in Tree [2*k] and its right child is stored in Tree [2*k+1] Again null is used to indicate an empty sub tree. In particular Tree[1] = NULL indicates that the tree is empty. CHAPTER 78

Traversing binary tree There are three standard ways of traversing a binary tree T with root R. these three algorithms called  Preorder  in order  Post order Preorder (a) Process the root R (b) Traverse the left sub tree of R in preorder (c) Traverse the right sub tree of R in preorder In order (a) Traverse the left sub tree of R in in order (b) Process the root R (c) Traverse the right sub tree of R in in order Post order (a) Traverse the left sub tree of R in post order (b) Traverse the right sub tree of R in post order (c) Process the root R CHAPTER 79

Each algorithm contains the same three steps, and that the left sub trees of R is always traversed before the right sub tree. The difference between the algorithms is the time at which root R is processed. Specifically in the “pre” algorithm the root R is processed before the sub tree are traversed; in the “in” algorithm the root R is processed between the traversal of the sub trees and in the “post” algorithm the root R is processed after the sub trees are traversed. These algorithms some times called node-left-right (NLR) traversal, the left-node- right (LNR) traversal and the left-right-node(LRN). Traversal algorithms using stacks Suppose a binary tree T is maintained in memory by some linked representation TREE(INFO, LEFT, RIGHT, ROOT) We can discuss these three algorithms one by one CHAPTER 710

Pre order traversal The pre order traversal algorithm use a variable PTR(pointer) which will contain the location of the node N currently being scanned. Example shown in fig where L(N) denotes the left child of node N and R(N) denotes the right child. The algorithm also uses an array STACK, which will hold the addresses of nodes for future processing. Algorithm Initially push NULL onto STACK and then set PTR:=ROOT. Then repeat the following steps until PTR=NULL or equivalently, while PTR ≠ NULL. (a) Proceed down the left-most path rooted at PTR, processing each node N on the path and pushing each right child R(N), if any, onto STACK. The traversing ends after a node N with no left child L(N) is processed.(thus PTR is updated using the assignment PTR:= LEFT[PTR], and the traversing stops when LEFT[PTR] = NULL) (b) [Backtracking.] pop and assign to POP the top element on STACK. If PTR ≠ NULL, then return to stop (a); otherwise Exit. Initial element NULL on STACK is used as a essential. CHAPTER 7 11

Binary search tree This is a data structure that enables one to search for and find an element with an average running time f(n) = O(log2 n). It also enables one to easily insert and delete elements. This structure contrasts with the following structures (a) Sorted linear array Here one can search for and find an element with a running time f(n) = O(log2 n), but its expensive to delete and insert elements. (b) Linked list. Here one can easily insert and delete elements, but its expensive to search for and find an element, since one must use a linear search with running time f(n) = O(n). Suppose T is a binary tree. Then T is called binary search tree if each node N of T has the following property: the value at N is greater than every value in the left sub tree of N and is less than every value in the right sub tree of N. Searching & Inserting in Binary search tree Suppose T is a binary search tree. The searching and inserting will be given by a single search and inserting algorithm. Traversing in T is the same as traversing in any binary tree; CHAPTER 7 12

Suppose an ITEM of information is given. The following algorithm finds the location of ITEM in the binary search tree T, or inserts ITEM as a new node in in its appropriate place in the tree. (a) Compare ITEM with the rot node N of the tree (i) If ITEM < N proceed to the left child of N (ii) If ITEM > N proceed to the right child of N (b) Repeat step (a) until one of the following occurs: (i) We meet a node N such that ITEM = N. in this case search is successful. (ii) We meet an empty sub tree, which indicates that the search is unsuccessful, and we insert ITEM in place of the empty sub tree. In other words, proceed from the root R down through the tree T until finding ITEM in T or inserting ITEM as a terminal node in T. CHAPTER 7 13

Complexity of the searching Algorithm Suppose we are searching for an item of information in a binary tree T. the number of comparison is bounded by the depth of tree. This comes from the fact that we proceed down a single path of the tree. Accordingly the running time of the search will be proportional to the depth of the tree. Suppose we are given n data items, A1,A An, and suppose the items are inserted in order into a binary search tree T. so searching required n permutation. Each such permutation will give rise to a corresponding tree. Accordingly, the average running time f(n) to search for an item in a binary tree T with n elements is proportional to log2n, that is, f(n) = O (log2n). Application of Binary searching Algorithm Consider a collection of n data items A1,A An,. Suppose we want to find and delete all duplicates in the collection. One straightforward way to do this is as follows: Algorithm : scan elements from A1 to An (from left to right) CHAPTER 7 14

(a) For each element Ak, compare Ak with A1,A Ak-1, that is compare Ak with those elements which precede Ak. (b) If Ak does occur among A1,A Ak-1, then delete Ak. After all elements have been scanned, there will be no duplicates. Deleting in a Binary Search Tree Suppose T is a binary search tree, and suppose an ITEM of information is given, here we discuss algorithm which delete item from the tree T. here first we find the location of node N which contains ITEM and also the location of the parent node P(N). The way N is deleted from the tree depends on the number of children of node N. there are three cases: Case 1: N has no children. Then N is deleted from T by simply replacing the location of N in the parent node P(N) by null pointer. CHAPTER 7 15

Case 2: N has exactly one child. Then N is deleted from T by simply replacing the location of N in P(N) by the location of the only child of N. Case 3: N has two children. Let S(N) denotes the in order successor of N. (the reader can verify that S(N) does not have left child.) Then N is deleted from T by first deleting S(N) from T( by using case 1, or case 2) and then replacing the node N in T by the node S(N). The third case is more difficult than the first two cases. In all cases, the memory space of the deleted node N is returned to the AVAIL list. Heap; HEAPSORT Heap is used in an elegant sorting algorithm called heap sort. Suppose H is a complete binary tree with n elements.( we assume H is maintained in memory by a linear array tree using sequential representation of H). Then H is called a heap, or max heap, if each node N of H has the following property : the value at N is greater than or equal to the value at each of the children of N. Accordingly, the value at N is greater than or CHAPTER 7 16

Equal to the value at any of the descendants of N. ( A min heap is defined analogously; the value at N is less than or equal to the value at any of the children of N). Inserting into Heap. Suppose H is a heap with N elements, and suppose an ITEM of information is given. We insert ITEM into the heap H as follows: (1) First adjoin ITEM at the end of H so that H is still a complete tree, but not necessarily a heap. (2) Then let ITEM rise to its “appropriate place” in H so that H is finally a heap. Deleting the root of a Heap. Suppose H is a heap with N elements, and suppose we want to delete the root R of H. this is accomplished as follows: (1) Assign the root R to some variable ITEM. (2) Replace the deleted node R by the last node L of H so that H is still a complete tree CHAPTER 7 17

But not necessarily a heap. (3) (Re heap) let L sink to its appropriate place in H so that H is finally a heap. Again we illustrate the way the procedure works before starting the procedure formally. General Trees. A general tree is defined to be a nonempty finite set T of elements, called nodes such that; (1) T contains a distinguished element R, called the root of T. (2) The remaining elements of T form an ordered collection of zero or more disjoint trees T1,T2……., Tm. The trees T1,T2……., Tm are called sub trees of the root R, and the roots of T1,T2……., Tm. Are called successors of R. CHAPTER 7 18