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.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Trees Briana B. Morrison Adapted from Alan Eugenio.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Fundamentals of Python: From First Programs Through Data Structures
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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 Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Starting at Binary Trees
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
Week 6 - Wednesday CS221.
Tree.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Week nine-ten: Trees Trees.
Trees Chapter 6.
Binary Trees, Binary Search Trees
Trees.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
8.2 Tree Traversals Chapter 8 - Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 its root The links from a node to its successors are called branches The successors of a node are called its children The predecessor of a node is called its parent Each node in a tree has exactly one parent except for the root node, which has no parent

3 Tree Terminology (continued) Nodes that have the same parent are siblings A node that has no children is called a leaf node (or, an external node) A node with at least one child is called an internal node. A generalization of the parent-child relationship is the ancestor-descendent relationship

4 Tree Terminology (continued) A subtree of a node is a tree whose root is a child of that node E.g. a subtree of node dog is marked as a triangle.

5 Tree Terminology (continued) The level (or depth) of a node is a measure of its distance from the root. Defined recursively: If node N is the root, its level is 0. (this is different than our textbook, which lets the level of root to be 1.) If node N is not the root, its level is 1 + the level of its parent. Height of a tree: maximum level (depth) in the tree We will denote height with h We will denote number of nodes by n

6 Binary Trees In a binary tree, each node has at most two children. A set of nodes T is a binary tree if either of the following is true T is empty Its root node has two subtrees, TL and TR, such that TL and TR are binary trees

7 Example Binary Tree Expression tree Each node contains an operator or an operand

8 Fullness and Completeness Trees grow from the top down Each new value is inserted in a new leaf node A binary tree is full if every node has two children except for the leaves Properties of binary trees: h and n relationship?

9 Tree Traversals Often we want to determine the nodes of a tree and their relationship Can do this by walking through the tree in a prescribed order and visiting the nodes as they are encountered This process is called tree traversal Three kinds of tree traversal Inorder Preorder Postorder

10 Tree Traversals (continued) Preorder: Visit root node, traverse TL, traverse TR Inorder: Traverse TL, visit root node, traverse TR Postorder: Traverse TL, Traverse TR, visit root node

11 Visualizing Tree Traversals Imagine a mouse that walks along the edge of the tree. If the mouse always keeps the tree to the left, it will trace a route known as the Euler tour Preorder traversal if we record each node as the mouse first encounters it Inorder if each node is recorded as the mouse returns from traversing its left subtree Postorder if we record each node as the mouse last encounters it Preorder? Inorder? Postorder?

12 Traversals Expression Trees An inorder traversal of an expression tree (inserting parenthesis where they belong) we get the infix form. A postorder traversal of an expression tree results in postfix form. x y + a b + c / * How can we evaluate an expression stored in such a tree? What type of traversal do we need?

13 The Node Class Just as for a linked list, a node consists of a data part and links to successor nodes The data part is a reference to type Object A binary tree node must have links to both its left and right subtrees

14 The BinaryTree Class * /+ xyba

15 The BinaryTree Class (continued)

16 See BinaryTree.java Add InOrderTraverse and PostOrderTraverse methods. Running time of postorder, preorder, inorder traversals?

17 Add to BinaryTree.java public int height() //return the height of the tree

18 Given an expression tree T Write algorithms (in pseudo-code) printExpression(T) //print the expression in //parenthesized infix form evaluateExpression(T) //return the value of the //expression

19 General Trees Nodes of a general tree can have any number of subtrees A general tree can be represented using a binary tree

20 General trees The tree node stores data firstChild // instead of left nextSibling //instead of right

21 Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty Its root has two subtrees such that each is a binary search tree and the value in the root is greater than all values of the left subtree but less than all values in the right subtree

22 Searching a Binary Tree

23 Class Interface Search Tree Java API’s TreeSet class implements a Binary Search Tree

24 BinarySearchTree Class

25 Insertion into a Binary Search Tree

26 Removing from a Binary Search Tree

27 Removing from a Binary Search Tree (continued)

28 public Object findKth(BinarySearchTree T, int k) //return the k-th smallest item from the BST T.