Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks

Slides:



Advertisements
Presentasi serupa
Struktur Data Departemen Ilmu Komputer FMIPA-IPB 2009
Advertisements

STRUKTUR DATA (10) tree manipulation
Binary Trees adalah parent
Design and Analysis of Algorithm Recursive Algorithm Analysis
Binary Tree Traversal.
By : Fitroh Amaluddin & Galih Wasis W.
Algoritma dan Struktur Data
Implementasi Binary Tree
Tree.
Pertemuan Struktur Data *Pohon Ekspresi *
Tree Yuliana S.
2. Introduction to Algorithm and Programming
Binary Tree Rangga Juniansyah.
Penelusuran Bab 7 Pohon Biner 219.
ADT Tree 2007/2008 – Ganjil – Minggu 8.
Binary Search Tree 2007/2008 – Ganjil – Minggu 9.
1 DATA STRUCTURE “ STACK” SHINTA P STMIK MDP APRIL 2011.
Tenia Wahyuningrum, S.Kom. MT
Presented By : Group 2. A solution of an equation in two variables of the form. Ax + By = C and Ax + By + C = 0 A and B are not both zero, is an ordered.
Struktur Data List Linear : Linked List (Single Linkedlist)
1 Diselesaikan Oleh KOMPUTER Langkah-langkah harus tersusun secara LOGIS dan Efisien agar dapat menyelesaikan tugas dengan benar dan efisien. ALGORITMA.
STRUKTUR DATA tree manipulation
BINARY TREE Universitas Ahmad Dahlan
1 Pertemuan Tree Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Pertemuan 13 Graph + Tree jual [Valdo] Lunatik Chubby Stylus.
Algoritma dan Struktur Data
Tree. Tree (Pohon) Dalam dunia nyata, sebuah pohon memiliki : akar, cabang, daun. Dalam dunia komputer, pohon (tree) memiliki 3 (tiga) bagian tersebut.
1 Pertemuan 09 Kebutuhan Sistem Matakuliah: T0234 / Sistem Informasi Geografis Tahun: 2005 Versi: 01/revisi 1.
Red-Black Trees.
1 Pertemuan 17 Heaps Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Mata kuliah :K0362/ Matematika Diskrit Tahun :2008
1 Pertemuan 12 B-Tree Matakuliah: T0534/Struktur Data Tahun: 2005 Versi: September 2005.
9.3 Geometric Sequences and Series. Objective To find specified terms and the common ratio in a geometric sequence. To find the partial sum of a geometric.
Binary Search Tree. Sebuah node di Binary Search Tree memiliki path yang unik dari root menurut aturan ordering – Sebuah Node, mempunyai subtree kiri.
OPERATOR DAN FUNGSI MATEMATIK. Operator  Assignment operator Assignment operator (operator pengerjaan) menggunakan simbol titik dua diikuti oleh tanda.
Binary Tree.
POHON / TREE.
Binary Tree.
Menggambar Tree wijanarto.
Defri Kurniawan POHON DAN POHON BINER Defri Kurniawan
TREE STRUCTURE (Struktur Pohon)
Induksi Matematika.
STRUKTUR DATA Chapt 6 : TREE Oleh : Yuli Praptomo PHS, S.Kom.
Struktur Data Binary Search Tree (BST)
Binary Tree Rangga Juniansyah.
Dynamic Array and Linked List
Diagram Pohon (Tree Diagram)
Manipulasi Tree.
Tim Struktur Data Program Studi Teknik Informatika UNIKOM
Dasar-Dasar Pemrograman
STRUKTUR DATA Tree (Struktur Pohon).
NAMA : SITI HAJAR NIM : UNIT : B NO.HP :0852 –
STRUKTUR DATA 2014 M. Bayu Wibisono.
Manipulasi Tree.
Teknik Informatika - Universitas Muhammadiyah Malang (UMM)
Algorithms and Programming Searching
REAL NUMBERS EKSPONENT NUMBERS.
Parts of a Tree.
Algoritma dan Struktur Data
Tree (Pohon).
POHON Pohon (Tree) merupakan graph terhubung tidak berarah dan tidak mengandung circuit. Contoh: (Bukan) (Bukan) (Bukan)
Oleh Shoffin Nahwa Utama, S.Kom
IT234 Algoritma dan Struktur Data
IT234 Algoritma dan Struktur Data
Pohon Biner.
Algoritma dan Struktur Data
IT234 Algoritma dan Struktur Data
TREE Oleh : Neny silvia Nurhidayah Afny wilujeng Setyorini
5 11/18/2018.
Review Struktur Data Nisa’ul Hafidhoh, MT.
Transcript presentasi:

Trees and Binary Trees Become Rich Force Others to be Poor Rob Banks Stock Fraud The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.

Nature View of a Tree leaves branches root

Computer Scientist’s View root leaves nodes branches

What is a Tree A tree is a finite nonempty set of elements. It is an abstract model of a hierarchical structure. consists of nodes with a parent-child relation. Applications: Organization charts File systems Programming environments Computers”R”Us Sales R&D Manufacturing Laptops Desktops US International Europe Asia Canada

Tree Terminology subtree Subtree: tree consisting of a node and its descendants Root: node without parent (A) Siblings: nodes share the same parent Internal node: node with at least one child (A, B, C, F) External node (leaf ): node without children (E, I, J, K, G, H, D) Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Degree of a node: the number of its children Degree of a tree: the maximum number of its node. A B D C G H E F I J K subtree

Tree Properties Property Value Number of nodes Height Root Node Leaves Interior nodes Ancestors of H Descendants of B Siblings of E Right subtree of A Degree of this tree A B C D G E F I H

Tree ADT We use positions to abstract nodes Generic methods: integer size() boolean isEmpty() objectIterator elements() positionIterator positions() Accessor methods: position root() position parent(p) positionIterator children(p) Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update methods: swapElements(p, q) object replaceElement(p, o) Additional update methods may be defined by data structures implementing the Tree ADT

Intuitive Representation of Tree Node List Representation ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of links to sub-trees How many link fields are needed in such a representation? Data Link 1 Link 2 … Link n

Trees Every tree node: object – useful information children – pointers to its children Data Data  Data  Data  Data  Data  Data 

A Tree Representation A node is represented by an object storing B A D Element Parent node Sequence of children nodes  B A D F C E B D A C E F

Left Child, Right Sibling Representation Data Left Child Right Sibling A B C D E F G H I J K L

Tree Traversal Two main methods: Recursive definition Preorder: Postorder Recursive definition Preorder: visit the root traverse in preorder the children (subtrees) traverse in postorder the children (subtrees)

Preorder Traversal Algorithm preOrder(v) visit(v) A traversal visits the nodes of a tree in a systematic manner In a preorder traversal, a node is visited before its descendants Application: print a structured document Algorithm preOrder(v) visit(v) for each child w of v preorder (w) Become Rich 1. Motivations 3. Success Stories 2. Methods 2.1 Get a CS PhD 2.2 Start a Web Site 1.1 Enjoy Life 1.2 Help Poor Friends 2.3 Acquired by Google 1 2 3 5 4 6 7 8 9

Postorder Traversal Algorithm postOrder(v) for each child w of v In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) cs16/ homeworks/ todo.txt 1K programs/ DDR.java 10K Stocks.java 25K h1c.doc 3K h1nc.doc 2K Robot.java 20K 9 3 1 7 2 4 5 6 8

Binary Tree A binary tree is a tree with the following properties: Each internal node has at most two children (degree of two) The children of a node are an ordered pair We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either a tree consisting of a single node, OR a tree whose root has an ordered pair of children, each of which is a binary tree Applications: arithmetic expressions decision processes searching A B C D E F G H I

BinaryTree ADT The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT Additional methods: position leftChild(p) position rightChild(p) position sibling(p) Update methods may be defined by data structures implementing the BinaryTree ADT

Examples of the Binary Tree Skewed Binary Tree E C D 5 A B C G E I D H F Complete Binary Tree 1 2 3 4

Differences Between A Tree and A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered. A B A B Are different when viewed as binary trees. Are the same when viewed as trees.

Data Structure for Binary Trees A node is represented by an object storing Element Parent node Left child node Right child node  B A D C E B D A C E

Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2  (a - 1) + (3  b)) +  - 2 a 1 3 b

Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Want a fast meal? Yes No How about coffee? On expense account? Yes No Yes No Starbucks Spike’s Al Forno Café Paragon

Maximum Number of Nodes in a Binary Tree The maximum number of nodes on depth i of a binary tree is 2i, i>=0. The maximum nubmer of nodes in a binary tree of height k is 2k+1-1, k>=0. Prove by induction.

Relations between Number of Leaf Nodes and Nodes of Degree 2 For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0=n2+1 PROOF: Let n and B denote the total number of nodes and branches in T. Let n0, n1, n2 represent the nodes with no children, single child, and two children respectively. B+1=n B=n1+2n2 n=n0+n1+n2  n1+2n2+1= n  n0=n2+1

Height 3 full binary tree. A full binary tree of a given height k has 2k+1–1 nodes. Height 3 full binary tree.

Labeling Nodes In A Full Binary Tree Label the nodes 1 through 2k+1 – 1. Label by levels from top to bottom. Within a level, label from left to right. 1 2 3 4 6 5 7 8 9 10 11 12 13 14 15

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent.

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child.

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child.

Full binary tree of depth 3 Complete Binary Trees A labeled binary tree containing the labels 1 to n with root 1, branches leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and 6, 7, respectively, and so on. A binary tree with n nodes and level k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of level k. 1 2 3 7 5 11 4 10 6 9 8 15 14 13 12 Full binary tree of depth 3 1 2 3 7 5 9 4 8 6 Complete binary tree

Binary Tree Traversals Let l, R, and r stand for moving left, visiting the node, and moving right. There are six possible combinations of traversal lRr, lrR, Rlr, Rrl, rRl, rlR Adopt convention that we traverse left before right, only 3 traversals remain lRr, lrR, Rlr inorder, postorder, preorder

Inorder Traversal In an inorder traversal a node is visited after its left subtree and before its right subtree Algorithm inOrder(v) if isInternal (v) inOrder (leftChild (v)) visit(v) inOrder (rightChild (v)) 3 1 2 5 6 7 9 8 4

Print Arithmetic Expressions Algorithm inOrder (v) if isInternal (v){ print(“(’’) inOrder (leftChild (v))} print(v.element ()) if isInternal (v){ inOrder (rightChild (v)) print (“)’’)} Specialization of an inorder traversal print operand or operator when visiting node print “(“ before traversing left subtree print “)“ after traversing right subtree +  - 2 a 1 3 b ((2  (a - 1)) + (3  b))

Evaluate Arithmetic Expressions recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees Algorithm evalExpr(v) if isExternal (v) return v.element () else x  evalExpr(leftChild (v)) y  evalExpr(rightChild (v))   operator stored at v return x  y +  - 2 5 1 3

Creativity: pathLength(tree) =  depth(v) v  tree Algorithm pathLength(v, n) Input: a tree node v and an initial value n Output: the pathLength of the tree with root v Usage: pl = pathLength(root, 0); if isExternal (v) return n return (pathLength(leftChild (v), n + 1) + pathLength(rightChild (v), n + 1) + n)

Euler Tour Traversal +  - 2 5 1 3 L B R Generic traversal of a binary tree Includes a special cases the preorder, postorder and inorder traversals Walk around the tree and visit each node three times: on the left (preorder) from below (inorder) on the right (postorder) +  - 2 5 1 3 L B R

Euler Tour Traversal eulerTour(node v) { perform action for visiting node on the left; if v is internal then eulerTour(v->left); perform action for visiting node from below; eulerTour(v->right); perform action for visiting node on the right; }

Tree

Binary Tree sebuah pengorganisasian secara hirarki dari beberapa buah simpul, dimana masing-masing simpul tidak mempunyai anak lebih dari 2. Simpul yang berada di bawah sebuah simpul dinamakan anak dari simpul tersebut. Simpul yang berada di atas sebuah simpul dinamakan induk dari simpul tersebut.

Binary Tree

Istilah dalam Tree

Struktur Binary Tree Masing-masing simpul dalam binary tree terdiri dari tiga bagian yaitu sebuah data dan dua buah pointer yang dinamakan pointer kiri dan kanan.

Deklarasi Tree typedef char typeInfo; typedef struct Node tree; typeInfo info; tree *kiri; /* cabang kiri */ tree *kanan; /* cabang kanan */ };

Pembentukan Tree Dapat dilakukan dengan dua cara : rekursif dan non rekursif Perlu memperhatikan kapan suatu node akan dipasang sebagai node kiri dan kapan sebagai node kanan. Misalnya ditentukan, node yang berisi info yang nilainya “lebih besar” dari parent akan ditempatkan di sebelah kanan dan yang “lebih kecil” di sebelah kiri. Sebagai contoh jika kita memiliki informasi “HKACBLJ” maka pohon biner yang terbentuk

Pembentukan Tree

Pembentukan Tree Langkah-langkah Pembentukan Binary Tree 1. Siapkan node baru - alokasikan memory-nya - masukkan info-nya - set pointer kiri & kanan = NULL 2. Sisipkan pada posisi yang tepat - penelusuran  utk menentukan posisi yang tepat; info yang nilainya lebih besar dari parent akan ditelusuri di sebelah kanan, yang lebih kecil dari parent akan ditelusuri di sebelah kiri - penempatan  info yang nilainya lebih dari parent akan ditempatkan di sebelah kanan, yang lebih kecil di sebelah kiri

Metode Traversal Salah satu operasi yang paling umum dilakukan terhadap sebuah tree adalah kunjungan (traversing) Sebuah kunjungan berawal dari root, mengunjungi setiap node dalam tree tersebut tepat hanya sekali Mengunjungi artinya memproses data/info yang ada pada node ybs Kunjungan bisa dilakukan dengan 3 cara: 1. Preorder 2. Inorder 3. Postorder Ketiga macam kunjungan tersebut bisa dilakukan secara rekursif dan non rekursif

Preorder Kunjungan preorder, juga disebut dengan depth first order, menggunakan urutan: Cetak isi simpul yang dikunjungi Kunjungi cabang kiri Kunjungi cabang kanan

Preorder void preorder(pohon ph) { if (ph != NULL) printf("%c ", ph->info); preorder(ph->kiri); preorder(ph->kanan); }

Preorder

Inorder Kunjungan secara inorder, juga sering disebut dengan symmetric order, menggunakan urutan: Kunjungi cabang kiri Cetak isi simpul yang dikunjungi Kunjungi cabang kanan

Inorder void inorder(pohon ph) { if (ph != NULL) inorder(ph->kiri); printf("%c ", ph->info); inorder(ph->kanan); }

Inorder

Postorder Kunjungan secara postorder menggunakan urutan: Kunjungi cabang kiri Kunjungi cabang kanan Cetak isi simpul yang dikunjungi

Postorder void postorder(pohon ph) { if (ph != NULL) postorder(ph->kiri); postorder(ph->kanan); printf("%c ", ph->info); }

Postorder

References http://www.cse.unt.edu/~huangyan/3110/Lectures /Trees.ppt