2/11/2016 1 IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.

Slides:



Advertisements
Similar presentations
Computer Science C++ High School Level By Guillermo Moreno.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
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, Binary Trees, and Binary Search Trees COMP171.
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.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
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.
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.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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 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.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Tree Data Structures.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
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.
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
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
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.
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.
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.
Binary Trees.
The Tree ADT.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Binary Trees.
Recursive Definition of Tree Structures
Binary Tree.
Week 6 - Wednesday CS221.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees Another Abstract Data Type (ADT)
Trees.
Binary Trees, Binary Search Trees
Binary Search Trees Definition (recursive):
Depict the Tree Structure in a picture
slides created by Alyssa Harding
Trees Another Abstract Data Type (ADT)
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Trees Another Abstract Data Type (ADT)
Binary Trees, Binary Search Trees
Binary Trees.
Binary Tree Properties & Representation
Tree.
Chapter 20: Binary Trees.
Trees.
Tree and its terminologies
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some other trees form new a tree; the node is the root of the tree. null

2/11/20162IT 179 c Terminology b g k e j a fhi d The root the links may be directed leaf Parent of e,f,g,h,i Siblings Ancestors Descendants Child of d depth of d = height of b = height of c = 1 3 1

2/11/20163IT 179 More Terminologies 1.In-degree: the number of links pointing to a node (always 1 or 0); the root is the only node with in-degree Out-degree: the number of link pointing out from a node; leaves are node with out- degree 0 3. Degree of a tree (arity): the maximum out- degree of nodes in the tree cb g k e j a fhi d

2/11/20164IT 179 Tree Implementation Using Array Array is a good choice if: the degree is small the tree is rather full the size does not change too often d4 d2 d7 d1 d5 d3 d6 size = 2 height - 1 What is the advantage and disadvantage of using array? d height = 3 degree = 2 1 st child (left-child) of i is 2*i+1; 2 nd child (right-child) of i is 2*i+2. The parent of i is (i-1)/2 d1 d2 d3 d4 d5 d height = 4 6

2/11/20165IT 179 A full-tree or complete tree is a perfect example for using array d8 d4 d10 d2 d5 d1 d6 d3 d7 d9 d4 d2 d5 d1 d6 d3 d7 d8d9d10d11d12d13d14d15 full-tree (no single child) complete tree perfect-tree

2/11/20166IT 179 Tree Implementation: linked lists  arrays Linked Lists data 1.data field 2.enough fields for pointers pointing the children Usually, we use the degree of the tree as the number of the pointer fields. Fixed, if the degree is small, e.g., binary tree (degree is 2).

2/11/20167IT 179 Binary Tree Linked Lists data 1.Data field 2.Right and Left Children

2/11/20168IT 179 A Binary Tree Node in Java 1.A data field 2.Two pointers to the left- child and right-child public class BinaryTree { /********** This is an inner class for tree nodes************/ private static class TNode { private E data; private TNode left,right; private TNode(E data, TNode left, TNode right) {//Construct a node with two children this.data = data; this.left = left; this.right = right; } /********** This is the end of the inner class TNode *******/ private TNode root; public BinaryTree() { root = null; }.....

2/11/20169IT 179 Calculate the size of the tree, i.e., the number of nodes nrnr nlnl X root = null; 0 n l n r private int size(TNode t) { if (t == null) return 0; return 1 + size(t.left) + size(t.right); } // Return the number nodes in the tree..... public int size() { return size(root); }

2/11/201610IT 179 Count how many k’s in the tree nrnr nlnl X root = NULL; 0 if x = k, n l n r ; otherwise, n l + n r private int count(TNode t, E k) { if (t == null) return 0; return (k.compareTo(t.data) == 0 ? 1 : 0) + count(t.left, k) + count(t.right, k); } // Return the number of k’s in the tree..... public int count(E k) { return count(root, k); }

2/11/201611IT 179 b Height of a Tree: The number of nodes in the longest path from the root. c f h e g a d root height = // Return the heights of the tree..... public int height() { return height(root); } Private int height(TNode t) { if (t == null) return 0; int L = height(t.left); int R = height(t.right); return 1 + (L > R ? L : R); }

2/11/201612IT 179 A random binary tree: Randomly insert data into a binary tree

2/11/201613IT 179 //add data to this BST, return false if data is already in the tree public void add(E data) { root = add(root, data); } Add a new data to the BinaryTree A private overloaded add method;

2/11/ IT 179 Randomly Insert (add) a data into a binary tree: data head toss a coin private TNode add(TNode node, E data){ if (node == null) return new Node (data,null,null); if ( Math.random() < 0.5) // toss a coin node.left = add(node.left, data); else node.right = add(node.right, data); return node; } data

2/11/201615IT 179 How to remove data from a binary tree? remove 13

2/11/201616IT 179 Syntax Trees of Arithmetic Expressions 3 * * *(2+1)+(3-3*5)

2/11/201617IT 179 Syntax Trees for Arithmetic Expressions 3 * * *(2+1)+(3-3*5) 3*(2+1)+3-3*5 3 * * 21 35

2/11/201618IT 179 Tree traversal: preorder, inorder, postorder R L X preorder: X  L  R R L X inorder: L  X  R R L X postorder: L  R  X

2/11/201619IT 179 private void inorder(TNode t) { if (t==null) return; inorder(t.left); System.out.print(t.data+", "); inorder(t.right); } // (1) Travel to the left-child // (2) The way we visit the node // (3) Travel to the right-child // Three ways of traversals..... public void inorder() { inorder(root); } public void preorder() { preorder(root); } public void postorder() { postorder(root); }..... private void preorder(TNode t) { if (t==null) return; System.out.print(t.data+", "); preorder(t.left); preorder(t.right); } // (1) The way we visit the node // (2) Travel to the left-child // (3) Travel to the right-child private void postorder(TNode t) { if (t==null) return; postorder(t.left); postorder(t.right); System.out.print(t.data+", "); } // (1) Travel to the left-child // (2) Travel to the right-child // (3) The way we visit the node

2/11/201620IT 179 ArithBinaryTree package myUtil; public class ArithBinaryTree{ private BinaryTree syntaxTree = new BinaryTree (); /** * Build up the syntax tree represented by BinaryTree infix ArithmeticException("Illegal Expression") */ public ArithBinaryTree(String infix) { AStack op= new AStack (); AStack > v = new AStack >(); // construct the syntax tree here }.....

2/11/201621IT 179 ArithBinaryTree: building up the syntax tree public ArithBinaryTree(String infix) { AStack op= new AStack (); AStack > v = new Stack >(); // construct the syntax tree here // token is a value v.push(new BinaryTree (token,null,null)); // evaluate operation of the operator in the token BinaryTree r,l,node; r = v.pop(); l = v.pop(); node = new BinaryTree (token,l,r); v.push(node); // perform operation on top of the op stack }

2/11/201622IT 179 ArithBinaryTree: evaluation public double evaluate() { return evaluate(syntaxTree); } private double evaluate(BinaryTree bt) { String str = bt.getRootData(); if (isOperator(str)) { double a = evaluate(bt.getLeft()); double b = evaluate(bt.getRight()); return perform(a,str,b); } return Double.parseDouble(str); }

2/11/201623IT 179 ArithBinaryTree: infix, prefix, postfix /** the infix notation of the expression. */ public String inFix() { Return syntaxTree.inOrderTraverse(); } /** the prefix notation of the expression. */ public String preFix() { return syntaxTree.preOrderTraverse(); } /** the postfix notation of the expression. */ public String postFix() { return syntaxTree.postOrderTraverse(); }