Definition ! A (rooted) tree is a structure defined on a finite set - - PDF document

definition
SMART_READER_LITE
LIVE PREVIEW

Definition ! A (rooted) tree is a structure defined on a finite set - - PDF document

Definition ! A (rooted) tree is a structure defined on a finite set of nodes that either: Trees Contains no nodes, or Contains a specially designated node called the root and k>=0 disjoint sets of nodes T 1 ,,T k , where


slide-1
SLIDE 1

1

Trees & Binary Trees

2

Definition

! A (rooted) tree is a structure defined

  • n a finite set of nodes that either:

– Contains no nodes, or

– Contains a specially designated node called the “root” and k>=0 disjoint sets of nodes T1,…,Tk, where each Ti is a tree. T1,…,Tk are called the subtrees of the root.

! Ordered tree: order of subtrees

matters.

A B E F M L K D G J N C H I

3

Treeminology

(Tree Terminology)

! Internal nodes and leaf nodes ! Parent/child relationship ! Siblings: children of the same parent ! Ancestor/descendant relationship ! Degree of a node: number of children

A B E F M L K D G J N C H I

4

Treeminology

(Tree Terminology)

! Path: a sequence n1,…,nk of nodes, s.t.

ni is the parent of ni+1.

! Tree height: length (number of edges)

  • f longest path from the root to a leaf

! Node depth: length of the path from

the root to the node

A B E F M L K D G J N C H I

slide-2
SLIDE 2

5

Binary Trees

! A binary tree T is a structure defined

  • n a finite set of nodes that either

– Contains no nodes, or – Is comprised of a root node, and two subtrees (left and right) each of which is also a binary tree. A B C F E D G A B C E D G F

6

More Terminology

! A full binary tree: each node has either

degree 0 (a leaf) or 2 (has exactly two non-empty children).

! A complete binary tree: a full binary

tree in which all leaves have the same depth.

A B C E D G F A B C E D G F

full: complete:

7

Treevia (Tree Facts)

! How many leaf nodes are there in a

complete binary tree of height h?

! What is the number of internal nodes

in such a tree?

! What is the total number of nodes? ! How many leaf nodes are there in a

full binary tree of height h?

! How tall can a full binary tree with n

leaf nodes be?

8

Array Implementation

A B C E D G F A B D C E F G

slide-3
SLIDE 3

9

Array Implementation

class BinaryTreeA { private int size; private Item[] items; // 0..size-1 int leftChild(int i) { return 2*i + 1; } int rightChild(int i) { return 2*i + 2; } int parent(int i) { return floor((i - 1) / 2); } }

10

Pointer-Based Implementation

class BinaryTree { private Item item; private BinaryTree left, right; BinaryTree leftChild() { return left; } BinaryTree rightChild() { return right; } boolean isLeaf() { return (left == null && right == nul); } }

11

Binary Tree Traversal

! Inorder: visit left subtree, then

the root, then the right subtree.

! Preorder: visit root, left, right. ! Postorder: visit left, right, root.

void inOrder(BinaryTree t) { if (t.leftChild() != null) inOrder(t.leftChild()); doStuff(t); if (t.rightChild() != null) inOrder(r.rightChild()); }