这一篇我们主要是通过二叉树的括号表示法来构建一颗二叉树
1、二叉树的构建(括号表示法)
1)、使用demo
@Test
public void stackBinaryTree()
{
StackBinaryTree stackBinaryTree = new StackBinaryTree("A(B(D(,G)),C(E,F))");
// StackBinaryTree.Node<Character> node = stackBinaryTree.findNode('B');
// System.out.println(node.getLeft() == null ? "null" : node.getLeft().getValue());
// System.out.println(node.getRight() == null ? "" : node.getRight().getValue());
System.out.println(stackBinaryTree.traversPrint());
System.out.println("------pre--------");
System.out.println(stackBinaryTree.preTraversStr());
System.out.println("------in--------");
System.out.println(stackBinaryTree.inTraversStr());
System.out.println("------after--------");
System.out.println(stackBinaryTree.afterTraversStr());
System.out.println("------gradation--------");
System.out.println(stackBinaryTree.gradationTraversStr());
}
A(B(D(,G)),C(E,F))
------pre--------
ABDGCEF
------in--------
DGBAECF
------after--------
GDBEFCA
------gradation--------
ABCDEFG
2)、树的节点
public class Node<E>
{
private E value;
private Node left;
private Node right;
public Node(E value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public E getValue() {
return value;
}
}
3)、树的构建
public StackBinaryTree(String treeExpression)
{
this.buildTreeByExpression(treeExpression);
}
private void buildTreeByExpression(String treeExpression)
{
Stack<Node<Character>> nodeStack = new Stack<>();
int length = treeExpression.length();
Node<Character> nowNode = null;
int flag = 1;
for (int i = 0; i < length; i++) {
Character nowChar = treeExpression.charAt(i);
if (nowChar == '(')
{
flag = 1;
nodeStack.push(nowNode);
}
else if (nowChar == ',')
{
flag = 2;
}
else if (nowChar == ')')
{
nodeStack.pop();
}
else
{
nowNode = new Node(nowChar);
if (Objects.isNull(parent))
{
parent = nowNode;
}
else if (flag == 1)
{
Node parentNode = nodeStack.pop();
parentNode.left = nowNode;
nodeStack.push(parentNode);
}
else
{
Node parentNode = nodeStack.pop();
parentNode.right = nowNode;
nodeStack.push(parentNode);
}
}
}
}
? 这里主要是使用了栈结构的先进后出。
4)、树的再次遍历打印
public String traversPrint()
{
if (Objects.isNull(parent))
{
return null;
}
StringBuffer buffer = new StringBuffer();
travers(parent,buffer);
return buffer.toString();
}
public void travers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
if (Objects.nonNull(node))
{
buffer.append(node.value);
}
if (Objects.nonNull(node.left) || Objects.nonNull(node.right))
{
buffer.append("(");
travers(node.left,buffer);
if (Objects.nonNull(node.right))
{
buffer.append(",");
}
travers(node.right,buffer);
buffer.append(")");
}
}
返回结果:
A(B(D(,G)),C(E,F))
5)、树的查找
public Node findNode(Character e)
{
return findNode(parent,e);
}
public Node findNode(Node node,Character e)
{
if (node == null)
{
return null;
}
if (node.value.equals(e))
{
return node;
}
Node leftNode = findNode(node.left, e);
if(leftNode == null)
{
return findNode(node.right,e);
}
return leftNode;
}
6)、树的先序遍历
public String preTraversStr()
{
StringBuffer buffer = new StringBuffer();
preTravers(parent,buffer);
return buffer.toString();
}
public void preTravers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
buffer.append(node.value);
preTravers(node.left,buffer);
preTravers(node.right,buffer);
}
? 这里主要是使用的递归:
------pre--------
ABDGCEF
7)、树的中序遍历
public String inTraversStr()
{
StringBuffer buffer = new StringBuffer();
inTravers(parent,buffer);
return buffer.toString();
}
public void inTravers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
inTravers(node.left,buffer);
buffer.append(node.value);
inTravers(node.right,buffer);
}
------in--------
DGBAECF
8)、树的后序遍历
public String afterTraversStr()
{
StringBuffer buffer = new StringBuffer();
afterTravers(parent,buffer);
return buffer.toString();
}
public void afterTravers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
afterTravers(node.left,buffer);
afterTravers(node.right,buffer);
buffer.append(node.value);
}
------after--------
GDBEFCA
9)、树的层次遍历
public String gradationTraversStr()
{
if (parent == null)
{
return "";
}
ArrayDeque<Node> deque = new ArrayDeque<>();
StringBuffer buffer = new StringBuffer();
deque.add(parent);
while (!deque.isEmpty())
{
Node node = deque.pop();
buffer.append(node.value);
if (Objects.nonNull(node.left))
{
deque.add(node.left);
}
if (Objects.nonNull(node.right))
{
deque.add(node.right);
}
}
return buffer.toString();
}
? 这里主要是利用队列先进先出的特性:
------gradation--------
ABCDEFG
10)、整体代码
public class StackBinaryTree {
private Node<Character> parent;
public StackBinaryTree(String treeExpression)
{
this.buildTreeByExpression(treeExpression);
}
private void buildTreeByExpression(String treeExpression)
{
Stack<Node<Character>> nodeStack = new Stack<>();
int length = treeExpression.length();
Node<Character> nowNode = null;
int flag = 1;
for (int i = 0; i < length; i++) {
Character nowChar = treeExpression.charAt(i);
if (nowChar == '(')
{
flag = 1;
nodeStack.push(nowNode);
}
else if (nowChar == ',')
{
flag = 2;
}
else if (nowChar == ')')
{
nodeStack.pop();
}
else
{
nowNode = new Node(nowChar);
if (Objects.isNull(parent))
{
parent = nowNode;
}
else if (flag == 1)
{
Node parentNode = nodeStack.pop();
parentNode.left = nowNode;
nodeStack.push(parentNode);
}
else
{
Node parentNode = nodeStack.pop();
parentNode.right = nowNode;
nodeStack.push(parentNode);
}
}
}
}
public Node findNode(Character e)
{
return findNode(parent,e);
}
public Node findNode(Node node,Character e)
{
if (node == null)
{
return null;
}
if (node.value.equals(e))
{
return node;
}
Node leftNode = findNode(node.left, e);
if(leftNode == null)
{
return findNode(node.right,e);
}
return leftNode;
}
public String preTraversStr()
{
StringBuffer buffer = new StringBuffer();
preTravers(parent,buffer);
return buffer.toString();
}
public void preTravers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
buffer.append(node.value);
preTravers(node.left,buffer);
preTravers(node.right,buffer);
}
public String gradationTraversStr()
{
if (parent == null)
{
return "";
}
ArrayDeque<Node> deque = new ArrayDeque<>();
StringBuffer buffer = new StringBuffer();
deque.add(parent);
while (!deque.isEmpty())
{
Node node = deque.pop();
buffer.append(node.value);
if (Objects.nonNull(node.left))
{
deque.add(node.left);
}
if (Objects.nonNull(node.right))
{
deque.add(node.right);
}
}
return buffer.toString();
}
public String afterTraversStr()
{
StringBuffer buffer = new StringBuffer();
afterTravers(parent,buffer);
return buffer.toString();
}
public void afterTravers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
afterTravers(node.left,buffer);
afterTravers(node.right,buffer);
buffer.append(node.value);
}
public String inTraversStr()
{
StringBuffer buffer = new StringBuffer();
inTravers(parent,buffer);
return buffer.toString();
}
public void inTravers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
inTravers(node.left,buffer);
buffer.append(node.value);
inTravers(node.right,buffer);
}
public String traversPrint()
{
if (Objects.isNull(parent))
{
return null;
}
StringBuffer buffer = new StringBuffer();
travers(parent,buffer);
return buffer.toString();
}
public void travers(Node node,StringBuffer buffer)
{
if (Objects.isNull(node))
{
return;
}
if (Objects.nonNull(node))
{
buffer.append(node.value);
}
if (Objects.nonNull(node.left) || Objects.nonNull(node.right))
{
buffer.append("(");
travers(node.left,buffer);
if (Objects.nonNull(node.right))
{
buffer.append(",");
}
travers(node.right,buffer);
buffer.append(")");
}
}
public class Node<E>
{
private E value;
private Node left;
private Node right;
public Node(E value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public E getValue() {
return value;
}
}
public Node getParent()
{
return parent;
}
}
|