binary tree
一、中序线索化
二叉树节点定义:
class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
int isleftChild = 1;//0:线索 1:左孩子
int isrightChild = 1; public TreeNode(int val) {
this.val = val;
}
}
在中序遍历的过程中完成线索化
//preNode始终指向中序序列中前一个访问的节点
private static TreeNode preNode = null;//只能用全局变量的形式来记录。 //curNode始终指向中序序列中当前访问的节点
public static TreeNode inIOrderThread(TreeNode curNode) {
if (curNode == null)
return null; //线索化左子树
InClueTree(curNode.left); //线索化当前节点
if (curNode.left == null) {
curNode.isleftChild = 0;
curNode.left = preNode;
}
if (preNode != null && preNode.right == null) {
preNode.isrightChild = 0;
preNode.right = curNode;
}
preNode = curNode;// //线索化右子树
InClueTree(curNode.right); return curNode;
}
遍历中序线索二叉树
public static void inOrderThreadTravel(TreeNode root) {
while (root != null) {
System.out.print(root.val + " ");
//存在线索,root的直接后继就是root.rigth;
if (root.isrightChild == 0) {
root = root.right;
}
//不存在线索的时候一定存在孩子节点,则root的直接后继就是其右子树的中序第一个元素。
else {
root = root.right;
while (root.left != null && root.isleftChild == 1) {
root = root.left;
}
}
}
}
二、表达式求值
有两个表达式
(4 + (13 / 5)) = 6 (a)
((2 + 1) * 3) = 9 (b)
对应的两个表达式树(a)(b)
特点:数字都在叶子节点,运算符都在根节点。
+ *
/ \ / \
4 / + 3
/ \ / \
13 5 2 1 (a) (b)
来看一下表达式树的前中后三种顺序遍历结果;
中序:
4 + 13 / 5 --- (a)
2 + 1 * 3 --- (b)
可以看出,表达式树的中序遍历结果就是正常书写顺序,也是计算机可以直接求解的方式。
后序:
4 13 5 / + --- (a)
2 1 + 3 * --- (b)
此时遍历结果非书写顺序,计算机也不能直接求解,非要按照这个顺序用计算机求解,怎么办?
解决方案:栈
按照遍历顺序对元素如下的操作:
1、如果元素是数字,入栈
2、如果元素是操作符不入栈,反而弹栈两个元素a,b;将a作为运算符的左操作数,b作为右操作数计算得到结果c;将结果c入栈。
3、重复上述操作,直到没有元素时,此时栈中一定只有一个元素,将其返回。
前序:
+ 4 / 13 5 --- (a)
* + 2 / 3 --- (b)
此时遍历结果非书写顺序,计算机也不能直接求解,非要按照这个顺序用计算机求解,怎么办?
解决方案:栈
与后序列操作类似,只不过按照遍历顺序的逆序,为什么是这样呢?
因为:栈的特点可以暂存之前遇到的信息,在后续操作中可以从栈中取出之前保存的信息;
四则运算符都是二元运算符,因此一次计算的顺利完成需要3个信息,两个数字,一个运算符号;
因此遇到数字时候压栈,遇到操作符时候不压栈,然而弹出两个元素进行计算,这是合理的
而观察表达式树我们发现,叶子节点全都是数字,跟节点全都是操作符号,在进一步可以这么想,父节点都是操作符,孩子节点都是数字(当然直观来看不是这样的,如表达式树(a)中根节点“+”的右孩子明明是“/”;其实在根节点“+”真正计算的时候,13 和 5的父节点“/”早就是新的数字了);结合树的遍历特点,要么遍历完孩子节点才遍历根节点(后序),要么遍历完孩子节点才遍历根节点(前序),总之,孩子节点(数字)总在父节点(符号)的一侧。不管是先序还是后序,我们都统一为先处理孩子节点,再处理父节点,后序顺序中,孩子节点刚好在父节点之前,因此不做顺序调整,而先序遍历的时候,孩子节点均在父节点之后,因此需要逆序调整。
import java.util.Stack; public class Solution {
public int evalRPN(String[] tokens) {
int res = 0;
if (tokens == null || tokens.length == 0)
return res;
int len = tokens.length;
Stack<Integer> stack = new Stack();
int i = 0;
for (; i < len; i++) {
if (isNum(tokens[i]))
stack.push(Integer.parseInt(tokens[i]));
else {
int a = stack.pop();
int b = stack.pop();
//除法有顺序要求哦
stack.push(operator(b, a, tokens[i]));
}
}
if (i == len)
return stack.pop();
return res;
} private boolean isNum(String str) {
boolean b = false;
try {
Integer.parseInt(str);
b = true;
} catch (Exception e) {
}
return b;
} private int operator(int a, int b, String s) {
int res = 0;
switch (s) {
case "+": {
res = a + b;
break;
}
case "-": {
res = a - b;
break;
}
case "*": {
res = a * b;
break;
}
case "/": {
res = a / b;
break;
}
}
return res;
}
}
binary tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- Scrum立会报告+燃尽图(3)选题
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2193 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...
- L178 smart meter watchdog
There is "no realistic prospect" of the government meeting its own deadline to install sma ...
- SpringBoot启动报:Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
使用spring boot对项目改造,启动报错: Caused by: java.lang.IllegalArgumentException: At least one JPA metamodel m ...
- BZOJ5312: 冒险【线段树】【位运算】
Description Kaiser终于成为冒险协会的一员,这次冒险协会派他去冒险,他来到一处古墓,却被大门上的守护神挡住了去路,守护神给出了一个问题, 只有答对了问题才能进入,守护神给出了一个自然数 ...
- Codeforces 990B :Micro-World
B. Micro-World time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- hibernate之xml映射文件关系维护,懒加载,级联
一:关系维护 --->inverse默认值false,表示不放弃关系的维护. --->inverse="true"配置在那一端,表示那一端xml对应的po放弃关系的 ...
- Git核心概念
Git作为流行的分布式版本管理系统,用好它要理解下面几个核心的概念. 1.Git保寸的是文件完整快照,而不是差异变化或者文件补丁.每次提交若文件有变化则会指向上一个版本的指针而不重复生成副本. Git ...
- CentOS 6安装php加速软件Zend Guard(转)
(尚未验证) PHP5.3以上的版本不再支持Zend Optimizer,已经被全新的 Zend Guard Loader 取代,下面是安装Zend Guard具体步骤,以下操作均在终端命令行执行 1 ...
- 部署Web API后Delete请求总是报 405(Method Not Allowed)解决办法
WebDAV 安装IIS的时候如果选择了WebDAV(Web Distribution Authorization Versioning) Publish,则所有的 ...
- SQL Server中的事务与其隔离级别之脏读, 未提交读,不可重复读和幻读
原本打算写有关 SSIS Package 中的事务控制过程的,但是发现很多基本的概念还是需要有 SQL Server 事务和事务的隔离级别做基础铺垫.所以花了点时间,把 SQL Server 数据库中 ...