Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -

Similar presentations


Presentation on theme: "CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -"— Presentation transcript:

1 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Trees in general -- terminology Binary trees -- properties, expression trees Array and linked representations CSE 373, Copyright S. Tanimoto, Binary Trees -

2 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Trees in General A Tree T is a set (possibly empty) of nodes, and a binary relation on this set called hasChild, such that there is a designated node called the root, and every node is reachable from the root by exactly one path of links in this relation. Alternatively we can define (as in Sahni): A tree t is a finite nonempty set of elements. One of these elements is called the root, and the remaining elements (if any) are partitioned into trees, which are called the subtrees of t. CSE 373, Copyright S. Tanimoto, Binary Trees -

3 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Example Tree n1 n2 n3 n4 n5 n6 n7 Connected (from the root), acyclic, root has indegree 0, all other nodes have indegree 1. A node with outdegree 0 is a leaf. Otherwise it’s an internal node. If (ni, nj) is in RT then ni is the parent of nj and nj is a child of ni The root is at depth 0. If n is at depth d, then a child of n is at depth d+1. A leaf has height 0. The height of an internal node is 1 + the maximum of the heights of its children. The height* of a tree is 1 + height of its root. The children of a node are considered unordered. The links are called edges or arcs or branches. (*) Note difference from Sahni’s definition of height. CSE 373, Copyright S. Tanimoto, Binary Trees -

4 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Set of nodes. Binary relation on nodes. (n, m) in RT means n is the parent of m and m is a child of n. There is a designated node called the root. The root has indegree 0. Every other node has indegree 1. Each node has outdegree 0, 1, or 2. Each child is either a left child or a right child. Each node has at most one left child and one right child. CSE 373, Copyright S. Tanimoto, Binary Trees -

5 Properties of Binary Trees
If T has k nodes, then it has k-1 edges. (This is actually true for any kind of tree.) A full binary tree of height h is a binary tree whose root has height h and which contains 2h+1-1 nodes. A complete binary tree with k nodes consists of a full binary tree plus 0 or more nodes that are children of the (leftmost) deepest elements of the full tree. (A complete binary tree can be thought of as a “prefix” of some full binary tree, scanning elements in level order.) CSE 373, Copyright S. Tanimoto, Binary Trees -

6 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Expression Tree + - * x 1 1 5 7 Inorder traversal produces: ((x - 1) + (5 * 7)) CSE 373, Copyright S. Tanimoto, Binary Trees -

7 CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Tree Traversal Preorder: Visit the root, visit the left subtree, visit the right subtree + - x 1 * 5 7 Inorder: Visit the left subtree, visit the root, visit the right subtree ((x - 1) + (5 * 7)) Postorder: Visit the left subtree, visit the right subtree, visit the root. x * + CSE 373, Copyright S. Tanimoto, Binary Trees -

8 Array Based Representation
A “Full” Binary Tree + - * x 1 1 5 7 - x 1 5 7 + * 1 2 3 4 5 6 CSE 373, Copyright S. Tanimoto, Binary Trees -

9 Another “Complete” Binary Tree
+ - ! x 1 1 5 - x 1 5 + ! 1 2 3 4 5 CSE 373, Copyright S. Tanimoto, Binary Trees -

10 An “Incomplete” Binary Tree
+ - ~ x 1 1 7 - x 1 7 + ~ 1 2 3 4 5 6 CSE 373, Copyright S. Tanimoto, Binary Trees -

11 Linked Representation
+ - ~ x 1 7 CSE 373, Copyright S. Tanimoto, Binary Trees -


Download ppt "CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -"

Similar presentations


Ads by Google