1.02k likes | 1.25k Views
Trees. Briana B. Morrison Adapted from Alan Eugenio. Topics. Trees Binary Trees Definitions (full, complete, etc.) Expression Trees Traversals Implementation. Tree Structures. Computers”R”Us. Sales. Manufacturing. R&D. US. International. Laptops. Desktops. Europe. Asia. Canada.
E N D
Trees Briana B. Morrison Adapted from Alan Eugenio
Topics • Trees • Binary Trees • Definitions (full, complete, etc.) • Expression Trees • Traversals • Implementation Binary Trees
Tree Structures Binary Trees
Computers”R”Us Sales Manufacturing R&D US International Laptops Desktops Europe Asia Canada What is a Tree • In computer science, a tree is an abstract model of a hierarchical structure • A tree consists of nodes with a parent-child relation • Applications: • Organization charts • File systems • Programming environments Binary Trees
Terminology • First let’s learn some terminology about trees… Binary Trees
Tree Structures Binary Trees
Tree Structures Binary Trees
Tree Structures Binary Trees
50, 80, 90, 84 Binary Trees
BINARY TREES Binary Trees
Binary Tree Definition • A binary tree T is a finite set of nodes with one of the following properties: • (a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) • (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree TL and the right subtreeTR. The nodes in T consist of node R and all the nodes in TL and TR. Binary Trees
Binary Tree • Applications: • arithmetic expressions • decision processes • searching • A binary tree is a tree with the following properties: • Each internal node has two children • The children of a node are an ordered pair • We call the children of an internal node left child and right child • Alternative recursive definition: a binary tree is either • a tree consisting of a single node, or • a tree whose root has an ordered pair of children, each of which is a binary tree A C B D E F G I H Binary Trees
Examples of Binary Trees • Expression tree • Non-leaf (internal) nodes contain operators • Leaf nodes contain operands • Huffman tree • Represents Huffman codes for characters appearing in a file or stream • Huffman code may use different numbers of bits to encode different characters • ASCII or Unicode uses a fixed number of bits for each character Binary Trees
Examples of Huffman Tree Code for b = 100000 Code for w = 110001 Code for s = 0011 Code for e = 010 Binary Trees
HOW CAN WE DEFINE leaves(t), THE NUMBER OF LEAVES IN THE BINARY TREE t? RECURSIVELY! Binary Trees
height(t) = ? To distinguish the height of an empty tree from the height of a single-item tree, What should the height of an empty tree be? Binary Trees
Tree Node Level and Path Length Binary Trees
Depth Example Depth 0 (root) Depth 1 Depth 2 Depth 1 Binary Trees
Tree Node Level and Path Length – Depth Discussion Is this tree complete? Is it full? Binary Trees