S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

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.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
BST Data Structure A BST node contains: A BST contains
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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.
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.
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.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Starting at Binary Trees
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Heaps & Priority Queues
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.
Binary Search Trees (BST)
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
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.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
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.
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
UNIT III TREES.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng Tree Data Structures S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng

Trees Data Structures Tree Binary tree Nodes Each node can have 0 or more children A node can have at most one parent Binary tree Tree with 0–2 children per node Tree Binary Tree

Trees Terminology Root  no parent Leaf  no child Interior  non-leaf Height  distance from root to leaf Root node Height Interior nodes Leaf nodes

Binary Search Trees Key property Value at node Example X Y Z Smaller values in left subtree Larger values in right subtree Example X > Y X < Z X Y Z

Not a binary search tree Binary Search Trees Examples 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Not a binary search tree Binary search trees

Binary Tree Implementation Class Node { int data; // Could be int, a class, etc Node *left, *right; // null if empty void insert ( int data ) { … } void delete ( int data ) { … } Node *find ( int data ) { … } … }

Iterative Search of Binary Tree Node *Find( Node *n, int key) { while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; Node * n = Find( root, 5);

Recursive Search of Binary Tree Node *Find( Node *n, int key) { if (n == NULL) // Not found return( n ); else if (n->data == key) // Found it else if (n->data > key) // In left subtree return Find( n->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5);

Example Binary Searches Find ( root, 2 ) root 10 5 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found 5 30 2 45 2 25 45 30 10 25

Example Binary Searches Find (root, 25 ) 10 5 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found 5 30 2 45 2 25 45 30 10 25

Degenerate binary tree Types of Binary Trees Degenerate – only one child Complete – always two children Balanced – “mostly” two children more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree

Binary Trees Properties Balanced Height = O( log(n) ) for n nodes Useful for searches Degenerate Height = O(n) for n nodes Similar to linked list Degenerate binary tree Balanced binary tree

Binary Search Properties Time of search Proportional to height of tree Balanced binary tree O( log(n) ) time Degenerate tree O( n ) time Like searching linked list / unsorted array

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

Binary Search Tree – Insertion Algorithm Perform search for value X Search 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 Observations O( log(n) ) operation for balanced tree Insertions may unbalance tree

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

Binary Search Tree – Deletion Algorithm Perform search for value X If X is a leaf, delete X Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation O( log(n) ) operation for balanced tree Deletions may unbalance tree

Example Deletion (Leaf) Delete ( 25 ) 10 10 10 < 25, right 30 > 25, left 25 = 25, delete 5 30 5 30 2 25 45 2 45

Example Deletion (Internal Node) Delete ( 10 ) 10 5 5 5 30 5 30 2 30 2 25 45 2 25 45 2 25 45 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf

Example Deletion (Internal Node) Delete ( 10 ) 10 25 25 5 30 5 30 5 30 2 25 45 2 25 45 2 45 Replacing 10 with smallest value in right subtree Deleting leaf Resulting tree

Balanced Search Trees Kinds of balanced binary search trees height balanced vs. weight balanced “Tree rotations” used to maintain balance on insert/delete Non-binary search trees 2/3 trees each internal node has 2 or 3 children all leaves at same depth (height balanced) B-trees Generalization of 2/3 trees Each internal node has between k/2 and k children Each node has an array of pointers to children Widely used in databases

Other (Non-Search) Trees Parse trees Convert from textual representation to tree representation Textual program to tree Used extensively in compilers Tree representation of data E.g. HTML data can be represented as a tree called DOM (Document Object Model) tree XML Like HTML, but used to represent data Tree structured

Parse Trees Expressions, programs, etc can be represented by tree structures E.g. Arithmetic Expression Tree A-(C/5 * 2) + (D*5 % 4) + - % A * * 4 / 2 D 5 C 5

Tree Traversal Goal: visit every node of a tree in-order traversal + - % A * * 4 / 2 D 5 C 5 Goal: visit every node of a tree in-order traversal void Node::inOrder () { if (left != NULL) { cout << “(“; left->inOrder(); cout << “)”; } cout << data << endl; if (right != NULL) right->inOrder() } Output: A – C / 5 * 2 + D * 5 % 4 To disambiguate: print brackets

Tree Traversal (contd.) + - % A * * 4 / 2 D 5 C 5 pre-order and post-order: void Node::preOrder () { cout << data << endl; if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); } Output: + - A * / C 5 2 % * D 5 4 void Node::postOrder () { if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); cout << data << endl; } Output: A C 5 / 2 * - D 5 * 4 % +

XML Data Representation E.g. <dependency> <object>sample1.o</object> <depends>sample1.cpp</depends> <depends>sample1.h</depends> <rule>g++ -c sample1.cpp</rule> </dependency> Tree representation dependency object depends depends rule sample1.o sample1.cpp sample1.h g++ -c …

Graph Data Structures E.g: Airline networks, road networks, electrical circuits Nodes and Edges E.g. representation: class Node Stores name stores pointers to all adjacent nodes i,e. edge == pointer To store multiple pointers: use array or linked list Mumbai Ahm’bad Calcutta Delhi Madurai Chennai

End of Chapter