TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Starting at Binary Trees
Data Structures TREES.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
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.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Tree.
Csc 2720 Instructor: Zhuojun Duan
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
Presentation transcript:

TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.

General Trees. Trees can be defined in two ways : Recursive Non- recursive A B CDE FG H I K J One natural way to define a tree is recursively

General trees-Definition A tree is a collection of nodes. The collection can be empty; otherwise a tree consists of a distinguish node r, called root, and zero or more non-empty (sub)trees T 1,T 2,T 3,….T K.. A B CDE F G H I K J Each of whose roots are connected by a directed edge from r.

General trees-Definition A tree consists of set of nodes and set of edges that connected pair of nodes. A B CDE FG H I K J

Eg. A table of contents and its tree representation Book C1 S1.1 S1.2 C2 S2.1 S2.1.1 S2.1.2 S2.2 S2.3 C3 Book C1C2 C3 S2.1S1.2S1.1S2.2 S2.3 S2.1.2S2.1.1

Degree The number of sub tree of a node is called its degree. Eg. Degree of book  3, C1  2,C3  0 Book C1C2 C3 S2.1S1.2S2.2 S2.3 S2.1.2S2.1.1 S1.1

Terminal Nodes and Non Terminal nodes Nodes that have degree 0 is called Leaf or Terminal node.Other nodes called non-terminal nodes. Eg.Leaf nodes :C3,S1.1,S1.2 etc. Book C1C2 C3 S2.1S1.2S2.2 S2.3 S2.1.2S2.1.1 S1.1

Parent, Children & Siblings Book is said to be the father (parent) of C1,C2,C3 and C1,C2,C3 are said to be sons (children ) of book. Children of the same parent are said to be siblings. Eg.C1,C2,C3 are siblings (Brothers) Book C1C2 C3 S2.1S1.2S2.2 S2.3 S2.1.2S2.1.1 S1.1

Length Book C1C2 C3 S2.1S1.2S2.2 S2.3 S2.1.2S2.1.1 The length of a path is one less than the number of nodes in the path.(Eg path from book to s1.1=3-1=2) S1.1

Ancestor & Descendent Book C1C2 C3 S2.1S1.2S2.2 S2.3 S2.1.2S2.1.1 If there is a path from node a to node b, then a is an ancestor of b and b is descendent of a. In above example, the ancestor of S2.1 are itself,C2 and book, while it descendent are itself, S2.1.1 and S S1.1

Height & Depth The height of a node in a tree is the length of a longest path from node to leaf.[ In above example node C1 has height 1, node C2 has height 2.etc. Depth : The depth of a tree is the maximum level of any leaf in the tree.[ In above example depth=3] Book C1C2 C3 S2.1S1.2S2.2 S2.3 S2.1.2S2.1.1 S1.1

Tree - Implementation : Keep the children of each node in a linked list of tree nodes. Thus each node keeps two references : one to its leftmost child and other one for its right sibling. LeftData Right

Left child -Right sibling representation of a tree A B C D E G F

A B C D F G

Data structure definition Class Treenode { Object element; Treenode leftchild; Treenode rightsibling; }

An application :File system There are many applications for trees. A popular one is the directory structure in many common operating systems, including VAX/VMX,Unix and DOS.

Binary trees A binary tree is a tree in which no nodes can have more than two children. The recursive definition is that a binary tree is either empty or consists of a root, a left tree, and a right tree. The left and right trees may themselves be empty; thus a node with one child could have a left or right child. We use the recursive definition several times in the design of binary tree algorithms.

One use of the binary tree is in the expression tree, which is central data structure in compiler design. - d b c + a * (a+((b-c)*d)) The leaves of an expression tree are operands, such as constant, variable names. The other nodes contain operators. Eg :

Tree traversal-iterate classes. The main tree traversal techniques are: Pre-order traversal In-order traversal Post-order traversal

Pre-order traversal To traverse a non-empty binary tree in pre-order (also known as depth first order), we perform the following operations.  Visit the root ( or print the root)  Traverse the left in pre-order (Recursive)  Traverse the right tree in pre-order (Recursive)

Pre-order traversal Pre-order list –1,2,3,5,8,9,6,10,4,  Visit the root ( or print the root)  Traverse the left in pre-order (Recursive)  Traverse the right tree in pre-order (Recursive)

In-order traversal Pre-order list –2,1,8,5,9,3,10,6,7,4 Traverse the left-subtree in in- order Visit the root Traverse the right-subtree in in- order.

post-order traversal Pre-order list –2,8,9,5,10,6,3,7,4,1 Traverse the left sub-tree in post-order Traverse the right sub-tree in post-order Visit the root

Properties of binary trees. If a binary tree contains m nodes at level L, then it contains at most 2m nodes at level L+1. A binary tree can contain at most 2 L nodes at L At level 0 B-tree can contain at most 1= 2 0 nodes At level 1 B-tree can contain at most 2= 2 1 nodes At level 2 B-tree can contain at most 4= 2 2 nodes At level L B-tree can contain at most  2 L nodes

Complete B-tree A complete B-tree of depth d is the B-tree that contains exactly 2 L nodes at each level between 0 and d ( or 2 d nodes at d) Complete B-tree Not a Complete B-tree

The total number of nodes (Tn) in a complete binary tree of depth d is 2 d+1 -1 Tn= ……2 d …..(1) 2Tn= …… 2 d+1 ….(2) (2)-(1)  Tn=2 d+1 -1

Threaded Binary Trees

Threaded Tree Example Amir Kamil8/8/

Threaded Tree Traversal We start at the leftmost node in the tree, print it, and follow its right thread If we follow a thread to the right, we output the node and continue to its right If we follow a link to the right, we go to the leftmost node, print it, and continue Amir Kamil8/8/0235

Threaded Tree Traversal Amir Kamil8/8/ Start at leftmost node, print it Output 1

Threaded Tree Traversal Amir Kamil8/8/ Follow thread to right, print node Output 1 3

Threaded Tree Traversal Amir Kamil8/8/ Follow link to right, go to leftmost node and print Output 1 3 5

Threaded Tree Traversal Amir Kamil8/8/ Follow thread to right, print node Output

Threaded Tree Traversal Amir Kamil8/8/ Follow link to right, go to leftmost node and print Output

Threaded Tree Traversal Amir Kamil8/8/ Follow thread to right, print node Output

Threaded Tree Traversal Amir Kamil8/8/ Follow link to right, go to leftmost node and print Output

Threaded Tree Traversal Amir Kamil8/8/ Follow thread to right, print node Output

Threaded Tree Traversal Amir Kamil8/8/ Follow link to right, go to leftmost node and print Output

Threaded Tree Modification We’re still wasting pointers, since half of our leafs’ pointers are still null We can add threads to the previous node in an inorder traversal as well, which we can use to traverse the tree backwards or even to do postorder traversals Amir Kamil8/8/0245

Threaded Tree Modification Amir Kamil8/8/