General Trees & Binary Trees

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Data Structures – CSC212 (1) Dr Muhammad Hussain Lecture - Binary Tree ADT Binary Tree ADT It is an ordered tree with the following properties 1.Each node.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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 & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
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,
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 CMSC 341 Introduction to Trees Textbook sections:
(c) University of Washington20-1 CSC 143 Java Trees.
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.
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.
Binary Trees.
CSE 373 Data Structures Lecture 7
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CC 215 Data Structures Trees
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Chapter 25 Binary Search Trees
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Trees Another Abstract Data Type (ADT)
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
Chapter 17 Object-Oriented Data Structures
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Binary Trees.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees CSE 373 Data Structures.
Trees Another Abstract Data Type (ADT)
Data Structures and Algorithms for Information Processing
Binary Trees.
Binary Search Trees (BSTs)
Binary Search Trees (BSTs)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Announcements Prelim 1 on Tuesday! A4 will be posted today
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Binary Search Trees (BSTs)
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

General Trees & Binary Trees CSC212

Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent one-to-one relation between data elements. Trees have a nested or a hierarchical structure. Hierarchical structures represent one-to-many relation between data elements.

Trees Examples of situations were one-to-many relations exist… these can be represented as trees. Relation between a parent and his children. Relation between a person and books he owns. Relation between a basketball team and the players on the team. Card catalog in a library.

Figure 23-1 Carole’s children and grandchildren Example Figure 23-1 Carole’s children and grandchildren Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-2 Jared’s parents and grandparents Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-3 A portion of a university’s administrative structure Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-4 Computer files organized into folders Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 23-5 A tree equivalent to the tree in Figure 23-4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Trees: Some Terminology A tree is represented as a set of nodes connected by edges. Nodes Edges A Tree Root Leafs

Trees: Comparison with Lists A List Unique first element called head. Unique last element called tail. Each element, other than the first and the last, has a unique predecessor and a unique successor. A Tree Unique first node called root. Each node has successors, called its children. Each node has one predecessor, called parent. Leafs have no children. Root has no parent.

Trees: More Terminology Simple path: a sequence of distinct nodes in the tree. Path length: number of nodes in a path. Siblings: two nodes that have the same parent. Ancestors: given a node A in a tree, the parent of the node A and the ancestors of the parent of A, are ancestors of A.

Trees: More Terminology Parent: a parent of a node is its predecessor. Child: a child of a node is its successor. Root: a unique node without any predecessor. Leafs: nodes without any children. Descendants: given a node A in a tree, the children of A and all descendents of the children of A are descendents of A.

Trees: More Terminology A G F E B C I D H J Sub-Tree Rooted at G A-B-F is a Path Level 1 Level 2 Level 3 Level 4 Height?

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts Root of an ADT tree is at tree’s top Only node with no parent All other nodes have one parent each Each node can have children A node with children is a parent A node without children is a leaf Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts General tree Node can any number of children N-ary tree Node has max n children Binary tree node has max 2 children Node and its descendants form a subtree Subtree of a node Tree rooted at a child of that node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Tree Concepts Subtree of a tree Subtree of the tree’s root Height of a tree Number of levels in the tree Path between a tree’s root and any other node is unique. Copyright ©2012 by Pearson Education, Inc. All rights reserved

Binary Trees A binary tree is a tree with the following: Each node can have at most two subtrees and therefore at most two children. Each subtree is identified as being either the left subtree or the right subtree of the parent. It may be empty. Nodes in a binary tree may be composite e.g. of variable type ‘Type’.

Figure 23-6 Three binary trees Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 23-7 The number of nodes in a full binary tree as a function of the tree’s height Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 23-7 The number of nodes in a full binary tree as a function of the tree’s height Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved The height of a binary tree with n nodes that is either complete or full is log2 (n + 1) rounded up. Figure 23-7 The number of nodes in a full binary tree as a function of the tree’s height Copyright ©2012 by Pearson Education, Inc. All rights reserved

Binary Trees + 7 * a c b [(a + b) * c] + 7 + * c b a 7

ADT Binary Tree Elements: The elements are nodes, each node contains the following data type: Type and has LeftChild and RightChild references. Structure: hierarchical structure; each node can have two children: left or right child; there is a root node and a current node. Domain: the number of nodes in a binary tree is bounded; domain contains empty tree, trees with one element, two elements, …

ADT Binary Tree Operations: Method Traverse (Order ord) requires: Binary Tree (BT) is not empty. input: ord. results: Each element in the tree is processed exactly once by a user supplied procedure. The order in which nodes are processed depends on the value of ord (Order = {preorder, postorder, inorder}) preorder: each node processed before any node in either of its subtrees. inorder: each node is processed after all its nodes in its left subtree and before any node in its right subtree. postorder: each node is processed after all its nodes in both of its subtrees. output: none.

Tree Traversals To traverse a tree means to process (e.g. printing it) each element in the tree. Tree traversals: n! ways of traversing a tree of n nodes. pre-order, in-order, post-order  three natural traversals orders. List traversals: n! ways of traversing a list of n nodes. front-to-back or back-to-front.  two natural traversals orders.

ADT Binary Tree Operations: Method Insert (Type e, Relative rel, boolean inserted) (Relative = {leftchild, rightchild, root, parent}) requires: (1) Full () is false and (2) either (a) rel = root and Empty() is true or (b) rel <> root and rel <> parent and Empty( ) is false. input: e, rel. results: if case (1) rel = leftChild, current node has a left child, or (2) rel = rightChild, current node has a right child, then inserted is false. Else a node containing e is added as rel of the current node in the tree and becomes the current node and inserted is true. output: inserted.

ADT Binary Tree Procedure DeleteSub () requires: Binary tree is not empty. input: none results: The subtree whose root node was the current node is deleted from the tree. If the resulting tree is not empty, then the root node is the current node. output: none. Procedure Update (Type e). requires: Binary tree is not empty. input: e. results: the element in e is copied into the current node. output: none.

ADT Binary Tree 5. Procedure Retrieve (Type e) requires: Binary tree is not empty. input: none results: element in the current node is copied into e. output: e.

ADT Binary Tree 6. Procedure Find (Relative rel, boolean found) requires: Binary tree is not empty. input: rel. results: The current node of the tree is determined by the value of rel and previous current node.. output: found. 7. Procedure Empty (boolean empty). requires: None. input: none results: If Binary tree is empty then empty is true; otherwise empty is false. output: empty.

ADT Binary Tree 8. Procedure Full (boolean full) requires: None. input: None. results: if the binary tree is full then full is true otherwise false. output: full.

ADT Binary Tree: BTNode Class public class BTNode <T> { public T data; public BTNode<T> left, right; /** Creates a new instance of BTNode */ public BTNode(T val) { data = val; left = right = null; } public BTNode(T val, BTNode<T> l, BTNode<T> r){ left = l; right = r; } }

ADT Binary Tree: Order & Relative Classes //These definitions are in separate files and define // the Order and Relative classes. public enum Order {preOrder, inOrder, postOrder}; public enum Relative {Root, Parent, LeftChild, RightChild};

ADT Binary Tree public class BT<T> { BTNode<T> root, current; /** Creates a new instance of BT */ public BT() { root = current = null; } public boolean empty(){ return root == null ? true: false;

ADT Binary Tree // Non-recursive version of findparent – uses pre-order traversal private BTNode<T> findparent (BTNode<T> p) { // Stack is used to store the right pointers of nodes LinkStack<BTNode<T>> stack = new LinkStack<BTNode<T>>(); BTNode<T> q = root; while (q.right != p && q.left != p){ if (q.right != null) stack.push(q.right); if (q.left != null) q = q.left; else q = stack.pop(); // Go right here } return q;

ADT Binary Tree /* Recursive version of findparent – preorder traversal used */ private BTNode<T> findparent (BTNode<T> p, BTNode<T> t) { if (t == null) return null; /* empty tree */ if (t.right == null && t.left == null) return null; else if (t.right == p || t.left == p) return t; /* parent is t */ else { BTNode q = findparent(p, t.left); if (q != null) return q; else return findparent(p, t.right); }

ADT Binary Tree public boolean find (Relative rel){ switch (rel) { case Root: //easy case current = root; return true; case Parent: if (current == root) return false; current = findparent(current, root); case LeftChild: if (current.left == null) return false; current = current.left;

ADT Binary Tree case RightChild: if (current.right == null) return false; current = current.right; return true; default: return false; }

ADT Binary Tree public boolean insert (Relative rel, T val) { switch (rel) { case Root: if (! empty()) return false; current = root = new BTNode<T>(val); return true; case Parent: //This an impossible case. return false; case LeftChild: if (current.left != null) return false; current.left = new BTNode<T>(val); current = current.left;

ADT Binary Tree case RightChild: if (current.right != null) return false; current.right = new BTNode<T> (val); current = current.right; return true; default: return false; }

ADT Binary Tree public T retrieve (){ return current.data; } public void update (T val){ current.data = val;

ADT Binary Tree public void delete_subtree(){ if (current == root){ current = root = null; } else { BTNode<T> p = current; find(Relative.Parent); if (current.left == p) current.left = null; else current.right = null; current = root; } }