Data Structures & Algorithm Design

Slides:



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

Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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 ),
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,
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.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Search Trees Data Structures Ananda Gunawardena
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.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Discrete Mathematics Chapter 5 Trees.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
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 CMSC 341 Introduction to Trees Textbook sections:
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
The Tree ADT.
Data Structures and Design in Java © Rick Mercer
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees.
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Trees.
Lecture 18. Basics and types of Trees
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Data Structures & Algorithm Design Trees Mais S. Al-Saoud Computer Science Department Karballa University March 2015

Tree Definition Tree: a set of elements of the same type such that it has a distinguished element called the root from which descend zero or more trees (subtrees) Examples in real life: Family tree Table of contents of a book Recursive! 2

Tree Terminology Nodes: the elements in the tree Edges: connections between nodes Root: the distinguished element that is the origin of the tree There is only one root node in a tree Leaf node: a node without an edge to another node Interior node: a node that is not a leaf node Empty tree has no nodes and no edges

Tree Terminology Parent or predecessor: the node directly above in the hierarchy A node can have only one parent Child or successor: a node directly below in the hierarchy Ancestors of a node: its parent, the parent of its parent, etc. Descendants of a node: its children, the children of its children, etc.

Tree Terminology Subtree of a node: consists of a child node and all its descendants A subtree is itself a tree A node may have many subtrees If A is the root of a binary tree and B is the root of its left or right subtrees, then A is said to be the father of B and B is said to be the left/right son of A. Two nodes are brothers if they are left and right sons of the same father.

Tree Terminology Root Subtrees of the root

Subtrees E Subtrees of the node labeled E

Tree Terminology Root Interior nodes Leaf nodes

Height of a Tree A path is a sequence of edges leading from one node to another Length of a path: number of edges on the path Height of a (non-empty) tree : length of the longest path from the root to a leaf (maximum level of any node) What is the height of a tree that has only a root node? By convention, the height of an empty tree is -1 9

Level and Degree of a Node Level of a node : number of edges between root and node Level of root node is 0 Level of a node that is not the root node is level of its parent + 1 Degree of a node: the number of children it has Degree of a tree: the maximum of the degrees of the tree’s nodes

Level of a Node Level 0 Level 1 Level 2 Level 3

Binary Trees General tree: a tree each of whose nodes may have any number of children n-ary tree: a tree each of whose nodes may have no more than n children Binary tree: a tree each of whose nodes may have no more than 2 children a binary tree is a tree with degree 2

Binary Trees Tree with 0–2 children per node The children (if present) are called the left child and right child a tree where every node has left and right subtrees is a binary tree

Binary Trees Strictly Binary Tree Complete Binary Tree All non-leaf nodes have two branches Complete Binary Tree All its leaf nodes at the same level Almost Complete Binary Tree The last level is partially filled from left to right.

Binary Trees A B C D E F G H I J K Not Complete BT, Strictly BT, Almost complete BT

Binary Tree Properties The number of nodes n in a complete binary tree is at least n = 2h and at most n = 2(h + 1) − 1 where h is the height of the tree. The number of leaf nodes L in a complete binary tree can be found using this formula: L = 2h where h is the height of the tree. The number of nodes n in a perfect binary tree can also be found using this formula: n = 2L − 1 where L is the number of leaf nodes in the tree.

Binary Tree Properties-Example

Binary Search Tree Binary Search Tree is a binary tree in which nodes are arranged according to their values. The left node has a value less than its parent The right node has a values greater than its parent

Binary Search Tree Construction How to build & maintain binary trees? Insertion Deletion Maintain key property Smaller values in left subtree Larger values in right subtree

Declaration BT struct node { A node consists of three fields such as : Left Child (LChild) Information of the Node (Info) Right Child (RChild) struct node { int info; struct node *left; struct node *right; };

Binary Search Tree – Insertion Perform comparison for value X Comparison will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y If X > Y, insert new leaf X as new right subtree for Y

Not a binary search tree Insertion- Examples 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Binary search trees Not a binary search tree

Insertion- Example Insert ( 20 ) 10 20 >10, right 20 < 30, left Insert 20 as left leaf 5 30 2 25 45 20

Binary Search Tree Given the following sequence of numbers, 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 The following binary search tree can be constructed.

Algorithm for insert node in BST Steps processing (by Recursion) 1.check if tree not contain any node if Root = NULL create node in tree 2.Else if x< Root  Inf Add the new value in the left subtree Rootleft = insert(Rootleft, x); 3.Else Add the new value in the right subtree Rootright = insert(Rootright, x);

Insertion of node in BST node * Insert(node *p, int x) { if(p==NULL) { p=make(x); return p; } else { if(x<p->inf) p->left=Insert(p->left, x); else p->right=Insert(p->right,x); return p; } }

Traversing : Means visiting all the nodes of the tree, in some specific order for processing. There are THREE ways of Traversing a Tree 1. Preoder Traversal 2. Inorder Traversal 3. Postorder Traversal 4. Levelorder Traversal

Preorder Traversal Method Traversing a binary tree in preorder (Root – Left – Right) 1. Visit the root. 2. Traverse the left subtree in preorder. 3. Traverse the right subtree in preorder.

Ex 1 Ex 2

Preorder Traversal Method void PreOrder(node *&h) { if (h!=NULL) cout<<h->inf; PreOrder(h->left); PreOrder(h->right); } }

Inorder Traversal Method Traversing a binary tree in inorder (Left – Root – Right) 1. Traverse the left subtree in inorder. 2. Visit the root. 3. Traverse the right subtree in inorder

Inorder Traversal Method int InOrder(node *&h) { if (h!=NULL) InOrder(h->left); cout<<h->inf; InOrder(h->right); } }

Ex 1 Ex 2

Postorder Traversal Method Traversing a binary tree in postorder (Left – Right – Root) 1. Traverse the left subtree in postorder. 2. Traverse the right subtree in postorder. 3. Visit the root postorder

Ex 1 Ex 2

Postorder Traversal Method int PostOrder(node *&h) { if (h!=NULL) PostOrder(h->left); PostOrder(h->right); cout<<h->inf; } }

Levelorder Traversal Method levelorder Example (Visit = print) Travers tree level by level

Traversals: Example