Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 other trees form new a tree; the node is the root of the tree. null

2 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 = 2 1 2 4 height of b = 4 1 2 height of c = 1 3 1

3 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 0. 2. 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

4 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? d7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 12 345 12 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 d6 0 1 2 3 4 5 6 height = 4 6

5 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 4 -1

6 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).

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

8 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; }.....

9 2/11/20169IT 179 Calculate the size of the tree, i.e., the number of nodes nrnr nlnl X root = null; 0 n l + 1 + 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); }

10 2/11/201610IT 179 Count how many k’s in the tree nrnr nlnl X root = NULL; 0 if x = k, n l + 1 + 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); }

11 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 1 2 3 height = 5 4 5 // 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); }

12 2/11/201612IT 179 A random binary tree: 24 13 23 2 4 34 13 1734 Randomly insert data into a binary tree

13 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;

14 2/11/201614 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

15 2/11/201615IT 179 How to remove data from a binary tree? 24 13 23 2 4 34 13 1734 remove 13

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

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

18 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

19 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

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

21 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............... }

22 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); }

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


Download ppt "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."

Similar presentations


Ads by Google