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]

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CS 171: Introduction to Computer Science II
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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)
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Tree Data Structures.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Types of Binary Trees Expression Tree A binary tree can store anything where the number ‘2’ comes into picture. In an arithmetic expression: So an arithmetic.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Search Trees Chapter 7 Objectives
Binary Tree ADT: Properties
AA Trees.
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Tree.
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

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] if lptr(T) != NULL call Rpreorder(lptr(T)) 3. [process the right subtree] if rptr(T) != NULL call Rpreorder(rptr(T)) 4. [Finished] return. 3

Iterative Postorder Traversal

Iterative Inorder Traversal

Types of Trees Expression tree Binary Search tree Heap tree Threaded binary tree Height balanced tree (AVL tree) B Tree

Expression Tree It is a binary tree which stores an arithmetic expression. The leaves of an expression tree are operands, such as constants or variable names, and all internal nodes are for operators. An expression tree is always a binary tree because an arithmetic expression contains either binary operators or unary operators (hence an internal node has at most two children).

Construction of Expression Tree Postfix expression: A B C * + D E * F + G / -

Binary Search Tree A binary tree T is termed binary search tree (or binary sorted tree) if each node N of T satisfies the following property: The value at N is greater than every value in the left sub-tree of N The value at N is less than every value in the right sub-tree of N Binary search tree operations: Searching Inserting Deleting Traversing

Searching in BST

Insertion in BST

Deletion in BST If N is leaf node N has exactly one child N has two children Case 1: N is deleted from T by simply setting the pointer of N in the parent node PARENT(N) by null value.

Deletion in BST Case 2: N is deleted from T by simply replacing the pointer of N in PARENT(N) by the pointer of the only child of N. Case 3: N is deleted from T by first deleting SUCC(N) from T (by using case1 or case2 it can be verified that SUCC(N) never has a child) and then replacing the data content in node N by the data content in node SUCC(N). Reset the left child of the parent of SUCC(N) by the right child of SUCC(N)

/*DECIDE THE CASE OF DELETION*/ IF(ptr->LCHILD=NULL) and (ptr- >RCHILD=NULL) then Case=1 ELSE IF(ptr->LCHILD != NULL) and (ptr->RCHILD != NULL) then Case=3 ELSE Case=2 EndIf /*DELETION CASE 1*/ IF(case=1) then If(parent->LCHILD = ptr) then Parent->LCHILD = NULL Parent->LCHILD = NULLELSE Parent->RCHILD = NULL EndIf Return Node(ptr) EndIf 14

/*DELETION Case 2*/ IF(case=2) then If(parent->LCHILD = ptr) then If(ptr->LCHILD=Null) then Parent->LCHILD = ptr->RCHILD ELSE Parent->LCHILD = ptr->LCHILD EndIf Else If(parent->RCHILD = ptr) then If(ptr->LCHILD=NULL) then Parent->RCHILD = ptr->RCHILD Else Parent->RCHILD = ptr->LCHILD EndIf ReturnNode(ptr) EndIf /*DELETION Case 3*/ If (case=3) Ptr=SUCC(ptr)Item1=ptr->DATADelete_BST(item1)Ptr->DATA=item1EndIfStopSUCC(ptr) Ptr1 = ptr-> RCHILD If (ptr1 != NULL) then while (Ptr1 -> LCHILD != NULL) ptr1 = ptr1-> LCHILD Return(ptr1) 15

Deletion in BST

Heap

Create Heap / Insertion in Heap

Deletion in Heap

Heap Sort

ANY QUESTIONS?? 21