1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen FS 2008 Bäume.

Slides:



Advertisements
Similar presentations
1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen FS 2008 Hashtabellen.
Advertisements

1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen SS 2007 Helmut Schauer.
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Lab 1: 1. Download all my programs in your computer under the same folder. 2. The tree shown in the following figure represents an expression: (((( 3 +
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Razdan CST230http://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Chapter 9 Trees Anshuman Razdan Div of Computing Studies.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Listen, Stacks und Queues headtail p x.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Stacks with Dynamic Memory Class Signature Class Node {public Data data; public Node next; } Class Stack {private Node top; public Stack(); public void.
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,
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 4 Data Structures ADT Examples  Stack  Queue  Trees.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Tree Implementations Chapter 24 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java,
Tree Implementations Chapter Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
CMSC 341 Binary Search Trees. 8/3/2007 UMBC CMSC 341 BST 2 Binary Search Tree A Binary Search Tree is a Binary Tree in which, at every node v, the values.
CSC 205 Java Programming II Lecture 26 Traversing Binary Tree.
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees Chapter 11 (continued)
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Recursive Definition of Tree Structures
Recursive Objects (Part 4)
Trees Chapter 11 (continued)
Trees What is a Tree? Tree terminology Why trees?
Tree Traversals – reminder
Chapter 20: Binary Trees.
Depict the Tree Structure in a picture
Stacks – review A Last-In First-Out (LIFO) structure Basic Operations:
Tree Traversals – reminder
Chapter 21: Binary Trees.
Trees.
Representation of a Threaded Tree
© המרכז להוראת המדעים האוניברסיטה העברית בירושלים
General Trees & Binary Trees
בתרגול הקודם רשימה מקושרת – Linked List Iterator Queue Stack
Slides by Steve Armstrong LeTourneau University Longview, TX
Trees Definitions Implementation Traversals K-ary Trees
General Trees & Binary Trees
2018, Fall Pusan National University Ki-Joune Li
Stacks with Dynamic Memory
if the tree is empty, do nothing,
CSC 143 Java Trees.
Trees.
Chapter 20: Binary Trees.
Building Java Programs
Tree.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

1 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Algorithmen und Datenstrukturen FS 2008 Bäume

2 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Binary Tree in Java (1) class BinaryTree { class Node { static Node empty = new Node(null) { boolean isEmpty() {return true;} }; Object info; Node left, right; Node(Object x) {info = x; left = empty; right = empty;} boolean isEmpty() {return false;}... } Node root; BinaryTree() {root = empty;} boolean isEmpty() {return root.isEmpty();}... }

3 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Binary Tree in Java (2) Anzahl der Knoten int count() { if (isEmpty()) return 0; else return 1+left.count()+right.count(); }

4 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Binary Tree in Java (3) Maximale Höhe int height() { if (isEmpty()) return 0; else return 1+Math.max(left.height(),right.height()); }

5 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Preorder Traversieren (rekursiv) preOrder() { if (!isEmpty()) { visit(info); left.preOrder(); right.preOrder(); }

6 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Postorder Traversieren (rekursiv) postOrder() { if (!isEmpty()) { left.postOrder(); right.postOrder(); visit(info); }

7 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Inorder Traversieren (rekursiv) inOrder() { if (!isEmpty()) { left.inOrder(); visit(info); right.inOrder(); }

8 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Preorder Traversieren (iterativ) preOrder() { if (isEmpty()) return; Stack stack = new Stack(); stack.push(root); do { Node t = (Node) stack.pop(); visit(t.info); if (!t.right.isEmpty()) stack.push(t.right); if (!t.left.isEmpty()) stack.push(t.left); } while (!stack.isEmpty()); }

9 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Levelorder Traversieren (iterativ) levelOrder() { if (isEmpty()) return; Queue queue = new Queue(); queue.put(root); do { Node t = (Node) queue.get(); visit(t.info); if (!t.left.isEmpty()) queue.put(t.left); if (!t.right.isEmpty()) queue.put(t.right); } while (!queue.isEmpty()); }

10 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Binäres Suchen in einem Sortierbaum Object search(Comparable x) { if (isEmpty()) return null; // x not found if (x.compareTo ( info)<0) return left.search(x); if (x.compareTo ( info)>0) return right.search(x); else return info; // x found }

11 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Einfügen in einen Sortierbaum Node insert(Comparable x) { if (isEmpty()) return new Node(x); if (x.compareTo ( info)<0) left = left.insert(x); else right = right.insert(x); return this; }

12 Helmut Schauer Educational Engineering Lab Department for Informatics University of Zurich Entfernen aus einem Sortierbaum Node remove(Comparable x) { if (isEmpty()) return this; if (x.compareTo ( info)<0) left = left.remove(x); else if (x.compareTo ( info)>0) right = right.remove(x); else if (left.isEmpty()) return right; else if (right.isEmpty()) return left; else { Node t = right; while (!t.left.isEmpty()) t = t.left; info = t.info; right = right.remove((Comparable)info); } return this; }