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.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Advertisements

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.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
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)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees Chapter 6.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
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; } 
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.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
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 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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
CSE 373 Data Structures Lecture 7
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
CC 215 Data Structures Trees
CSCE 210 Data Structures and Algorithms
Section 8.1 Trees.
CSE 373 Data Structures Lecture 7
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
TREES General trees Binary trees Binary search trees AVL trees
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

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 a few terms: parent, child, decendent, ancestor, sibling, subtree, path, degree,

Subtree a b d e f ij g h c k root A node and all of its descendents.

Paths in a Tree a c b e f d g j i h Path 1 Path 2 Path 1: { a, b, f, j } Path 2: { d, i } From a parent node to its child and other descendents. There exists a unique path from any node to any of its descendents.

Depth and Height tree height = 4 depth 0 depth 1 depth 2 depth 3 depth 4 node height = 2 Height of a node: the length of a node to its deepest descendent.

Degree The number of children of a node x is called the degree of x. degree = 3 degree = 1degree = 0

Binary Trees Each node has at most two children. r a) it is empty, or b) it consists of three disjoint subsets: 1) a root node 2) a left binary subtree 3) a right binary subtree A set of nodes T is a binary tree if a d b c f e left subtree right subtree Left child: the child node on the left. Right child: the child node on the right.

Representing Rooted Trees: Direct Way Every node has three references One reference to the object stored at that node One reference to the node’s parent one reference to the node’s children

Representing Rooted Trees: Child-sibling representation Every node has three references (direct way) One reference to the object stored at that node One reference to the node’s parent one reference to the node’s left most children One reference to the node’s sibling

Child-sibling representation

Tree Traversal A traversal is a manner of visiting each node in a tree once. There are several different traversals, each of which orders the nodes differently. A recursive way (preorder traversal) 1.visit the root of T 2.for each subtree T’ of the root, recursive traverse T’ The order in which you visit the subtrees in step 2 is arbitrary. The simplest way is to start at the root ʼ s firstChild and follow the nextSibling links. In binary trees, the order is always left to right. A typical application: listing file directory

Implementation What is the time complexity?

Tree Traversal Another recursive way (postorder traversal) 1.for each subtree T’ of the root, recursive traverse T’ 2.visit the root of T In a postorder traversal, you visit each node’s children (in the left- to-right order) before the node itself A typical application: sum the total disk space used root directory

Postorder tree Traversal

Tree Traversal Another recursive way (inorder traversal for a binary tree) 1.recursively traverse T’s left subtree 2.visit the root of T 3.recursively traverse T’s right subtree Yet another way (level order traversal) 1.visit the root of T 2.visit all depth-1 nodes from left to right 3.visit all depth-2 nodes, and so on how would you implement this?

Implementing Level Traversal Enqueue the root 1.Dequeue a node 2.Visit it 3.Enqueue its children (in order from left to right)

Using Tree Traversal 1 Computing the arity (or degree) of a tree, i.e., the maximum number of children of a node Arity/degree = 6

Computing arity

Using Tree Traversal 2 Computing the height of a tree Using recursive 1.If the root has no children, the height is 0 2.Otherwise, the height is 1+max(height(leftsubtree), height(rightsubtree)

Computing tree height

Expression Trees An expression tree is a binary tree that represents an arithmetic expression leaves are operands internal nodes are operators prefix infix postfix

From Postfix to Expression Tree The idea is to use a stack to store subtrees for subexpressions.

Evaluating an expression tree

Binary Trees A few APIs isLeaf() setLeft() setRight() setData()

Set A set is a collection of elements that contains no duplicates. Standard Operations: containment, union, and set differences

Binary Search Tree A binary search tree is often used to implement a set where the element type if comparable. Property: For any node X, every key (element) in the left subtree of X is less than X’s key, and every key in the right subtree of X is greater than X’s key A same set of elements may have different BSTs, and an inorder traversal of a binary search tree visits the nodes in sorted order.

Building a BST for a set

Contains(c) Checking if a given element is C contained by a set is easier when the set is represented by a BST

Contains(c)

Add(k) The worst-case time needed to search for a key or to add a key to a BST T is O(height(T)).

Building a BST for a set The tree built for a set of elements depends on the insertion order. The tree can be in two extreme cases: 1.Well-balanced tree (best case) 2.Very unbalanced tree (worst case) Starting from a root, adding elements in the set one by one

remove() Case 1: the node to be removed has no children Solution: Just delete the node

remove() Case 2: the node to be removed has only one child Solution: Just replace the deleted node with its child.

remove() Case 3: the node to be removed has two children Solution: replace the node with its successor

remove() Case 3: the node to be removed has two children The successor’s left child cannot exist, why?

successor() Case 1: The node has a right subtree. Go down to leftmost element in right subtree. Case 2: The node does not have a right subtree. If there actually is a successor, then it must be the root r of this subtree. To find r, go up the tree following parent pointers, stopping when the previous node in the path is a left child of the current node.

iterator() The tree iterator follow the nature order, starting at the minimum, or leftmost, element of the tree, proceeding all the way to the maximum. next() Go down the tree, following left child pointers until we find a node with no left child. This must be the minimum. hasNext() Implement using successor() remove() What is their time complexity()?