Presentation is loading. Please wait.

Presentation is loading. Please wait.

Depict the Tree Structure in a picture

Similar presentations


Presentation on theme: "Depict the Tree Structure in a picture"— Presentation transcript:

1 Depict the Tree Structure in a picture
height of d10 = 1 Terminologies The root d11 the links are directed height of d9 = 4 1 Ancestors d9 d10 1 2 leaf Descendants Parent d8 2 depth of d8=2 Children d4 d3 d5 d6 d7 3 leaf leaf leaf leaf d1 d2 leaf Siblings 4 leaf 11/8/2018 ITK 168

2 More Terminologies In-degree: (of a node) the number of links pointing to the node (always 1 or 0); the root is the only node with in-degree 0. 2. Out-degree: (of a node) the number of links 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 23 d4 27 d8 d3 d5 d6 d7 d4 d1 d2 Arity = 5 11/8/2018 ITK 168

3 Recursive Definition of Tree Structures (not so precise)
11/8/2018 No rule is violated but we don’t like it An empty collection is a tree A single node is a tree A node being linked to a finite number of trees forms a tree. Is this definition good enough ? 23 d4 27 d4 d8 d1 d2 d3 d5 d6 d7 11/8/2018 ITK 168

4 Recursive Definition of Tree Structures
Empty is a tree; the root is null A single node is a tree; the node is the root of the tree. A node points to a finite number of roots of some trees form a tree; the node is the root of the tree. (so, the node can’t point to a node that is not the root.) 23 d4 27 d4 d8 d1 d2 d3 d5 d6 d7 11/8/2018 ITK 168

5 Tree Implementation Linked Lists data field
11/8/2018 Tree Implementation Linked Lists data data field enough fields for pointers pointing the children Usually, using the number of degree of the tree. Fixed, if the degree is small, e.g., binary tree (degree is 2). 11/8/2018 ITK 168

6 Tree Implementation using Array the degree is small
1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 d1 d2 d3 d4 d5 d6 d7 d1 d2 d3 d4 d5 d6 Array is a good choice if: the degree is small the tree is rather full the size does not change too often height = 4 degree = 2 size = 2height - 1 d1 2 1 d2 d3 5 d4 d5 d6 3 4 6 12 1st child (left-child) of i is *i+1; 2nd child (right-child) of i is 2*i+2. The parent of i is (i-1)/2 d7 What is the advantage and disadvantage of using array? 11/8/2018 ITK 168

7 A full-tree or complete tree is a perfect example for using array
d1 d1 d2 d3 d2 d3 d7 d4 d5 d6 d7 d4 d5 d6 d8 d10 d8 d9 d10 d11 d12 d13 d14 d15 d9 complete tree full-tree 11/8/2018 ITK 168

8 A random binary tree: Randomly insert data into a binary tree 2 13 34
24 23 4 17 34 11/8/2018 ITK 168

9 A Binary Tree Node in Java
A data field Two pointers to the left-child and right-child A Binary Tree Node in Java public class BinaryTree <E extends Comparable<E>> { /********** This is an inner class for tree nodes************/ private static class TNode<E> { private E data; private TNode<E> left,right; private TNode(E data, TNode<E> left, TNode<E> right) {//Construct a node with two children this.data = data; this.left = left; this.right = right; } /********** This is the end of the inner class Node<E> *******/ private TNode<E> root; public BinaryTree() { root = null; ..... 11/8/2018 ITK 168

10 Add a new data to the BinaryTree
//add data to this BST, return false if data is already in the tree public boolean add(E data) { if (root != null) return add(root, data); root = new TNode<E>(data, null, null); return true; } A private overloaded add method root is private; 11/8/2018 ITK 168

11 Randomly Insert (add) a data into a binary tree:
toss a coin head private boolean add(TNode<E> t, E data){ if (Math.random() < 0.5) // toss a coin if (t.left == NULL) t.left = new TNode<E>(data,null,null); else add(t.left, data); else if (t.right == NULL) t.right = new TNode<E>(data,null,null); else add(t.right,data); return true; } 11/8/2018 ITK 168

12 Tree traversal: preorder, inorder, postorder
X L R X X L R L R inorder: L  X  R preorder: X  L  R postorder: L  R  X 11/8/2018 ITK 168

13 System.out.print(t.data+", ");
// Three ways of traversals ..... public void inorder() { inorder(root); } public void preorder() { preorder(root); } public void postorder() { postorder(root); } private void inorder(TNode<E> 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 private void preorder(TNode<E> 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<E> 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 11/8/2018 ITK 168

14 Calculate the size of the tree, i.e., the number of nodes
X Ln Rn root = NULL; Ln Rn // Return the number nodes in the tree ..... public int size() { return size(root); } private int size(TNode<E> t) { if (t==NULL) return 0; return 1 + size(t.left) + size(t.right); } 11/8/2018 ITK 168

15 Count how many k’s in the tree
X root = NULL; if x = k, Ln Rn ; otherwise, Ln + Rn Ln Rn // Return the number of k’s in the tree ..... public int count(E k) { return count(root, k); } private int count(TNode<E> t, E k) { if (t==NULL) return 0; return (k.compareTo(t.data) == 0 ? 1 : 0) + count(t.left, k) + count(t.right, k); } 11/8/2018 ITK 168

16 Height of a Tree: The number of nodes in the longest path from the root.
1 // Return the heights of the tree ..... public int height() { return height(root); } private height(TNode<T> t) { if (t==NULL) return 0; int l = height(t.left); int r = height(t.right); return 1 + (l > r ? l : r); } d10 d9 2 3 d8 d4 d5 4 d1 d2 5 11/8/2018 ITK 168

17 Copy and Clone root root d8 d8 d3 d5 d3 d5 d1 d2 d1 d2 11/8/2018
ITK 168


Download ppt "Depict the Tree Structure in a picture"

Similar presentations


Ads by Google