Download presentation
Presentation is loading. Please wait.
1
Recursive Definition of Tree Structures
Empty is a tree; the root is null A node pointing to a finite number of the roots of some other trees is the root of a tree. null null null null null null null null null null null null null null null 5/25/2018 IT 179
2
Terminology the links is directed, always pointing down The root 1 1 1
Ancestors a 1 Descendants height of b = 4 b c 1 1 height of c = 1 2 leaf depth of d =2 Parent of e,f,g,h,i 2 d Children of d Siblings e f g h i 3 leaf leaf leaf Siblings j k Siblings m n k 4 leaf leaf leaf leaf leaf 5/25/2018 IT 179
3
More Terminologies 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 a b c d e f g h i j k 5/25/2018 IT 179
4
Tree Implementation Using Array Array is a good choice if:
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 Array is a good choice if: the degree is small the tree is rather full the size does not change too often height = 3 degree = 2 size = 2height - 1 d1 1 2 d2 d3 d7 What is the advantage and disadvantage of using array? d4 d5 d6 3 4 5 6 12 d7 1st child (left-child) of i is 2*i+1; 2nd child (right-child) of i is 2*i+2. The parent of i is (i-1)/2 height = 4 5/25/2018 IT 179
5
A perfect-tree or complete tree is a perfect example for using array
d1 d1 d2 d3 d2 d3 d7 d4 d4 d5 d6 d5 d6 d7 d8 d8 d9 d10 d11 d12 d13 d14 d15 d9 d10 complete tree full-tree (no single child) perfect-tree 24-1 5/25/2018 IT 179
6
Tree Implementation: linked lists arrays
5/25/2018 Tree Implementation: linked lists arrays Links data data field 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). 5/25/2018 IT 179
7
Binary Tree (2 degree tree)
5/25/2018 Linked Lists Binary Tree (2 degree tree) data Data field Right and Left Children 2 34 13 4 34 5/25/2018 IT 179
8
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 <T> { /********** 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 TNode<E> *******/ private TNode<T> root; public BinaryTree() { root = null; ..... 5/25/2018 IT 179
9
Calculate the size of the tree, i.e., the number of nodes
X nl nr root = null; nl nr // 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); } 5/25/2018 IT 179
10
Count how many k’s in the tree
X root = NULL; if x = k, nl nr ; otherwise, nl + nr nl nr // 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); } 5/25/2018 IT 179
11
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 int 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); } b c 2 3 d e f 4 g h 5 5/25/2018 IT 179
12
A random binary tree: Randomly insert data into a binary tree 2 13 34
24 23 4 17 34 5/25/2018 IT 179
13
Add a new data to the BinaryTree
//add data to this BST, return false if data is already in the tree public void add(E data) { root = add(root, data); } A private overloaded add method; 5/25/2018 IT 179
14
Randomly Insert (add) a data into a binary tree:
toss a coin head data data private TNode<E> add(TNode<E> node, E data){ if (node == null) return new Node<E>(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; } 5/25/2018 IT 179
15
How to remove data from a binary tree?
2 13 34 13 24 23 4 17 34 remove 13 What is your strategy? 5/25/2018 IT 179
16
Syntax Trees of Arithmetic Expressions
+ * - * 3 + 3 2 1 3 5 3*(2+1)+(3-3*5) 5/25/2018 IT 179
17
Syntax Trees for Arithmetic Expressions
- + + * * - * 3 3 5 * 3 + 3 3 + 2 1 3 5 2 1 3*(2+1)+3-3*5 3*(2+1)+(3-3*5) 5/25/2018 IT 179
18
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 5/25/2018 IT 179
19
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 5/25/2018 IT 179
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.