Trees ---- Soujanya.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Binary Search Trees Chapter 7 Objectives
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
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.
Data Structures Using C++1 Chapter 11 Binary Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
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.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Traversing a Binary Tree 10/23/ Traversing Binary Tree There are 3 ways of traversing a binary tree T having root R. 1. Preorder Traversing Steps:
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Starting at Binary Trees
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
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.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Discrete Mathematics Chapter 5 Trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
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.
Binary Search Trees Chapter 7 Objectives
Data Structure and Algorithms
Recursive Objects (Part 4)
Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Chapter 21: Binary Trees.
Abstract Data Structures
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Chapter 9 Binary Trees.
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Trees ---- Soujanya

Contents Binary Tree Terminology Traversing Binary Trees Binary Search Tree Time Complexity

Binary Tree Definition: A Binary Tree T is defined as a set of elements, called nodes, such that: (a) T is empty (called the null tree or empty tree), or (b) T contains a distinguished node R, called the root of T, and the remaining nodes of T form an ordered pair of disjoint binary trees T1 and T2.

Terminology Root N Path Left Child S1 S2 Right Child Edge Branch Leaf

Traversing of Binary Trees There are 3 standard ways of Traversing a binary tree T with root R. Pre Order: (1) Process the root R. (2) Traverse the left sub-tree of R in preorder (3) Traverse the right sub-tree of R in preorder

Contd… In Order: Post Order: (1) Traverse the left sub-tree of R in in order (2) Process the root R. (3) Traverse the right sub-tree of R in in order Post Order: (1) Traverse the left sub-tree of R in post order (2) Traverse the right sub-tree of R in post order (3) Process the root R.

Preorder Traversal Algorithm: Initially push NULL onto the stack and then set PTR:= ROOT. Then repeat the following steps until 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.

Contd.. (b) [Backtracking] Pop and assign to PTR the top element on Stack. If PTR=NULL, then return to Step (a); otherwise exit.

Inorder Traversal Algorithm: Initially push the NULL onto the STACK and then set PTR:=ROOT. Then repeat the following steps until NULL is popped from STACK. (a) Proceed down the left-most path rooted at PTR, pushing each node N onto STACK and stopping when a node N with no left child is pushed onto the STACK.

Contd.. (b) [Backtracking] Pop and process the nodes on STACK. If NULL is popped, then exit. IF a node N with a right child R(N) is processed, set PTR= R(N) (by assigning PTR:= RIGHT[PTR] and return to step (a). we emphasize that a node N is processed only when it is popped from STACK.

Postorder Traversal Algorithm: Initially push NULL onto the STACK and then set PTR:=Root. Then repeat the following steps until NULL is popped from the STACK. (a) Proceed down the left-most path rooted at PTR. At each node N of the path, push N onto the STACK and, if N has a right child R(N), push –R(N) onto STACK.

Contd.. (b) [Backtracking:] Pop and process positive nodes on STACK. If NULL is popped, then Exit. If a negative node is popped, that is, if PTR=-N for some node N, set PTR=N (by assigning PTR:=-PTR) and return to step (a). We emphasize that a node N is processed only when it is popped from STACK and it is positive.

Binary Search Tree Definition: A binary tree T is called a binary search tree (or binary sorted 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. Note: This property guarantees that the inorder traversal of T will yield a sorted listing of the elements of T.

Searching and Inserting In Binary Trees Suppose an ITEM of information is given. The following algorithm finds location of ITEM in the binary search tree T, or inserts ITEM as a new node in its appropriate place in the tree. (a) Compare ITEM with the root 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.

Contd.. (b) Repeat step (a) until one f the following occurs: (i) We meet a node N such that ITEM=N. In this case the 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.

Deleting in a Binary Search Tree Suppose an ITEM of information is to be deleted from the tree. The deletion algorithm first uses the search and finds the location of the node N which contains ITEM and also the location of the parent node P(N). The way N is deleted from the tree depends primarily on the number of children of node N.

Contd.. Case 1: N has no children Case 2: N has exactly one child Case 3: N has two children.