General Trees. General Trees. General Trees. More formally. N-ary Trees

Similar documents
Binary Search Trees (BST)

Ordered Lists and Binary Trees

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Algorithms and Data Structures

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Converting a Number from Decimal to Binary

Data Structures and Algorithms

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Full and Complete Binary Trees

TREE BASIC TERMINOLOGIES

Data Structure with C

Data Structures. Level 6 C Module Descriptor

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Binary Trees and Huffman Encoding Binary Search Trees

Chapter 14 The Binary Search Tree

Algorithms and Data Structures Written Exam Proposed SOLUTION

DATA STRUCTURES USING C

Big Data and Scripting. Part 4: Memory Hierarchies

Any two nodes which are connected by an edge in a graph are called adjacent node.

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures and Algorithms(5)

The ADT Binary Search Tree

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Alex. Adam Agnes Allen Arthur

Analysis of Algorithms I: Binary Search Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

Binary Search Trees 3/20/14

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Symbol Tables. Introduction

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Binary Coded Web Access Pattern Tree in Education Domain

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Physical Data Organization

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Binary Search Trees CMPSC 122

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Optimal Binary Search Trees Meet Object Oriented Programming

Lecture Notes on Binary Search Trees

Binary Heap Algorithms

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Binary Search Trees. Ric Glassey

Algorithms Chapter 12 Binary Search Trees

10CS35: Data Structures Using C

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Lecture Notes on Binary Search Trees

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

OPTIMAL BINARY SEARCH TREES

Recursive Implementation of Recursive Data Structures

7.1 Our Current Model

M-way Trees and B-Trees

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Hierarchical Model APPENDIXE. E.1 Basic Concepts

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Merkle Hash Trees for Distributed Audit Logs

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

LINKED DATA STRUCTURES

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Structural Design Patterns Used in Data Structures Implementation

Glossary of Object Oriented Terms

Data Structure [Question Bank]

COMPUTER SCIENCE. Paper 1 (THEORY)

Binary Heaps. CSE 373 Data Structures

Lecture 1: Course overview, circuits, and formulas

PES Institute of Technology-BSC QUESTION BANK

Lecture 1: Data Storage & Index

Analysis of Algorithms I: Optimal Binary Search Trees

From Last Time: Remove (Delete) Operation

6 Creating the Animation

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Why Use Binary Trees?

Database Design Patterns. Winter Lecture 24

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

GRAPH THEORY LECTURE 4: TREES

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Transcription:

eneral Trees eneral Trees eneral trees are similar to binary trees, except that there is no restriction on the number of children that any node may have. hapter 7 Well, non-binary trees anyway. More formally tree, T, is a finite set of one or more nodes such that there is one designated node r called the root of T, and the remaining nodes in (T - {r}) are partitioned into n 0 disjoint subsets T 1, T 2,, T k, each of which is a tree, and whose roots r 1, r 2,, r k, respectively, are children of r. eneral Trees One way to implement a a general tree is to use the same node structure that is used for a linkbased binary tree. Specifically, given a node n, n s left pointer points to its left-most child (like a binary tree) and, n s right pointer points to a linked list of nodes that are siblings of n (unlike a binary tree). Root R N-ary Trees arent of V V ncestors of V n n-ary tree is a generalization of a binary tree, where each node can have no more than n children. S1 2 1 S2 Siblings of V Since the maximum number of children for any node is known, each parent node can point directly to each of its children -- rather than requiring a linked list. Subtree rooted at V hildren of V 1

N-ary Trees This results in a faster search time (if you know which child you want). N-ary Trees: xample n n-ary tree with n = 3 The disadvantage of this approach is that extra space reserved in each node for n child pointers, many of which may not be used. ointer-based implementation of the n-ary tree eneral Trees: xample general tree inary tree with the pointer structure of the preceding general tree eneral Trees: xample (ont d.) ointer-based implementation of the general tree Tree T (Java) We use positions to abstract nodes eneric methods: integer size() boolean ismpty() terator elements() terator positions() ccessor methods: position root() position parent(p) positionterator children(p) Query methods: boolean isnternal(p) boolean isxternal(p) boolean isroot(p) Update method: object replace (p, o) dditional update methods may be defined by data structures implementing the Tree T ++ T lass TNode { public: TNode (const LM); constructor ~TNode(); destructor LM value(); return node s value bool isleaf(); TRU if is a leaf TNode* parent(); return parent TNode* leftmost_child(); return first child TNode* rightmost_sibling(); return right sibling void setvalue(lm); set node s value void insert_first(tnode* n); insert first child void insert_next(tnode* n); insert right sibling void remove_first(); remove first child void remove_next(); remove right sibling }; 2

++ T lass entree { public: entree(); constructor ~entree(); destructor void clear(); free nodes TNode* root(); return root void newroot(lm, TNode*, TNode*); }; combine J K L reorder Traversal: 1) process root 2) recursively process children from left to right eneral Tree Traversal lgorithm rint (TNode rt) preorder traversal from root nput: a general tree node Output: none information printed to screen TNode temp if (rt is a leaf) output Leaf: else output nternal: output value stored in node temp = leftmost_child of rt while (temp is not NULL) rint (temp) note recursive call temp = right_sibling of temp J K L reorder Traversal: 1) process root 2) recursively process children from left to right M N O J K L norder Traversal: J K L J K L ostorder Traversal: 1) no node is processed until all of its children have been processed, recursively, left to right 2) process root by definition, none J K L 3

arent ointer mplementation R W X Y Z mplementations arent s ndex Label Node ndex 0 0 1 1 1 2 7 7 7 R W X Y Z 0 1 2 3 4 5 6 7 8 9 10 ommon ones, plus make up your own! Lists of children Leftmost hildright Sibling Leftmost hildright Sibling Linked mplementations 4

Linked mplementations onverting to a inary Tree Left childright sibling representation essentially stores a binary tree. Use this process to convert any general tree to a binary tree. forest is a collection of one or more general trees. Sequential mplementations List node values in the order they would be visited by a preorder traversal. Saves space, but allows only sequential access. Need to retain tree structure for reconstruction. Sequential mplementations xample: or binary trees, us a symbol to mark null links. Which node was the right child of the root? space efficient, but not time efficient 5

Sequential mplementations xample: Mark nodes as leaf or internal. What about general trees? Not only must the general tree implementation indicate whether a node is a leaf or internal node, it must also indicate how many children the node has. no need for null pointers when both children are null What about general trees? lternatively, the implementation can indicate when a node s child list has come to an end. nclude a special mark to indicate the end of a child list. ll leaf nodes are followed by a ) symbol since they have no children. leaf node that is also the last child for its parent would indicate this by two or more successive ) symbols. Sequential mplementations xample: or general trees, mark the end of each subtree. R))))))) 6