分享

Java之数据结构基础、线性表、栈和队列、数组和字符串,树—

 niefeng2011 2014-01-07

       这部分内容作为计算机专业最基础的知识,几乎被所有企业选中用来作考题,因此,本章我们从本章开始,我们将从基础方面对数据结构进行讲解,内容主要是线性表,包括栈、队列、数组、字符串等,主要讲解基础知识,如概念及简单的实现代码,非线性结构我们在后面的文章中给出。

一、数据结构概念

    用我的理解,数据结构包含数据和结构,通俗一点就是将数据按照一定的结构组合起来,不同的组合方式会有不同的效率,使用不同的场景,如此而已。比如我们最常用的数组,就是一种数据结构,有独特的承载数据的方式,按顺序排列,其特点就是你可以根据下标快速查找元素,但是因为在数组中插入和删除元素会有其它元素较大幅度的移动,所以会带来较多的消耗,所以因为这种特点,使得数组适合:查询比较频繁,增、删比较少的情况,这就是数据结构的概念。数据结构包括两大类:线性结构和非线性结构,线性结构包括:数组、链表、队列、栈等,非线性结构包括树、图、表等及衍生类结构。本章我们先讲解线性结构,主要从数组、链表、队列、栈方面进行讨论,非线性数据结构在后面会继续讲解。

二、线性表

     线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。其基本操作主要有:

 1)MakeEmpty(L) 这是一个将L变为空表的方法
   2)Length(L) 返回表L的长度,即表中元素个数 
   3)Get(L,i) 这是一个函数,函数值为L中位置i处的元素(1≤i≤n)
   4)Prev(L,i) 取i的前驱元素
   5)Next(L,i) 取i的后继元素
   6)Locate(L,x) 这是一个函数,函数值为元素x在L中的位置
   7)Insert(L,i,x)在表L的位置i处插入元素x,将原占据位置i的元素及后面的元素都向后推一个位置
   8)Delete(L,p) 从表L中删除位置p处的元素
   9)IsEmpty(L) 如果表L为空表(长度为0)则返回true,否则返回false
   10)Clear(L)清除所有元素
   11)Init(L)同第一个,初始化线性表为空
   12)Traverse(L)遍历输出所有元素
   13)Find(L,x)查找并返回元素
   14)Update(L,x)修改元素
   15)Sort(L)对所有元素重新按给定的条件排序
   16) strstr(string1,string2)用于字符数组的求string1中出现string2的首地址

   不管采用哪种方式实现线性表,至少都应该具有上述这些基本方法,下面我会将下数据结构的基本实现方式。

三、基础数据结构

    数据结构是一种抽象的数据类型(ADT),可以这么说,我们可以采用任意的方式实现某种数据结构,只要符合将要实现的数据结构的特点,数据结构就是一种标准,我们可以采用不同的方式去实现,最常用的两种就是数组和链表(包括单链表、双向链表等)
数组是非常常见的数据类型,在任何一种语言里都有它的实现。
3.1我们这里采用Java来简单实现一下数组
数组是一种引用类型的对象,我们可以像下面这样的方式来声明数组:
  1. int a[];  
  2. int[] b;  
  3. int []c;  
  4. a = new int[10];  
总结起来,声明一个数组有基本的三个因素:类型、名称、下标,Java里,数组在格式上相对灵活,下标和名称可以互换位置,前三种情况我们可以理解为声明一个变量,后一种为其赋值。或者像下面这样,在声明的时候赋值:
  1. int c[] = {2,3,6,10,99};  
  2. int []d = new int[10];  
我稍微解释一下,其实如果只执行:int[] b,只是在栈上创建一个引用变量,并未赋值,只有当执行d = new int[10]才会在堆上真正的分配空间。上述第一行为静态初始化,就是说用户指定数组的内容,有系统计算数组的大小,第二行恰恰相反,用户指定数组的大小,由系统分配初始值,我们打印一下数组的初始值:
  1. int []d = new int[10];  
  2. System.out.println(d[2]);  
结果输出0,对于int类型的数组,默认的初始值为0.

但是,绝对不可以像下面这样:       int e[10] = new int[10];

无法通过编译,至于为什么,语法就是这样,这是一种规范,不用去想它。
我们可以通过下标来检索数组。下面我举个简单的例子,来说明下数组的用法。

  1. public static void main(String[] args) {  
  2.     String name[];  
  3.     name = new String[5];  
  4.     name[0] = "egg";  
  5.     name[1] = "erqing";  
  6.     name[2] = "baby";  
  7.     for (int i = 0; i < name.length; i++) {  
  8.         System.out.println(name[i]);  
  9.     }  
  10. }  
这是最简单的数组声明、创建、赋值、遍历的例子,下面写个增删的例子:

  1. package com.xtfggef.algo.array;  
  2. public class Array {  
  3.     public static void main(String[] args) {  
  4.         int value[] = new int[10];  
  5.         for (int i = 0; i < 10; i++) {  
  6.             value[i] = i;  
  7.         }  
  8.         // traverse(value);  
  9.         // insert(value, 666, 5);  
  10.         delete(value, 3);  
  11.         traverse(value);  
  12.     }  
  13.     public static int[] insert(int[] old, int value, int index) {  
  14.         for (int k = old.length - 1; k > index; k--)  
  15.             old[k] = old[k - 1];  
  16.         old[index] = value;  
  17.         return old;  
  18.     }  
  19.     public static void traverse(int data[]) {  
  20.         for (int j = 0; j < data.length; j++)  
  21.             System.out.print(data[j] + " ");  
  22.     }  
  23.     public static int[] delete(int[] old, int index) {  
  24.         for (int h = index; h < old.length - 1; h++) {  
  25.             old[h] = old[h + 1];  
  26.         }  
  27.         old[old.length - 1] = 0;  
  28.         return old;  
  29.     }  
  30. }  
简单写一下,主要想说明数组中删除和增加元素的原理:增加元素,需要将index后面的依次向后移动,然后将值插入index位置,删除则将后面的值依次向前移动,较简单。要记住:数组是表示相同类型的一类数据的集合,下标从0开始,就行了。

数组实现的线性表可以参考ArrayList,在JDK中附有源码,感兴趣的同学可以读读。

3.2下面我简单介绍下单链表

单链表是最简单的链表,有节点之间首尾连接而成,简单示意如下:


除了头节点,每个节点包含一个数据域一个指针域,除了头、尾节点,每个节点的指针指向下一个节点,下面我们写个例子操作一下单链表。
  1. package com.xtfggef.algo.linkedlist;  
  2. public class LinkedList<T> {  
  3.     /** 
  4.      * class node 
  5.      * @author egg 
  6.      * @param <T> 
  7.      */  
  8.     private static class Node<T> {  
  9.         T data;  
  10.         Node<T> next;  
  11.         Node(T data, Node<T> next) {  
  12.             this.data = data;  
  13.             this.next = next;  
  14.         }  
  15.         Node(T data) {  
  16.             this(data, null);  
  17.         }  
  18.     }  
  19.     // data  
  20.     private Node<T> head, tail;  
  21.     public LinkedList() {  
  22.         head = tail = null;  
  23.     }  
  24.     /** 
  25.      * judge the list is empty 
  26.      */  
  27.     public boolean isEmpty() {  
  28.         return head == null;  
  29.     }  
  30.     /** 
  31.      * add head node 
  32.      */  
  33.     public void addHead(T item) {  
  34.         head = new Node<T>(item);  
  35.         if (tail == null)  
  36.             tail = head;  
  37.     }  
  38.     /** 
  39.      * add the tail pointer 
  40.      */  
  41.     public void addTail(T item) {  
  42.         if (!isEmpty()) {   
  43.             tail.next = new Node<T>(item);  
  44.             tail = tail.next;  
  45.         } else {   
  46.             head = tail = new Node<T>(item);  
  47.         }  
  48.     }  
  49.     /** 
  50.      * print the list 
  51.      */  
  52.     public void traverse() {  
  53.         if (isEmpty()) {  
  54.             System.out.println("null");  
  55.         } else {  
  56.             for (Node<T> p = head; p != null; p = p.next)  
  57.                 System.out.println(p.data);  
  58.         }  
  59.     }  
  60.     /** 
  61.      * insert node from head 
  62.      */  
  63.     public void addFromHead(T item) {  
  64.         Node<T> newNode = new Node<T>(item);  
  65.         newNode.next = head;  
  66.         head = newNode;  
  67.     }  
  68.     /** 
  69.      * insert node from tail 
  70.      */  
  71.     public void addFromTail(T item) {  
  72.         Node<T> newNode = new Node<T>(item);  
  73.         Node<T> p = head;  
  74.         while (p.next != null)  
  75.             p = p.next;  
  76.         p.next = newNode;  
  77.         newNode.next = null;  
  78.     }  
  79.     /** 
  80.      * delete node from head 
  81.      */  
  82.     public void removeFromHead() {  
  83.         if (!isEmpty())  
  84.             head = head.next;  
  85.         else  
  86.             System.out.println("The list have been emptied!");  
  87.     }  
  88.     /** 
  89.      * delete frem tail, lower effect 
  90.      */  
  91.     public void removeFromTail() {  
  92.         Node<T> prev = null, curr = head;  
  93.         while (curr.next != null) {  
  94.             prev = curr;  
  95.             curr = curr.next;  
  96.             if (curr.next == null)  
  97.                 prev.next = null;  
  98.         }  
  99.     }  
  100.     /** 
  101.      * insert a new node 
  102.      * @param appointedItem 
  103.      * @param item 
  104.      * @return 
  105.      */  
  106.     public boolean insert(T appointedItem, T item) {  
  107.         Node<T> prev = head, curr = head.next, newNode;  
  108.         newNode = new Node<T>(item);  
  109.         if (!isEmpty()) {  
  110.             while ((curr != null) && (!appointedItem.equals(curr.data))) {  
  111.                 prev = curr;  
  112.                 curr = curr.next;  
  113.             }  
  114.             newNode.next = curr;   
  115.             prev.next = newNode;  
  116.             return true;  
  117.         }  
  118.         return false;   
  119.     }  
  120.     public void remove(T item) {  
  121.         Node<T> curr = head, prev = null;  
  122.         boolean found = false;  
  123.         while (curr != null && !found) {  
  124.             if (item.equals(curr.data)) {  
  125.                 if (prev == null)  
  126.                     removeFromHead();  
  127.                 else  
  128.                     prev.next = curr.next;  
  129.                 found = true;  
  130.             } else {  
  131.                 prev = curr;  
  132.                 curr = curr.next;  
  133.             }  
  134.         }  
  135.     }  
  136.     public int indexOf(T item) {  
  137.         int index = 0;  
  138.         Node<T> p;  
  139.         for (p = head; p != null; p = p.next) {  
  140.             if (item.equals(p.data))  
  141.                 return index;  
  142.             index++;  
  143.   
  144.         }  
  145.         return -1;  
  146.     }  
  147.     /** 
  148.      * judge the list contains one data 
  149.      */  
  150.     public boolean contains(T item) {  
  151.         return indexOf(item) != -1;  
  152.     }  
  153. }  
单链表最好玩儿的也就是增加和删除节点,下面的两个图分别是用图来表示单链表增、删节点示意,看着图学习,理解起来更加容易!

接下来的队列和栈,我们分别用不同的结构来实现,队列用数组,栈用单列表,读者朋友对此感兴趣,可以分别再用不同的方法实现。
四、队列
队列是一个常用的数据结构,是一种先进先出(First In First Out, FIFO)的结构,也就是说只能在表头进行删除,在表尾进行添加,下面我们实现一个简单的队列。

  1. package com.xtfggef.algo.queue;  
  2. import java.util.Arrays;  
  3. public class Queue<T> {  
  4.     private int DEFAULT_SIZE = 10;    
  5.     private int capacity;     
  6.     private Object[] elementData;     
  7.     private int front = 0;  
  8.     private int rear = 0;     
  9.     public Queue()  
  10.     {  
  11.         capacity = DEFAULT_SIZE;  
  12.         elementData = new Object[capacity];  
  13.     }     
  14.     public Queue(T element)  
  15.     {  
  16.         this();  
  17.         elementData[0] = element;  
  18.         rear++;  
  19.     }  
  20.     public Queue(T element , int initSize)  
  21.     {  
  22.         this.capacity = initSize;  
  23.         elementData = new Object[capacity];  
  24.         elementData[0] = element;  
  25.         rear++;  
  26.     }     
  27.     public int size()  
  28.     {  
  29.         return rear - front;  
  30.     }  
  31.     public void add(T element)  
  32.     {  
  33.         if (rear > capacity - 1)  
  34.         {  
  35.             throw new IndexOutOfBoundsException("the queue is full!");  
  36.         }  
  37.         elementData[rear++] = element;  
  38.     }  
  39.         public T remove()  
  40.     {  
  41.         if (empty())  
  42.         {  
  43.             throw new IndexOutOfBoundsException("queue is empty");  
  44.         }  
  45.           
  46.         @SuppressWarnings("unchecked")  
  47.         T oldValue = (T)elementData[front];  
  48.           
  49.         elementData[front++] = null;   
  50.         return oldValue;  
  51.     }    
  52.     @SuppressWarnings("unchecked")  
  53.     public T element()    
  54.     {    
  55.         if (empty())    
  56.         {    
  57.             throw new IndexOutOfBoundsException("queue is empty");    
  58.         }    
  59.         return (T)elementData[front];    
  60.     }     
  61.     public boolean empty()  
  62.     {  
  63.         return rear == front;  
  64.     }  
  65.       
  66.     public void clear()  
  67.     {         
  68.         Arrays.fill(elementData , null);  
  69.         front = 0;  
  70.         rear = 0;  
  71.     }  
  72.     public String toString()  
  73.     {  
  74.         if (empty())  
  75.         {  
  76.             return "[]";  
  77.         }  
  78.         else  
  79.         {  
  80.             StringBuilder sb = new StringBuilder("[");  
  81.             for (int i = front  ; i < rear ; i++ )  
  82.             {  
  83.                 sb.append(elementData[i].toString() + ", ");  
  84.             }  
  85.             int len = sb.length();  
  86.             return sb.delete(len - 2 , len).append("]").toString();  
  87.         }  
  88.     }  
  89.     public static void main(String[] args){  
  90.         Queue<String> queue = new Queue<String>("ABC"20);  
  91.         queue.add("DEF");  
  92.         queue.add("egg");  
  93.         System.out.println(queue.empty());  
  94.         System.out.println(queue.size());  
  95.         System.out.println(queue.element());  
  96.         queue.clear();  
  97.         System.out.println(queue.empty());  
  98.         System.out.println(queue.size());  
  99.     }  
  100. }  

队列只能在表头进行删除,在表尾进行增加,这种结构的特点,适用于排队系统。

五、栈

栈是一种后进先出(Last In First Out,LIFO)的数据结构,我们采用单链表实现一个栈。

  1. package com.xtfggef.algo.stack;  
  2. import com.xtfggef.algo.linkedlist.LinkedList;  
  3. public class Stack<T> {  
  4.   
  5.     static class Node<T> {  
  6.         T data;  
  7.         Node<T> next;  
  8.   
  9.         Node(T data, Node<T> next) {  
  10.             this.data = data;  
  11.             this.next = next;  
  12.         }  
  13.   
  14.         Node(T data) {  
  15.             this(data, null);  
  16.         }  
  17.     }  
  18.   
  19.     @SuppressWarnings("rawtypes")  
  20.     static LinkedList list = new LinkedList();  
  21.   
  22.     @SuppressWarnings("unchecked")  
  23.     public T push(T item) {  
  24.         list.addFromHead(item);  
  25.         return item;  
  26.     }  
  27.   
  28.     public void pop() {  
  29.         list.removeFromHead();  
  30.     }  
  31.   
  32.     public boolean empty() {  
  33.         return list.isEmpty();  
  34.     }  
  35.   
  36.     public int search(T t) {  
  37.         return list.indexOf(t);  
  38.     }  
  39.   
  40.     public static void main(String[] args) {  
  41.         Stack<String> stack = new Stack<String>();  
  42.         System.out.println(stack.empty());  
  43.         stack.push("abc");  
  44.         stack.push("def");  
  45.         stack.push("egg");  
  46.         stack.pop();  
  47.         System.out.println(stack.search("def"));  
  48.     }  
  49. }  

六、树

树形结构是一类重要的非线性结构。树形结构是结点之间有分支,并具有层次关系的结构。它非常类似于自然界中的树。树结构在客观世界中是大量存在的,例如家谱、行政组织机构都可用树形象地表示。树在计算机领域中也有着广泛的应用,例如在编译程序中,用树来表示源程序的语法结构;在数据库系统中,可用树来组织信息;在分析算法的行为时,可用树来描述其执行过程。本章重点讨论二叉树的存储表示及其各种运算,并研究一般树和森林与二叉树的转换关系,最后介绍树的应用实例。

七、二叉树

二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的、分别称作这个根的左子树右子树的二叉树组成。关于更多概念,请大家自己上网查询,我们这里将用代码实现常见的算法。更多的概念,请访问:http://student./course_ware/data_structure/web/SHU/shu6.2.3.1.htm 。

1、二叉树的建立

首先,我们采用广义表建立二叉树(关于广义表的概念,请查看百科的介绍:http://baike.baidu.com/view/203611.htm)

我们建立一个字符串类型的广义表作为输入:

String  expression = "A(B(D(,G)),C(E,F))";与该广义表对应的二叉树为:


写代码前,我们通过观察二叉树和广义表,先得出一些结论:

  • 每当遇到字母,将要创建节点
  • 每当遇到“(”,表面要创建左孩子节点
  • 每当遇到“,”,表明要创建又孩子节点
  • 每当遇到“)”,表明要返回上一层节点
  • 广义表中“(”的数量正好是二叉树的层数

根据这些结论,我们基本就可以开始写代码了。首先建议一个节点类(这也属于一种自定义的数据结构)。

  1. package com.xtfggef.algo.tree;  
  2.   
  3. public class Node {  
  4.   
  5.     private char data;  
  6.     private Node lchild;  
  7.     private Node rchild;  
  8.   
  9.     public Node(){  
  10.           
  11.     }  
  12.     public char getData() {  
  13.         return data;  
  14.     }  
  15.   
  16.     public void setData(char data) {  
  17.         this.data = data;  
  18.     }  
  19.   
  20.     public Node getRchild() {  
  21.         return rchild;  
  22.     }  
  23.   
  24.     public void setRchild(Node rchild) {  
  25.         this.rchild = rchild;  
  26.     }  
  27.   
  28.     public Node getLchild() {  
  29.         return lchild;  
  30.     }  
  31.   
  32.     public void setLchild(Node lchild) {  
  33.         this.lchild = lchild;  
  34.     }  
  35.   
  36.     public Node(char ch, Node rchild, Node lchild) {  
  37.         this.data = ch;  
  38.         this.rchild = rchild;  
  39.         this.lchild = lchild;  
  40.     }  
  41.   
  42.     public String toString() {  
  43.         return "" + getData();  
  44.     }  
  45. }  
根据广义表创建二叉树的代码如下:

  1. public Node createTree(String exp) {  
  2.         Node[] nodes = new Node[3];  
  3.         Node b, p = null;  
  4.         int top = -1, k = 0, j = 0;  
  5.         char[] exps = exp.toCharArray();  
  6.         char data = exps[j];  
  7.         b = null;  
  8.         while (j < exps.length - 1) {  
  9.             switch (data) {  
  10.             case '(':  
  11.                 top++;  
  12.                 nodes[top] = p;  
  13.                 k = 1;  
  14.                 break;  
  15.             case ')':  
  16.                 top--;  
  17.                 break;  
  18.             case ',':  
  19.                 k = 2;  
  20.                 break;  
  21.             default:  
  22.                 p = new Node(data, nullnull);  
  23.                 if (b == null) {  
  24.                     b = p;  
  25.                 } else {  
  26.                     switch (k) {  
  27.                     case 1:  
  28.                         nodes[top].setLchild(p);  
  29.                         break;  
  30.                     case 2:  
  31.                         nodes[top].setRchild(p);  
  32.                         break;  
  33.                     }  
  34.                 }  
  35.             }  
  36.             j++;  
  37.             data = exps[j];  
  38.         }  
  39.         return b;  
  40.     }  
思路不难,结合上述的理论,自己断点走一遍程序就懂了!

2、二叉树的递归遍历

二叉树的遍历有三种:先序、中序、后序,每种又分递归和非递归。递归程序理解起来有一定的难度,但是实现起来比较简单。对于上述二叉树,其:

    a 先序遍历

            A B D G C E F

    b 中序遍历

           D G B A E C F 

    c 后序遍历

           G D B E F C A

先、中、后序递归遍历如下:

  1. /** 
  2.      * pre order recursive 
  3.      *  
  4.      * @param node 
  5.      */  
  6.     public void PreOrder(Node node) {  
  7.         if (node == null) {  
  8.             return;  
  9.         } else {  
  10.             System.out.print(node.getData() + " ");  
  11.             PreOrder(node.getLchild());  
  12.             PreOrder(node.getRchild());  
  13.   
  14.         }  
  15.     }  
  16.   
  17.     /** 
  18.      * in order recursive 
  19.      *  
  20.      * @param node 
  21.      */  
  22.     public void InOrder(Node node) {  
  23.         if (node == null) {  
  24.             return;  
  25.         } else {  
  26.             InOrder(node.getLchild());  
  27.             System.out.print(node.getData() + " ");  
  28.             InOrder(node.getRchild());  
  29.         }  
  30.     }  
  31.   
  32.     /** 
  33.      * post order recursive 
  34.      *  
  35.      * @param node 
  36.      */  
  37.     public void PostOrder(Node node) {  
  38.         if (node == null) {  
  39.             return;  
  40.         } else {  
  41.             PostOrder(node.getLchild());  
  42.             PostOrder(node.getRchild());  
  43.             System.out.print(node.getData() + " ");  
  44.         }  
  45.     }  
二叉树的递归遍历实现起来很简单,关键是非递归遍历有些难度,请看下面的代码:

3、二叉树的非递归遍历

先序非递归遍历:

  1. public void PreOrderNoRecursive(Node node) {  
  2.         Node nodes[] = new Node[CAPACITY];  
  3.         Node p = null;  
  4.         int top = -1;  
  5.         if (node != null) {  
  6.             top++;  
  7.             nodes[top] = node;  
  8.             while (top > -1) {  
  9.                 p = nodes[top];  
  10.                 top--;  
  11.                 System.out.print(p.getData() + " ");  
  12.                 if (p.getRchild() != null) {  
  13.                     top++;  
  14.                     nodes[top] = p.getRchild();  
  15.                 }  
  16.                 if (p.getLchild() != null) {  
  17.                     top++;  
  18.                     nodes[top] = p.getLchild();  
  19.                 }  
  20.             }  
  21.         }  
  22.     }  
原理:利用一个栈,先序遍历即为根先遍历,先将根入栈,然后出栈,凡是出栈的元素都打印值,入栈之前top++,出栈之后top--,利用栈后进先出的原理,右节点先于左节点进栈,根出栈后,开始处理左子树,然后是右子树,读者朋友们可以自己走一遍程序看看,也不算难理解!

中序非递归遍历:

  1. public void InOrderNoRecursive(Node node) {  
  2.         Node nodes[] = new Node[CAPACITY];  
  3.         Node p = null;  
  4.         int top = -1;  
  5.         if (node != null)  
  6.             p = node;  
  7.         while (p != null || top > -1) {  
  8.             while (p != null) {  
  9.                 top++;  
  10.                 nodes[top] = p;  
  11.                 p = p.getLchild();  
  12.             }  
  13.             if (top > -1) {  
  14.                 p = nodes[top];  
  15.                 top--;  
  16.                 System.out.print(p.getData() + " ");  
  17.                 p = p.getRchild();  
  18.             }  
  19.         }  
  20.     }  
原理省略。

后续非递归遍历:

  1. public void PostOrderNoRecursive(Node node) {  
  2.         Node[] nodes = new Node[CAPACITY];  
  3.         Node p = null;  
  4.         int flag = 0, top = -1;  
  5.         if (node != null) {  
  6.             do {  
  7.                 while (node != null) {  
  8.                     top++;  
  9.                     nodes[top] = node;  
  10.                     node = node.getLchild();  
  11.                 }  
  12.                 p = null;  
  13.                 flag = 1;  
  14.                 while (top != -1 && flag != 0) {  
  15.                     node = nodes[top];  
  16.                     if (node.getRchild() == p) {  
  17.                         System.out.print(node.getData() + " ");  
  18.                         top--;  
  19.                         p = node;  
  20.                     } else {  
  21.                         node = node.getRchild();  
  22.                         flag = 0;  
  23.                     }  
  24.                 }  
  25.             } while (top != -1);  
  26.         }  
  27.     }  

八、树与二叉树的转换

本人之前总结的:



        本篇博文转载自:http://blog.csdn.net/zhangerqing/article/details/8796518;http://blog.csdn.net/zhangerqing/article/details/8822476;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多