880 likes | 1.07k Views
Trees. Briana B. Morrison Adapted from Alan Eugenio. Topics. Trees Binary Trees Definitions (full, complete, etc.) Expression Trees Traversals. 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 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
A C D B E G H F K I J Tree Terminology • Root: node without parent (A) • Internal node: node with at least one child (A, B, C, F) • External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D) • Ancestors of a node: parent, grandparent, grand-grandparent, etc. • Depth of a node: number of ancestors • Height of a tree: maximum depth of any node (3) • Descendant of a node: child, grandchild, grand-grandchild, etc. • Subtree: tree consisting of a node and its descendants subtree 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
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? -1 Binary Trees
Tree Node Level and Path Length Binary Trees
Tree Node Level and Path Length – Depth Discussion Is this tree complete? Is it full? Binary Trees
Tree Node Level and Path Length – Depth Discussion Is this tree complete? Is it full? What is its height? Binary Trees
Tree Node Level and Path Length – Depth Discussion Is this tree complete? Is it full? What is its height? Binary Trees
A B C D E R S F G L THIS ASSOCIATION SUGGESTS THAT A COMPLETE BINARY TREE CAN BE STORED IN AN ARRAY: Binary Trees